避免 C# JIT 开销
是否有一种简单的方法可以预先对 C# 代码进行 JIT,而不是等待第一次调用代码?我读过有关 NGEN 的内容,但我认为这对我没有帮助。
我的应用程序等待并响应来自 UDP 端口的特定外部事件,并且没有任何关键路径代码 (a) 在事件到达之前运行,或 (b) 再次运行,因此 JIT 的成本很高在这种情况下。使用 ANTS Profiler 检查 JIT 的开销约为 40-50%,有时高达 90%。我的应用程序对延迟高度敏感,每一毫秒都很重要。
我最初的想法是,我可以向每个关键路径方法添加一个 bool 参数,并在事件发生之前调用这些方法,以便启动 JIT 编译。然而,有没有一种更漂亮、更不那么老套的方式呢?
非常感谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用 NGEN,如果它不起作用,您可能会遇到更深层次的问题。
但是,为了回答您的问题,这篇关于如何 pre-jit 的文章使用 < code>System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod 强制执行 JIT。它包括使用反射来获取方法句柄的示例代码。
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.事件第二次到达时会发生什么?是更快还是同样慢。如果它仍然很慢,那么 JIT 不是问题,因为代码只在第一次运行时进行“JIT”一次。
NGEN将为您提供答案。我的建议是采用您需要的最少代码(如果愿意的话,关键路径)并将其放入虚拟/沙箱项目中。开始分析/生成此代码并查看性能。
如果这个最低限度的代码,即使在经过 NGEN 处理后在多次调用中也表现不佳,那么预编译不会对您有帮助。代码中的其他内容导致了性能瓶颈。
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.