(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .

Windows Phone 8: Compile in the Cloud

(7 votes)
Peter Kuhn
>
Peter Kuhn
Joined Jan 05, 2011
Articles:   44
Comments:   29
More Articles
8 comments   /   posted on Nov 22, 2012
Categories:   Windows Phone

In the first part of this series, I have introduced you to some of the fundamental changes in Windows Phone 8 on the operating system level. A lot of the improvements however go far beyond that and affect not only the local situation on devices, but the surrounding infrastructure and phone ecosystem as a whole.

With this, I not necessarily mean the great improvements that have been introduced for the Windows Phone Store [1] or in the field of business opportunities – there's also a lot in the box on a very technical level that's not obvious to a quick passing view. One of these things is what I want to talk about today: Compile in the Cloud.

A Quick Look Back

To understand what has been changed and accomplished by that new feature, we first have to take a look back at how things are done on Windows Phone 7, or for that matter, on all other .NET platforms. When you use Visual Studio to build e.g. your C# code, the compiler does not produce a binary that contains machine code. Instead, your source code is translated into something called Intermediate Language (IL). Only when your code is about to be executed will a so-called just-in-time (JIT) compiler do the final step and translate the IL code into something that can be understood by and executed on the target machine architecture. This alone is a pretty fascinating detail, as 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 (we like to refer to this as the "Groundhog Day" effect :)).

image

Now the first question that pops into ones mind is: why would you want to do something like that? It seems like a lot of unnecessary extra work at first, does it? The truth is that this has quite some advantages in a lot of situations. First of all, Intermediate Language is a CPU-independent instruction set. This means that you can pass around the IL code as is, and as long as there's a JIT compiler that knows how to translate it to machine code, it'll work on all platforms without modification. Second, the fact that compilation happens every time the application is executed also means that it is built against the current version of installed dependencies (namely the .NET runtime itself) from scratch on each run – meaning that changes to that runtime will not break the application as long as the public API stays compatible. There's also some inherent optimization to the concept of just-in-time compilation: code that is not needed during execution also will never be compiled. And finally there's some other features that IL and the included metadata enable, for example enforcing code security and code validation. If you're interested in it, you can easily take a look at what the IL version of your code looks like using tools like ILdasm.exe [2]:

image

If you're scared by what you see, don't worry. If you never learned assembly language it's quite natural that it looks all gibberish.

One of the drawbacks of JIT compilation is that it adds some overhead at execution time. As you can imagine, things like parsing metadata, enforcing code validation, setting up dummy methods during load time (stubs) and compiling them into real code on the fly all come at a price. On desktop computers, this price usually is easy to pay, as the computational power is sufficient to do all these things in a blink of an eye. However, with limited resource like – let's say – on a mobile device, these penalties suddenly become very relevant, and no matter how optimized the mechanism is in terms of performance, we're suddenly talking about seconds instead of milliseconds. Additionally, we are even facing some kind of vicious circle here: on a smart phone, the JIT compiler should perform as fast as possible. However, fast usually means producing less optimized code, which on the other hand would result in additional benefits for applications. All in all, the clever concept of IL and JIT compilation is one of the reasons on Windows Phone 7 why we see relatively long start times of applications, sometimes in the range of several seconds. Something had to be done here, and Microsoft did :).

A Solution?

A seemingly obvious solution to the problem is to skip the IL and JIT part altogether and simply produce machine code, like it was and still is done in other languages today. That would eliminate issues like long start times of apps immediately, and even result in a performance boost at runtime too, because no code would have to be compiled on the fly. The idea of this is nothing new to the .NET world. In fact, a tool named NGEN has been around since the very early days of .NET [3]. NGEN generates platform-specific machine code from your IL assemblies that then can be used without the need for a tool like the JIT compiler at all.

imageThere are however some problems with this. One is that generating those native images is slow even on powerful machines. A prominent example of software that actively makes use of NGEN is the free image processing software Paint.NET [4] which incorporates an optimization step with NGEN as part of its installation. In some situations it's possible that this optimization takes several minutes to complete. Can you imagine the time it would take to do the same on a phone?

A more subtle, yet even more important detail can be found in the above linked page with the NGEN documentation:

