Why does a call to a function in a dll take less time if you call it repeatedly?

  • Thread starter Thread starter Justin Galzic
  • Start date Start date
J

Justin Galzic

Could explain how the .net assemblies (Dlls) work when they are used
by an exe -- I'd much appreciate it. If your exe is making a call to a
function that's part of the .net framework library (ie
System.Math.Ceiling), the amount of time taken up on the first call to
the specific function is usually about 80-90% more than when the
function is called again. Can someone explain to me why this works as
it does? I know that when you use a Dll, it's got to get loaded into
the system and map all of proc addresses for the functions into
memory. On the first call, are these mapping of the function to a
particular location in memory left blank and is the majority of time
spent filling in these mappings?

This brings up the question of whether there is a way to load the Dlls
you're going to use for a certain EXE and set it up so everything is
pre-cached? Is it possibly a setting for your project in Visual
Studio.net?

Apologies if this sounds vague but I'm not entirely familiar with this
stuff just yet.

Thanks,
Justin
 
Beyond the memory loading and mapping, IL libaraies also have to be JIT
compiled (from IL to x86, for example) when loaded for the first time. After
that, the cached native code is reused, which is much faster.
 
Ed, thanks for answering to my post. If the underlying CIL is complied
with JIT compiler only when the assembly is loaded, is it possible to
do the JIT compilation of all of the functions that you'll use from a
particular set of libraries ahead of time before the actual time
you'll go to use them so everything is cached as native code right
away?

Justin
 
Check out Ngen at
http://msdn.microsoft.com/library/d...ols/html/cpgrfnativeimagegeneratorngenexe.asp.

Justin Galzic said:
Ed, thanks for answering to my post. If the underlying CIL is complied
with JIT compiler only when the assembly is loaded, is it possible to
do the JIT compilation of all of the functions that you'll use from a
particular set of libraries ahead of time before the actual time
you'll go to use them so everything is cached as native code right
away?

Justin

"Ed Kaim [MSFT]" <[email protected]> wrote in message
Beyond the memory loading and mapping, IL libaraies also have to be JIT
compiled (from IL to x86, for example) when loaded for the first time. After
that, the cached native code is reused, which is much faster.
 
Besides ngen, you could write a method that references all the methods you
want compiled, and call that method, like this:

void TriggerJIT() {
if (1 == 0) {
myObj.MethodToCompileA();
myObj.MethodToCompileB();
myObj.MethodToCompileC();
Math.Ceiling(0);
}
}

The only caveat is that you couldn't apply any compiler optimizations that
do dead-code elimination. You could put this method in a seperate assembly
and never compile it with optimizations, or if you don't need to, you could
just leave optimizations out of your main assembly.

Chris Capel

Justin Galzic said:
Ed, thanks for answering to my post. If the underlying CIL is complied
with JIT compiler only when the assembly is loaded, is it possible to
do the JIT compilation of all of the functions that you'll use from a
particular set of libraries ahead of time before the actual time
you'll go to use them so everything is cached as native code right
away?

Justin

"Ed Kaim [MSFT]" <[email protected]> wrote in message
Beyond the memory loading and mapping, IL libaraies also have to be JIT
compiled (from IL to x86, for example) when loaded for the first time. After
that, the cached native code is reused, which is much faster.
 
Nevermind that. I was confused.

Chris Capel said:
Besides ngen, you could write a method that references all the methods you
want compiled, and call that method, like this:

void TriggerJIT() {
if (1 == 0) {
myObj.MethodToCompileA();
myObj.MethodToCompileB();
myObj.MethodToCompileC();
Math.Ceiling(0);
}
}

The only caveat is that you couldn't apply any compiler optimizations that
do dead-code elimination. You could put this method in a seperate assembly
and never compile it with optimizations, or if you don't need to, you could
just leave optimizations out of your main assembly.

Chris Capel

Justin Galzic said:
Ed, thanks for answering to my post. If the underlying CIL is complied
with JIT compiler only when the assembly is loaded, is it possible to
do the JIT compilation of all of the functions that you'll use from a
particular set of libraries ahead of time before the actual time
you'll go to use them so everything is cached as native code right
away?

Justin

"Ed Kaim [MSFT]" <[email protected]> wrote in message
Beyond the memory loading and mapping, IL libaraies also have to be JIT
compiled (from IL to x86, for example) when loaded for the first time. After
that, the cached native code is reused, which is much faster.

Could explain how the .net assemblies (Dlls) work when they are used
by an exe -- I'd much appreciate it. If your exe is making a call to a
function that's part of the .net framework library (ie
System.Math.Ceiling), the amount of time taken up on the first call to
the specific function is usually about 80-90% more than when the
function is called again. Can someone explain to me why this works as
it does? I know that when you use a Dll, it's got to get loaded into
the system and map all of proc addresses for the functions into
memory. On the first call, are these mapping of the function to a
particular location in memory left blank and is the majority of time
spent filling in these mappings?

This brings up the question of whether there is a way to load the Dlls
you're going to use for a certain EXE and set it up so everything is
pre-cached? Is it possibly a setting for your project in Visual
Studio.net?

Apologies if this sounds vague but I'm not entirely familiar with this
stuff just yet.

Thanks,
Justin
 
Back
Top