Managed vs Native Linkers

MonoTouch 5.3.2 is now available (in the alpha channel) and if you look at your application size you’ll notice an executable that’s a bit smaller than before. It must be the linker right ? sort of.

The managed linker (the one I often write about) is unchanged in this new alpha release – but it’s not the only linker being used when building MonoTouch applications. The native linker, part of gcc and coming with Apple iOS SDK, also plays a role in removing unused code. Has this linker changed ? no, but the Mono[Touch] runtime has.

The Mono runtime has internal calls (icalls), i.e. native functions that are called from managed code. E.g. to get the sinus of an angle you call the System.Math.Sin(double) method, which is defined like this:

[MethodImplAttribute (MethodImplOptions.InternalCall)]
public extern static double Sin (double a);

and, because of the [MethodImp(MethodImplOptions.InternalCall)] attribute, the call ends up inside the following function of the Mono runtime:

gdouble 
ves_icall_System_Math_Sin (gdouble x)
{
	MONO_ARCH_SAVE_REGS;
	return sin (x);
}

In many ways it’s similar to p/invokes but, until this release, it was not possible for the native linker to remove the unused icalls (native code) from your applications, i.e. you always got every icall MonoTouch was built with.

Zoltan (who does all kind of extraordinary things, minus blogging them) has changed how icalls are stored and called in a way that enables the native linker to eliminate them. Since the managed linker already removed unused references we’re now in a situation where the native linker can detect and remove more unused code than ever before.

How big are those savings ? The actual savings will vary according to how many (and which) icalls your application requires. E.g. MonoTouch.Dialog‘s Sample binary executable (release build) is 114 kb smaller, about 1.7%, in 5.3.2 than it was for 5.2.9.

About these ads

About spouliot

Xamarin Hacker, Mono Contributor, Open Source Enthusiast with strong interests in Code Analysis, Cryptography and Security. Presently working on MonoTouch, previous projects were Moonlight and libgdiplus and a lot of the cryptographic work done on Mono.
This entry was posted in linker, mono, monotouch, xamarin. Bookmark the permalink.

One Response to Managed vs Native Linkers

  1. Pingback: Managed Crypto vs CommonCrypto | take a bit, get a byte

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s