"Images need to be regenerated when the original assembly or one of its dependencies is serviced."

The first part is somewhat obvious; when I update the original application, the optimized image needs to be recreated. However, the crucial part is the second one: when a dependency (read: .NET runtime components) are changed, the image needs to be recreated also. As a consequence, if we compiled apps to native code, updates to the runtime on the phone inevitably would require to recompile all installed apps on that device too. If optimizing one app potentially could take dozens of minutes, then recompiling your collection of a hundred apps would probably make you go berserk…

A Better Solution!

After this somewhat lengthy introduction on the topic I think you have already guessed that the real solution to all this must be the "Compile in the Cloud" feature that is mentioned so prominently in the headline already. However, simply moving the compilation of IL to native images (NGEN) to the cloud would be a weak solution that introduced all sorts of new problems. For example, 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. Re-downloading a hundred apps after a platform update? I don't think so. So, if you were a clever Microsoft employee and faced all these problems:

  • JIT is too slow, we can't (continue to) do that
  • Compiling to native images locally is way too slow, people would hate us
  • Re-downloading pre-compiled native images all the time also is impracticable, no chance!

Then what would you do? The correct answer is: simply create yet another language that we can compile to and that solves all these problems :). Ladies and gentlemen, please welcome MDIL – Machine Dependent Intermediate Language. Here is the process for this new concept:

imageTo understand why the solution here was to develop a new language, we first have to fully understand the reason why native images need to be regenerated their dependencies are changed. In the process of creating these images all kind of information about the physical layout of references is baked into the result, in particular address offsets. That data potentially becomes stale when those dependencies are changed, for example when new members are added to them. So despite the fact that an API may stay very similar and fully compatible to its previous version, the physical layout of that code can change and cause all sorts of problems up to very severe data inconsistencies and even corruption. This is when MDIL comes into play. It 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.

How You Can Benefit From This

What do you as a developer have to do for your app to support this? Literally nothing. That is one of the beautiful details of this feature: 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. Also, 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.

More Details

If you want to test your app as an optimized native image before submitting it to the Windows Phone Store, you can do that by following some simple steps. It requires a physical device though and cannot be performed in the emulator. Details on this can be found in the Windows Phone 8 SDK documentation "How to test the retail version of your app for Windows Phone 8" [5].

A lot more information on the topic can be found in both the Build session "Deep Dive into the Kernel of .NET on Windows Phone 8" [6] as well as the Channel 9 Going Deep show "Inside Compiler in the Cloud and MDIL" [7]. The latter one features Peter Sollich, who also is listed as the inventor of MDIL in Microsoft's patent application for it [8 (PDF)].

Summary

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. I'm pretty sure that a lot of developers even will never learn about this and frankly, they don't need to – which is why it is such an elegant solution. To the marketing people, it's probably simply "hey we've improved startup times so much it'll drop your jaw". To me, exploring the details of the technology once again was one of these magic moments that make me realize how much effort and thinking goes into pushing forward the platform and evolving the concepts that have been around for ten years now. Fascinating, isn't it?


Subscribe

