NGENing some misconceptions,

NGen or Native code GENeration, is a utility that comes along with .net framework, we will see what it does in a moment , first i wuld like to breifly discuss the execution of .net assemblies in clr,

say i have to develop an application , that will run over CLR, ok i ll choose any CLS -compliant language  whichever suits my requirement, and write code using it, then i ll use the respective compiler to compile my code down to MSIL and i also requested the compiler to generate the assembly also ,did you notice the difference ??

generating MSIL code is not equivalent to generating assembly , infact its a subset, u will be generating msil code if u r generating a .net module ,or multifile assembly. genrating the assembly  require the compiler to put in manifest data also ,the manifest is the meta data about the assembly ,what it contains , strong name , referneced assemblies ,type it contains, that is why assemblies are also called self descripting components.:)

ok so i got an assembly , say myApp.dll/ or myApp.exe which everrs suits u , i ll go with myApp.exe

so far so good,

now i tried to execute the assembly, the assemblies generated as executables contains bootstrap code, that inovke the clr ,loads it into the process ,creates application domain , and hand over the execution to the managed code entry point. When managed code starts execution , since its the MSIL , so it must be converted to native code , this is done by a JIT compiler, which not only genreates code on fly but also optimize it according  to the platform ,application is running on,the jit compilation is done on method by method basis, when ever a method is called , clr looks in to the method address table that it maintains, if there is an address location specified that means the mehtod has already been converted to native , else , jit comes in action , it takes the whole method  generate its native code,in doing so , if the method refers to a type declared in other  assemblies , then at this moment that reffered assembly is also loaded .and then the execution resumed as if nothign happened

hmmmm now where is ngen in this scenario. ?

ngen is used to CONVERT MSIL code NATIVE Code, the sole reason is to reduce the applicaiton load time , and avoid jitting , also since its one time operation , it tries to optimize more then jitter. THE PURPOSE OF NGEN is to enhance performance. but ngen is not very effective cuz it comes with limitations, i ll point some ,and the rest u can read with links i ll provide at the end,

when u ngen an assembly , say

ngen myApp.exe

ngen loads myApp.exe and CLR and generates it native output that will be stored in winnt/assembly/native (something like that),
some Important points
1)  ngen does not convert ur assembly(myApp.exe) rather it produce a separate output.
2) ngen does not generate assembly manifest to native code
3) ngen generated code is installed in assembly cache , u cannot redistribute,
4) ngen generated code is build specific and each compilation will result in a different build no.
5) ngenerated code is very touchy about its environment, if clr detects changes in its environment like u installed extra rams,updated ur framework installaiton , it will refuse to use the ngen geneated code and will call jitter to rescue.
6) when deploying assemblies , u can use install-time ngening, but u must have actual assemblies installed on the deployment pc , u cant just take the ngened code and put it to deployment pc, this is due to the fact that the manifest still resides in assembly file 🙂

gosh !! ngen  is a blessing or what !!

it depends totally on ur solution what seems to fit , i think if u want full show from clr then its best to le jitter do it work , ms is trying to improve jit performance with more optimizations and jit-compiliation .

check these links for detailed info
http://www.codeguru.com/Csharp/.NET/net_general/toolsand3rdparty/article.php/c4651/

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpgrfnativeimagegeneratorngenexe.asp

in my last post , my freind Raja Khurram Shehzad  proposed that to avoid code tempering we can use ngen to use native code on client machines,  i think i have pointed enough issues , if i have put something wrong , plz correct me. but still i m not conviced that ngen will protect code tempering.

regards

Advertisements

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