If you are doing some reading regarding to the compilation of codes, you will of cause or should come across this word, "JIT".
As mentioned by itself, just in time compilation happen just at the time when it is needed.
I did some searching and finally landed on a MSDN article to understand more about JIT.
Why JIT is 'born'?
- to improve the first access performance.
When the loader only load and compile during runtime for the needed assemblies, it allows the application to start up quicker compare to those application that is use interpreter or native compiler. But is it always improve for both cold and warm start up? Read further on the blog to get more insight.
How does JIT works?
- use late binding to load only when it is needed.
With JIT, machine code that is generated is thrown away once the application stop debug / exit. It need to recompile every time. The generated code is not shared because the generated code is tied to the process. These characteristics cause some performance drawback.
The savor of JIT performance drawback has arise!! which is using NGen.
NGen?????
NGen is a process of pre-compiling MSIL (Microsoft Intermediare Language) executables in to machnine code prior execution time. It pre-compile before run time, and even before it get publish.
How does NGen ( pre-compile) safe JIT from performance?
1) Reduce application start-up time ( pre-compile before run time)
2) improve memory usage by allowing the code page to be shared across multiple processes).
Since sharing is caring~ sharing does brings some benefit. But we all know very well, sharing has some downside too. If I share my apples with my friends, I get to eat lesser. Same goes to the sharing in NGen, it has some took some extra disc space.
NGen had performance hit if MSIL Assemblies are deleted once they have been complied into NGen images or when the NGen image are deleted, both will cause the application not running correctly.
NGen minimize memory size by allow application to share the generated code component in assemblies, BUT the size of NGen image is larger than MSIL assemblies.
The improve speed might not be significant for cold start up but it is significantly improve for warm start up because NGen reuse the pages.
How to use NGen?
-by pre-compiling the application using the ngen.exe tool ( ships with the Microsoft .Net framework redistributable)
-can choose to compile it asynchronously(based on priority) or synchronously(load all at the same time) or both.
Both? How to compile asynchronously and synchronously?
-divide the application assemblies according to priority. For absolutely critical (important) -> synchronous, for important but not critical -> priority is 1 or 2, less frequently used: priority : 3.
NGen can perform better if u give the correct booster!
GAC is like a container that can hold NGen images and by pass all the validations by loader.
To get the optimal performace from Ngen, all application need to be strong-named and installed into GAC.
For NGen, the base address is advised to be unique because re-base will happen if the loader cannot place the module at desired address. It gets more complicated when the assemblies are hard-binds to another assemblies.
JIT on the other had doesn't have this problem. Although bothe NGen and JIT are address based, the base address for JIT is generated at the run time based on where the code is placed in memory.
MSIL will not face this issue as the references in MSIL are token-based and not address based.
There is no way to directly specify the load adress of NGen image, developer need to explicitly specified by assigning the base addresses to the application's assemblies.
No comments:
Post a Comment