Comments

  • TarekWahab

    Re: Windows Phone 8: Compile in the Cloud


    posted by TarekWahab on Nov 23, 2012 19:43

    Hi Peter,

    Great article. I understand the benefits of such an architecture over what was done in WP7. My confusion though arises from what you say regarding native images becoming stale when the platform is updated.

    " In the process of creating these images all kind of information about the physical layout of references is baked into the result, in particular address offsets. That data potentially becomes stale when those dependencies are changed, for example when new members are added to them"

    I don't understand why this should happen? If the libraries are linked dynamically (as is done on other native platforms) shouldn't the linking step take care of this? Or is it the case that the .NET libraries are statically linked to an application when we build it? Perhaps I don't understand how managed libraries work, and it would be good to get the clarification.

    Thanks.

  • MisterGoodcat

    Re: Windows Phone 8: Compile in the Cloud


    posted by MisterGoodcat on Nov 24, 2012 10:29

    Hi Tarek,

    although it may seem similar at first, no static linking happens here. However, the native images very much depend on the exact version of dependencies they were built against. The problems particularly arise with the way derived types work. They need to copy the physical layout of fields from their base classes, and also the layout of the method tables. Now, if for whatever reasons these layouts change in a platform update (and these changes may be completely unrelated to the functionality your derived classes are using), there's a mismatch between what's now the current, correct layout in the base classes and what your derived types have copied over when their images were built before. This leads to inconsistencies or even crashes, so these images all need to be recreated.

    I didn't want to scare people away by getting too technical :), but if you're interested in more details, I highly recommend watching the Channel 9 video with Mani Ramaswamy and Peter Sollich. He provides some very nice explanations that demonstrate when and why generated assembly becomes stale and needed to be recreated, and how MDIL solves this problem.

    I hope this helps.

  • panospace

    Re: Windows Phone 8: Compile in the Cloud


    posted by panospace on Nov 24, 2012 17:36

    Hi Peter, does compiling to MDIL eliminate the need to obfuscate code?

    Do Win RT apps also get compiled to MDIL or is this just used in Windows Phone?

  • MisterGoodcat

    Re: Windows Phone 8: Compile in the Cloud


    posted by MisterGoodcat on Nov 25, 2012 15:13

    Hi Panospace,

    compiling to MDIL has the same degree of "inherent obfuscation" as using native code. When you look at the output you'll realize that it generates assembly that is dependent on the CPU architecture, with some place holder tokens and specific instructions the "linker" knows how to treat. However, do not forget that what you as a developer upload to the Phone Store still is IL, and if your application supports 7.x those users will get the same IL assemblies like before - so no, I wouldn't say it eliminates the need for obfuscation at the moment, if you're sensitive about your code.

    The concept of MDIL is compelling and there have been hints that it is thought about how or if it can be brought over to other platforms to make them benefit from it too. However, at the moment there are no official announcements on this (that I'm aware of), and it is currently not used on other platforms than Windows Phone (8).


  • BeeOShop

    Re: Windows Phone 8: Compile in the Cloud


    posted by BeeOShop on Apr 29, 2015 15:02

    Mau info toko tas online di Bandung yang menjual berbagai macam tas untuk keperluan wanita dan anak-anak seperti tas-tas wanita yang branded dari berbagai merek terkenal seperti Louis Vuitton, PradaBrussels, Furla, Chanel, Hermes, dan banyak lagi tas branded wanita lainnya dengan bermacam model seperti dompetslingbaghandbag dan tote bag, yang trendy dengan harga yang terjangkau? Silahkan cek di Bee Bagshop grosir tas online di Bandung yang menjual tas-tas dengan moel dan warna yang bervariasi dengan harga yang murah. Selain tas wanita, kami juga menjual tas sekolah anak model selempang dan ransel yang juga branded tiruan seperti jansport dan kipling dengan banyak variasi model dan warna, dijamin putra-putri anda menyukainya.

    Bagi yang ingin merawat kulit wajah tanpa harus repot dan mahal, simak tips kecantikan berikut yang mudah dan murah. Coba Sabun Amoorea. Manfaat sabun ajaib amoorea ini diantaranya untuk berbagai permasalahan kulit anda, seperti jerawat, scars (bekas jerawat), mengencangkan kulit, membuat kulit tampak lebih cerah dan segar, mengatasi lingkar hitam di bawah mata (mata panda), dll. Untuk melihat review sabun amoorea ini klik di sini. Simak juga testimoni yang sudah menggunakan sabun ini, simak selengkapnya. Meski harga sabun amoorea ini cukup terjangkau, produk ini aman, karena terbuat dari 100% bahan alami dan sudah ada ijin dari BPOM sehingga anda tidak perlu khawatir dan ragu memakainya. Jika aman (tidak berbahaya) apakah memakai sabun amoorea ada efek sampingnya? Reaksi apa yang timbul saat pertama menggunakannya? Simak info lengkapnya di sini.

    How To Get Rid Of Acne and Scars site generally tell about  tips for beauty and health skin, and especially tell anything about acne, myths, acne and scars treatments , not only on your face (including acne and scars on your body such as chest and back)which may help you to war against acne and the scars to leaves on your face and body (back and chest), through naturally, home remedies, and medical treatment. Some people believe that natural treatment is most safe. But its seems would take time longer then medical treatment. In my oppinion, using medical treatment is more effective and you'll see the result faster. To find what the best treatment for your acne, better you understand what causes of acne. The following are some factors which can cause acne: Hormones, diet, stress, medication, clothing, environment factors, genes, and cosmetics. For back or chest acne cases, it may caused by your tight clothes. To get rid of acne on back or chest, it's good to wear synthetic loose clothing, such as cotton linen, to prevent excessive rubbing and irritation on the skin. Another treatment that can help you to get rid of back or chest acne is keep your body always clean, with take a bath regularly, preferably with an anti-bacterial soap or a salicylic acid-based cleanser is highly recommended. When acne goes, it will leaves behind the red on skin, and seems impossible to heal it fast overnight. So, it would be better to do something which may offer some remarkable effects on the redness from acne. If you choose naturally treatment,  just take a cube of ice, garlic, or lemon juice mixed with rose water, choose one of them, then apply to your reddish part of your face for a while. Do this continously and measured, then see the result. Below this, some good habits which may help you to get rid of acne scars naturally and fast are: first, drinking lots of water to makes your skin well hydrated, and finally make you skin regenerate faster.  Second, eat the food that containing much the essential vitamins and mineral to help repairing scars skin from the inside. Third, naturally treatment to get rid of acne scar, by applying aloe vera on scars, may help to avoid more scarring, soothe inflamation, and make your skin keep smooth. Fourth, treat your acne scars with natural ingredient, tomato. Its containing much vitamin A, which very efficient in repairing scars skin.  If you want to get rid of acne scars ons faster than naturally or home remedies treatments, using the latest product of technology, such as laser treatment and chemical peeling, or even you can choose microdermabration that guaranteed more effective than other treatments.

    Ada banyak bermacam permasalahan kulit di wajah, salah satu yang paling menggangu adalah jerawat, apalagi jika yang muncul adalah jerawat batu yang ukurannya besar dan menyebabkan rasa sakit. Solusi mengenai berbagai cara menghilangkan jerawat batu ini banyak sekali ditemukan, mulai dari pengobatan alami, maupun secara medis. Untuk kasus jerawat batu ini, sebaiknya anda bisa mengambil tindakan pengobatan yang tepat, untuk kasus jerawat batu disarankan langsung saja ambil perawatan medis, karena beresiko tinggi meninggalkan bekas berupa bopeng di wajah, jika salah menangani. Dengan langsung mengambil tindakan medis, setidaknya akan meminimalisasi resiko timbulnya scars bopeng di wajah yang mana scars jenis ini relatif sulit dihilangkan. Tidak seperti scars berupa noda/ flek hitam. meski tidak mudah juga, namun berkali-kali lipat jauh lebih mudah dan murah dibandingkan dengan mengatasi bopeng bekas jerawat. Perawatan alami yang dapat anda tempuh untuk mengobati jerawat ataupun scars yang berupa noda hitam/ flek bekas jerawat, anda dapat mencoba dengan menggunakan bahan alami misalnya jeruk nipis atau madu yang sudah sangat terkenal khasiatnya yang untuk kesehatan dan kecantikan kulit. Dengan perawatan alami, cenderung lebih aman, hanya saja untuk perawatan ini biasanya dibutuhkan waktu yang lebih lama serta ketelatenan untuk mengatasinya. FYI, Jeruk nipis ini yang tidak hanya dapat memudarkan flek hitam di wajah, tetapi juga dapat memutihkan dan mencerahkan kulit anda. Namun jika anda tidak memiliki cukup kesabaran, sebaiknya langsung saja konsultasi dengan dokter. Untuk menentukan metode pengobatan jerawat dan scars ini biasanya dokter akan melihat tingkat keparahan dari jerawat/ scars anda, bisa dengan pemberian obat minum seperti anti biotik, obat oles (salep), peeling, mikrodermabrasi, laser atau metode pengobatan lainnya. Namun sebelum anda memutuskan menggunakan salah satu treatment tersebut sebaiknya telah jelas terlebih dahulu mengenai efek samping yang ditimbulkan.

    Seiring dengan semakin tingginya pengguna internet dari waktu ke waktu, hal ini menciptakan peluang bisnis tersendiri. Diataranya peluang bejualan tas murah secara online, buatan konveksi-konveksi lokal. Di Bandung cukup banyak konveksi tas yang membuat beraneka macam tas dan dompet tiruan atau kw. Model-model dari tas wanita ini kebanyakan menjiplak tas dan dompet wanita dari brand terkenal luar negeri seperti merek-merek LV atau Louis Vuitton, Hermes, Chanel, Furla, dll yang sangat terkenal. Tas tiruan seperti ini familiar disebut tas replika/ kw, dengan beraneka tingkatanya: Kw I, Kw II, dll. Tas-tas wanita branded ini di Bee Bagshop dijual dengan harga yang bevariasi antara 66-300 ribuan. Pembelinya berasal dari kalangan anak SMA, anak kuliah, pekerja kantoran, serta ibu-ibu muda. Untuk tas sekolah dan kuliah anak, kebanyakan modelnya meniru model tas branded seperti kipling, dan jansport, dengan model tas punggung/ ransel ataupun model selempang. Untuk harga tas-tas branded kw dijual dengan harga yang jauh di bawah harga aslinya, sehingga bisa dipastikan kualitasnya pun berbeda dengan yang original. Di Bee Bagshop sendiri kisaran harga untuk tas jansport tiruan ini dijual dengan harga 60-80 ribuan, sementara harga barang original mungkin sekitar 800-900 ribuan, bahkan mungkin ada juga yang lebih mahal. Untuk tas ransel branded tiruan ini kebanyakan pembelinya berasal dari anak sekolah dan kuliah.

    Jika anda wanita yang terlalu sibuk bekerja, dan selalu bingung bahkan tidak sempat untuk sekedar memikirkan menu masakan sehari-hari untuk keluarga anda, silahkan kunjungi situs Aneka Resep dan Cara membuat Masakan Praktis Khas Indonesia, situs ini berisi kumpulan resep masakan yang menggunakan bahan-bahan umum seperti ayam, daging sapi, serta beraneka seafood (udang, cumi, ikan), dan juga bahan-bahan makanan yang tidak asing bagi  masyarakat Indonesia seperti tahu, tempe, serta nasi, . Selain itu ada juga resep cemilan khas nusantara, yang dapat anda jadikan inspirasi untuk menu masakan dan makanan ringan keluarga anda di rumah. Seperti resep membuat masakan khas suatu daerah seperti soto ayam lamongan, ataupun masakan khas lebaran seperti opor ayam, beraneka masakan berbahan dasar ayam ataupun bahan makanan lainnya, yang pastinya praktis dan mudah dibuat.

  • wardoyoming

    Re: Windows Phone 8: Compile in the Cloud


    posted by wardoyoming on May 17, 2015 11:37
    nning the job Make sure you have the right number and sizes of props InspecinPre Harga besi h beam 300 x 300 x 10 x 15 x 12 m untuk kontraktor Boat Canvas Brochure Get expert advice on Doral Boat Tops and CEditorial Guideli Jual Baja cnp 150 x 50 x 20 x 2 x 6 m terbesar certain systems, and in certain environments - for instancehe piles can roll ba jual besi beton ulir 8 mm 12 m pekanbaru master steel About UsFAQContact UsMember BenefitsPrivacy PolicyShopSite MapBlro norm specific grosir besi plat bordes chekered plat 3 mm jayapura jual besi bajaSubmitted On May 30, 2013Recommend Article Article Comments Printch CurvesDoral distributor baja siku 75 x 75 x 7 mm x 6 m terbaru moves to another location, simply disassemble the building andngVideo LibraryAd toko baja plat hitam kalimantan tengah
  • wardoyoming

    Re: Windows Phone 8: Compile in the Cloud


    posted by wardoyoming on May 17, 2015 11:38
    ainment: MusicDominic HoughWe will never sell or rent your email address Mu cara membuat blog link web
  • doktercinta

    Re: Windows Phone 8: Compile in the Cloud


    posted by doktercinta on May 25, 2015 17:43
    I hope this website the more visitors it. by. obat perangsang wanita

Add Comment

Login to comment:
  *      *       

From this series