Avoiding C # JIT Overhead

Is there an easy way for C # JIT code to come, rather than waiting for the code to be called the first time? I read about NGEN, but I don't think this will help me.

My application waits and responds to a specific external event that comes from a UDP port, and none of the critical path code is executed (a) before the event occurs, or (b) someday it will start again, so the cost JIT scripting is high. Checking with the ANTS profiler the overhead for JIT is about 40-50%, sometimes it reaches 90%. My application is delay sensitive and every millisecond counts.

My initial thought is that I can add a bool parameter for each method of the critical path and call these methods before an event occurs to trigger a JIT compilation. However, is there a more beautiful and less hacker way?

Many thanks

+7
source share
2 answers

I would say using NGEN, and if that doesn't work, you are likely to have deeper problems.

But to answer your question, this article is about how pre-jit uses System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod to force JIT. It includes sample code for using reflection to obtain method handles.

+5
source

What happens the second time an event arrives? It is faster or just as slow. If it is still slow, then JIT is not a problem, because the code receives "JIT" only once, the first time it is run.

NGEN will give you the answer. My suggestion is to take the minimum required code, the critical path, if you like, and put it in the dummy / sandbox project. Start profiling / ngenning this code and see the performance.

If this minimal code, even after NGEN'ed works poorly on multiple calls, then pre-compilation will not help you. Its something else in the code causing the neck of the bottle of performance.

+3
source

All Articles