[Mono-list] Code optimizations

Yoav HaCohen yoavhacohen at gmail.com
Fri Feb 20 06:43:52 EST 2009


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);
|

This code could be written like that:
public System.Drawing.Point Foo()
{
	int cx, cy;
	System.Drawing.Point result;
	NativeFoo(int a, out result.Y, out result.X);
	return result;
|

But it's possible to pass a property as an out or ref argument.

However, it's easy for the compiler to find when the properties are inlined
and the private fields of the struct point are referenced directly, the code
can be translated in compile time to:
public System.Drawing.Point Foo()
{
	int cx, cy;
	System.Drawing.Point result;
	NativeFoo(int a, out result.y, out result.x); // use the directly the
private fields of System.Drawing.Point.

	return result;
|

In addition, if the struct result is allocated on the caller stack (also by
compiler optimization), than we should eliminate the copy of the two
integers twice.

What do you think?
-- 
View this message in context: http://www.nabble.com/Code-optimizations-tp22119164p22119176.html
Sent from the Mono - General mailing list archive at Nabble.com.



More information about the Mono-list mailing list