运行为第一时间的方法时.NET推出代码非常缓慢。如何修复withough NGEN? [英] .NET Release code very slow when running a method for the first time. How to fix it withough NGen?

查看:226
本文介绍了运行为第一时间的方法时.NET推出代码非常缓慢。如何修复withough NGEN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经部署在释放模式(64)我预想的要快一个应用程序,我注意到,有每当一个新的方法,或者是第一次执行一套方法严重放缓。
我说的严重的我的意思是100-200毫秒执行第一时间和它之后需要远低于1毫秒。

I have deployed a application in release mode (x64) which I expected to be fast, and I noticed that there is a severe slowdown whenever a new method, or set of methods is executed for the first time. When I say severe I mean 100-200 ms to execute the first time and it takes well under 1 ms after that.

这是我发现了什么,这似乎是由于这应该是编译的方法他们第一次运行时的JIT编译器。我期望从这个有一些延迟,但100毫秒是在执行过程中一个灾难性的延迟。

From what I found, this seems to be due to the JIT compiler which is supposed to compile methods the first time they are run. I expected some delay from this, but 100 ms is a disastrous delay in the middle of execution.

我知道NGEN,但NGEN需要做在安装时间机器。机器这是所有有限的用户权限而无法安装任何东西。该应用程序部署为一个可执行文件和参考的DLL。我想这就是为什么我永远无法得到NGEN的工作。

I know about NGen, but NGen needs to be done at install time on the machine. The machines this is for all have limited user rights and cannot install anything. The app is deployed as an executable and reference DLLs. I think that is why I could never get NGen to work.

有没有什么办法,使JIT编译每个方法的启动?

Is there any way to make the JIT to compile every method at start up?

我虽然创建虚拟变量并添加启动程序,会做什么,但每运行一次的方法,因此可当开机完成什么。请问这是否足以迫使编译或做方法的每一个代码路径需要单独执行。

I though of creating dummy variables and adding a start-up routine that would do nothing but run every method once so it can be what when start-up completes. Would that be sufficient to force a compile or does every code path of a method need to be executed separately.

推荐答案

哇。这种延迟是不寻常的JIT。简介您的应用程序的确定的瓶颈是JIT。

Wow. Such delays are unusual for the JIT. Profile your app to make sure the bottleneck is the JIT.

现在,如果这是真的JIT,这里有一个比添加一个更好的方法哑参无处不在:

Now, if it's really the JIT, here's a much better method than adding a dummy argument everywhere:

使用的 RuntimeHelpers.PrepareMethod 每个非泛型方法。该功能将迫使JIT处理它。

Use RuntimeHelpers.PrepareMethod on each non-generic method. This function will force the JIT to process it.

您还可以使用的 RunClassConstructor 方法上的每个类.. 。好了,运行他们的静态构造函数

You can also use RunClassConstructor method on each class to... well, run their static constructors.

下面是一些(完全未经测试)代码:

Here's some (totally untested) code:

foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
    if (type.IsGenericTypeDefinition || type.IsInterface)
        continue;

    RuntimeHelpers.RunClassConstructor(type.TypeHandle);

    foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly))
        RuntimeHelpers.PrepareMethod(method.MethodHandle);
}

这篇关于运行为第一时间的方法时.NET推出代码非常缓慢。如何修复withough NGEN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