简体   繁体   English

避免C#JIT开销

[英]Avoiding C# JIT overhead

Is there an easy way of JIT-ing C# code up front, rather than waiting for the first time the code is invoked? 是否有一种简单的方法可以预先JIT编写C#代码,而不是等待第一次调用代码? I have read about NGEN but I don't think that's going to help me. 我读过NGEN,但我认为这不会对我有所帮助。

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. 我的应用程序等待并响应来自UDP端口的特定外部事件,并且没有关键路径代码(a)在事件到达之前运行,或者(b)再次运行,因此JIT的成本很高在这种情况下。 Checking with ANTS profiler the overhead for JIT is around 40-50%, sometimes it's as high as 90%. 使用ANTS分析器检查JIT的开销约为40-50%,有时甚至高达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. 我最初的想法是,我可以为每个关键路径方法添加一个bool参数,并在事件发生之前调用这些方法,以便启动JIT编译。 However, is there a prettier and less hacky way? 但是,是否有更漂亮,更少hacky的方式?

Many thanks 非常感谢

I'd say use NGEN and if it doesn't work, you likely have deeper problems. 我会说使用NGEN,如果它不起作用,你可能会有更深层次的问题。

But, to answer your question, this article on how to pre-jit uses System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod to force a JIT. 但是,要回答你的问题,这篇关于如何预jit的文章使用System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod来强制一个JIT。 It includes sample code to use reflection to get the method handles. 它包含使用反射来获取方法句柄的示例代码。

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. 如果它仍然很慢,那么JIT不是问题,因为代码在第一次运行时只被“JIT”编辑一次。

NGEN would provide the answer for you. NGEN会为您提供答案。 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. 开始分析/ NGenning此代码并查看性能。

If this bare minimum code, even after being NGEN'ed performs poorly on multiple calls, then pre-compiling isn't going to help you. 如果这个极少的代码,即使在NGEN'ed多次调用后表现不佳,那么预编译也无法帮助你。 Its something else in the code that is causing performance bottle necks. 它在代码中的其他东西导致性能瓶颈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM