Sunday, April 30, 2006

When Your Need Is Speed

Came across a recent article form MSDN magazine (The Performance Benefits of NGen.). I have been really intersted in these CLR nitty gritty's, and Ngen is one of my favourite discussions with my CLR crazy freinds.

Typically the methods in managed executables are JIT (Just in time) compiled.The machine code generated by the JIT compiler is thrown away once the process running that executable exits; therefore, the method must be recompiled when the application is run again. Moreover, the generated code is tied to the process that created it and cannot be shared between processes that are running the same application. This can be a performance drawback.

But When Your Need Is Speed Ngen can be the answer.NGen refers to the process of precompiling Microsoft intermediate language (MSIL) executables into machine code prior to execution time. This results in two primary performance benefits. First, it reduces application startup time by avoiding the need to compile code at run time. Second, it improves memory usage by allowing for code pages to be shared across multiple processes.

Mind you, ngen is not jsut another traditional static back-end compilation tool.Unlike statically compiled binaries, NGen images are tied to the machine where they are created and hence cannot be deployed. Instead, the application's installer needs to issue commands to create native images for the specific assemblies on the client machine at setup time. Also unlike traditional binaries, NGen images merely form a cache-managed applications will continue to run correctly even if all the NGen images are deleted. Of course, there will be a performance hit if that happens, but there will be no correctness issues.

NGen typically improves the warm startup time of applications, and sometimes the cold startup time as well. Cold startup time is primarily dominated by the number of pages that need to be fetched from disk. The improvement in cold startup time while using NGen can be attributed largely to the fact that pages of MSIL that need to be touched during compilation no longer need to be accessed at execution time.

Improvements in warm startup time come from reusing pages of the NGen images that were brought in when the application had been running earlier. This is especially beneficial to large client-side UI applications where startup time is critical to the user experience.

NGen also improves the overall memory usage of the system by allowing different processes that use the same assembly to share the corresponding NGen image among them. This can be very useful in both client and server scenarios in which the total memory footprint must be minimized.

No comments:

Post a Comment