[Mono-list] Code optimizations

Robert Jordan robertj at gmx.net
Fri Feb 20 09:30:57 EST 2009


Yoav HaCohen wrote:
> Here is my first idea:
> 
> Suppose we have the following code:
> 
> [DllImport(NATIVE_LIBRARY, CallingConvention = CallingConvention.Cdecl)]
>         private static extern void NativeFoo(int temp, out int cx, out int
> cy);
> 
> public System.Drawing.Point Foo()
> {
> 	int cx, cy;
> 	NativeFoo(int a, out cx, out cy);
> 	return new System.Drawing.Point(cx, cy);
> |

I'm aware that you did not intend to start a discussion
about p/invoke, but I could not resist (1):

You don't want to p/invoke just to get one point out of
the unmanaged world. The managed/unmanaged transition
is too costly for this. Any optimization done by the JIT
after this call would be pointless.

When dealing with (arrays of) Point/Rectangle/Size (and other similar
well known structs), there is a (rather dirty) way to gain speed:

Define Point/Rectangle/Size as structs in the unmanaged
world and pass them by reference back and forth.

Robert

(1) that's why designers are using "lorem ipsum" paragraphs for
prototyping ;)



More information about the Mono-list mailing list