Update – 10/2013: Check out Announcing the release of the .NET Framework for Windows Phone 8 from the .NET Team blog to learn more about how to use the .NET MDIL compiler.
While developing Windows Phone 7 or other .NET based applications on Visual Studio, building the project converted the code to an Intermediate Language (IL) which was later converted to binary, by a just-in-time (JIT) compiler, when it needed to be executed. The JIT compiler is invoked on a per-method basis as soon as the method in question is invoked. For subsequent calls to the method, the previous results are cached and reused, but only until the application is shut down! Then, all the compiled code is simply thrown away and the whole process starts over the next time the application is launched.
Advantages of this:
- Intermediate Language is a CPU-independent instruction set and works on all platforms without modification as long as it has a JIT compiler.
- As compilation happens every time the application is executed, it is built against the current version of installed dependencies (namely the .NET runtime itself) from scratch on each run. Changes to that runtime will not break the application as long as the public API stays compatible.
- Some other features that IL and the included metadata enable are enforcing code security and code validation.
However, JIT compilation adds some overhead at execution time and the limitation of computational resources make things like parsing metadata, enforcing code validation, setting up dummy methods during load time (stubs) and compiling them into real code on the fly very expensive, costing seconds as opposed to milliseconds. Also, on smart phones the JIT compiler needs to work as fast as possible which means a less optimum code will be generated curbing the resulting additional benefits for the application. This lead to the relatively long application startup time on WP7.
Solution: Compile in the Cloud with MDIL
Before going into compiling in the cloud another solution that could be thought of, though it isn't as effective, is generating platform-specific machine code from the IL assemblies that then can be used without the need for a tool like the JIT compiler at all. However, generating those native images is slow even on powerful machines and images need to be regenerated when the original assembly or one of its dependencies is serviced. Simply moving the compilation of IL to native images, to the cloud would be a weak solution that introduces all sorts of new problems. Even if we used the power of the cloud to do the heavy lifting, we still would need to download that newly compiled image to our phones.
So, to solve this create yet another language that we can compile to and this language is MDIL - Machine Dependent Intermediate Language. MDIL is a format that somewhat resembles the final machine code, but it has placeholder tokens in all the places that usually would contain the hard-coded literals that potentially cause problems. This concept allows to have an "almost done" native image that can be finished in a final, very lean step directly on the phone. Once any dependencies change, that final step can easily be repeated in little time. The actual creation of the MDIL file (which is the heavy lifting in this process) can be performed in the cloud. When a client installs an app from the Phone Store, he already receives that prepared MDIL image from Microsoft's servers.
- As a developer you have to do nothing to support it. the initial compilation to MDIL is performed the moment when you upload the application to the Store. You don't have to do anything to make this work, it happens automatically behind the scenes for you.
- You don't have to worry about problems with users who still run Windows Phone 7.x. Supposed your app supports these platforms of course, those users receive an unaltered version of your app package that is JIT compiled on the device just like before. Only users of Windows Phone 8 are provided with the optimized, pre-compiled MDIL version of your app.
The result of this new feature can immediately be seen when you run an app on a Windows Phone 8 device and compare in particular the startup time to its behavior on Windows Phone 7 devices – improvements of 50% and more are absolutely possible.
Compile in the Cloud is one of these hidden gems in the set of new features of Windows Phone 8 that most likely won't be noticed and talked about a lot outside of a rather small, interested community.