JIT compilation question

  • Thread starter Thread starter Sheila Jones
  • Start date Start date
S

Sheila Jones

Hello,

I posted this as a follow-on question yesterday, but nobody has bitten:

Is a .Net application recompiled (i.e. from CIL to native code) each time it
is run? Or is the native code cached - and if so, where? Thanks.
 
It is compiled only once, then it is stored in the native assembly cache,
or something.
You can precompile it yourself too with ngen.exe
But it gets a little more complicated.
The whole application isn't compiled until the parts are needed, so the
initial startup time will be fast enough, but you may encounter slowdowns
the first time you do new stuff, like getting a new exception.
 
Each method is JIT compiled when called for the first time, the JIT'd code
is NOT persisted, it's simply cached in memory and remains there until the
program ends (application domain unloads).

Willy.
 
Thanks for the reply. I knew about methods only being compiled the first
time they are called, I just didn't know whether the CLR forgot everything
it had compiled when the application terminated. You say it stores the
native code.

I suppose the follow-on question is: how does the CLR know that the cached
native code is still valid and that, for example, the .exe hasn't been
replaced with an updated version?
 
So you are saying that a CLR program is re-JITted each time it is run? Why
isn't the native code cached?
 
So you are saying that a CLR program is re-JITted each time it is run? Why
isn't the native code cached?

As I understand it, if the native code were cached, then the CLR would be
unable to validate it the next time its run. Basically for security reasons.

n!
 
I suppose the follow-on question is: how does the CLR
know that the cached
native code is still valid and that, for example, the .exe hasn't been
replaced with an updated version?

Hi,

Each time an assembly is compiled it is assigned an
internal MVID {Module Version Identifier}. The MVID is
seperate and unrelated to any "user friendly" versioning
schemes; it exists specifically so that the CLR can know
if an assembly's contents and/or public contracts have
changed since the last time it was JIT-ed.

When the CLR attempts to load a cached pre-compiled
module's native image the native image's MVID is compared
to the MVID in the deployed assembly, and if necessary the
code is re-JIT-ed. Note that the compiled native image
stores the MVID of the target module AND MVIDS OF ALL
MODULES THE TARGET DEPENDS ON - so a change to any
dependancy can trigger a recompilation of a whole bunch of
stuff...

--Richard
 
-----Original Message-----
Each method is JIT compiled when called for the first time, the JIT'd code
is NOT persisted, it's simply cached in memory and remains there until the
program ends (application domain unloads).

Willy.

Uhhm, this is not correct according to "Essential .NET
Volume 1" {Don Box, Chris Sells} pages 153-157. Please
refer to my other reply in this question thread for a
synopsis of the Don Box answer to this question...

--Richard
 
Richard,

Am I? I was only replying to OP's question which did not mentioned running
pre-jitted code (NGEN).

<Is a .Net application recompiled (i.e. from CIL to native code) each time
it
is run? Or is the native code cached - and if so, where? Thanks.>

Willy.
 
By default, the JIT happens every time, but it only happens for the
functions that are called. There's a tradeoff between the time it takes the
JIT to run and the time and overhead of opening another file and reading it
into memory. There's also the issue of tracking the environment (security,
etc.) of the method when it was JITted, and then comparing that with the
environment of the current program.

There is a tool named NGEN that can "pre-jit" an entire assembly for you as
a load optimization, and some of the BCL assemblies have that done. That
pre-jitted cache is only valid for the same environment, but the check is
done on a per-user basis. There is also another level of indirection in the
NGEN case, and the resulting code may be slightly less optimal.

Doing this on a per-function basis would be really complex.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Eric Gunnerson said:
I'm fairly sure that Don and Chris are talking about the NGEN case, not the
normal case.

I've just checked in the book, and indeed they are.
 
Back
Top