7

Is there an easy way of JIT-ing C# code up front, rather than waiting for the first time the code is invoked? I have read about NGEN but I don't think that's going to 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 (a) run before the event arrives, or (b) ever run again, so the cost of JIT is high in this scenario. Checking with ANTS profiler the overhead for JIT is around 40-50%, sometimes it's as high as 90%. My application is highly latency sensitive, and every millisecond counts.

My initial thought is that I could add a bool parameter to every critical path method, and call those methods before the event occurs, in order to initiate the JIT compile. However, is there a prettier and less hacky way?

Many thanks

11
  • 10
    Why wouldn't NGEN help you? Nov 1, 2011 at 23:58
  • 2
    Have you determined that it is a problem? Or do you merely think it might be? Nov 1, 2011 at 23:59
  • I'm (virtually) certain it's the problem Henk. I just implemented on a single method my hacky approach and the JIT overhead for that method in ANTS profiler has disappeared.
    – endian
    Nov 2, 2011 at 0:02
  • NGEN is the way to go. Just run it via your installer/script when you deploy and forget about any IL compilation overhead at runtime. Nov 2, 2011 at 0:04
  • 1
    Random question: is there any way that while you are waiting, you could send a spoof message through the system, just redirecting any side-effects? so you can prime the code without actually doing anything? just a thought Nov 2, 2011 at 0:17

2 Answers 2

7

I'd say use NGEN and if it doesn't work, you likely have deeper problems.

But, to answer your question, this article on how to pre-jit uses System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod to force a JIT. It includes sample code to use reflection to get the method handles.

1
3

What happens the second time the event arrives? Is it faster then or just as slow. If its still slow then JIT is not the problem, because the code gets "JIT"ed only once, the first time it is run.

NGEN would provide the answer for you. My suggestion is to take the bare minimum of the code you need, the critical path if you will, and put it in dummy/sandbox project. Start profiling/NGenning this code and seeing the performance.

If this bare minimum code, even after being NGEN'ed performs poorly on multiple calls, then pre-compiling isn't going to help you. Its something else in the code that is causing performance bottle necks.

1
  • The code is lightning fast after the first event. As I mentioned above, I've identified that it's definitely JIT overhead that's causing the performance issues. I am in the process of adding a load of calls to RuntimeHelpers.PrepareMethod, hopefully that will remove this issue.
    – endian
    Nov 2, 2011 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.