以下代码(由 Vitaliy Liptchinsky 编写)遍历程序集中的所有类型并在所有方法上调用 PrepareMethod。这会改善冷启动延迟吗?
Thread jitter = new Thread(() =>
{
foreach (var type in Assembly.Load("MyHavyAssembly, Version=1.8.2008.8," +
" Culture=neutral, PublicKeyToken=8744b20f8da049e3").GetTypes())
{
foreach (var method in type.GetMethods(BindingFlags.DeclaredOnly |
BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance |
BindingFlags.Static))
{
if (method.IsAbstract || method.ContainsGenericParameters)
continue;
System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod(method.MethodHandle);
}
}
});
jitter.Priority = ThreadPriority.Lowest;
jitter.Start();