« New* .NET 3.5 Feature: AddIn Framework Resources (Part 2 of 2) | Main | Multi-core CPU Caching: What is volatile? »

January 25, 2008


TrackBack URL for this entry:

Listed below are links to weblogs that reference CLR Fun Stuff: How is my C# code converted into machine instructions?:


Kazi Manzur Rashid

Shiplu bhai, I have somehow become addicted of your blogs. Keep up the good works rocking.

Chris Pietschmann

Very nice description.

But, you forgot to mention that the framework stores the native compilations in a cache so every time you run the application it doesn't have to recompile it over and over. This gives a performance boost since only the first time the code is run its compiled, and then everytime you execute the app after that it just loads the cached compilation.

There are some exceptions when the framework doesn't save the native compilation, but I'm not sure exactly what they are; I used to know but I've forgotten over the years. Basically, just remember that it does cache some of the JITted code to improve performance when it needs to run that code again.

I guess I'll have to look it up and post my findings...

Egil Hansen

There is also NGen, which is often used during installation of .net programs, to pre-compile the program in to the native language of the platform it is being installed on.

Also, as Chris mentions, the caching helps out a lot as well. I think the exception where caching do not work, is when reflection is used.

Nikola Malovic

First of all, great summary of NET execution model (one more subscriber to your blog here :) )

it is true that JIT compiler optimizes number fo compilations but the compiled mehtods are not stored in any cache, they are stored in High Frequency Loader Heap

Also, as far I am aware every method is been jit compiled once per type/domain

More details on basics of NET memory model can be found here

Shafqat Ahmed

Dear Kazi: Thank you vey much

Chris: If you look at the last diagram you will see that the function table points to the compiled version in memory. It does not point to the JITC function. So it would be cached as long as the compiled memory location remains. Since JITC function is responsible for compiling the MSIL and is no longer pointed by function it gets the performance boost as a reason of the function. I was not much explicit about it in the post.

Egil: Yes, Ngen.exe does CPU specific compilation but it does not take the exact version of the CPU and optimize to those instructions. So first time access to much of the code would be fast in Ngened executable but functions that are repeated many times may perform better in JITed version as JIT compilation will take the advantage of the CPU.

Malovic: Another reader to your posts as well, highly interesting!

Egil Hansen

Shafqat: Ok, it seems odd to me, that ngen would not use the same optimizations as the JIT compiler. Surely, both ngen and the JIT compiler have access to the same information.

Is there a rule of thump about when to use ngen and when not to?

Ohh and by the way, thanks for a interesting post. You have a new reader in me as well.

Shafqat Ahmed

Egil: I have read somewhere in MS blog that from .NET 2.0 the NGen.exe and the JITed code are different. It has been so mainly because, the team wanted less issues to arise from the NGen'ed executable. If you compile to x86 generic model you don't take advantge of the some extra instruction set provided in some processors then still it will support more processors. But with JITed code you can take advantage of those instructions.


You write very well.

Masudur Rahman

Very nice description.

Waleed Eissa

Thanks a lot, that was very informative, looking forward to reading more great posts like this one ...

generic viagra

Ohh and by the way, thanks for a interesting post. You have a new reader in me as well.

generic cialis

I think to not have a woman on the Legislative Council is tantamount to disregarding the views of close to half the population of Brunei Darussalam.

Lisa Alloju

My programming skill is not as good as yours.

The comments to this entry are closed.