是静态方法热切编译(JIT'ed)? [英] Are static methods eagerly compiled (JIT'ed)?

查看:128
本文介绍了是静态方法热切编译(JIT'ed)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按我的理解,这两个实例方法和静态方法治疗由CLR编译器和IL code同样是即时编译每当方法被称为第一次。今天,我和我的同事讨论,他告诉我说,静态方法不及时治疗方式相同实例方法。即静态方法只要集加载到应用程序域,而实例方法被即时编译因为他们是所谓的第一次即时编译。

Per my understanding, both instance methods and static methods are treated same by CLR compiler and the IL code is JITted whenever the method is called first time. Today I had a discussion with my colleague and he told me that the static methods are not treated the same way as instance methods. i.e. Static methods are JITted as soon as the assembly is loaded into application domain whereas instance methods are JITted as they are called for the first time.

其实我一头雾水,不明白了一个道理,为什么静态方法应该由CLR急切地编译?我明白了有关对象或约束时,执行区用于静态构造函数或关键终结的释放方法。但是,如果一些类有静态和实例相结合的方法,我真的不知道为什么所有的静态方法会尽快包含类的程序集将被加载到内存中即时编译?

I am actually confused and do not see a reason as to why the static methods should be eagerly compiled by CLR? I understand about static constructors or finalizer methods of the Critical Finalizer Objects or when constrained execution regions are used. But if some class has a combination of static and instance methods, I am really not sure why all static methods would be JITted as soon as the assembly containing the class would be loaded into memory?

请帮我理解这种行为。

推荐答案

看着当get方法使用的WinDbg / SOS显示,静态方法在调用它们不会被编译JIT编译。

Looking at when the methods get JIT compiled using WinDbg/SOS shows that static methods are not compiled prior to calling them.

考虑下面的类:

class SomeType
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public void InstanceMethod()
    {
        Console.WriteLine("instance");
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static void TypeMethod()
    {
        Console.WriteLine("type");
    }
}

我用的是NoInlining选项prevent编译器内联这些方法在一个发布版本。

I use the NoInlining option to prevent the compiler from inlining these methods in a release build.

如果我运行类似下面的小应用程序,附加的WinDbg我能观察到,当这些方法得到JIT编译。

If I run a small app like below and attach the WinDbg I can observe when the methods get JIT compiled.

var st = new SomeType();

Console.WriteLine("attach");
Console.ReadLine();

Console.WriteLine("calling methods");
st.InstanceMethod();
SomeType.TypeMethod();

Console.ReadLine();

在附加表的方法为 SOMETYPE 的一点是这样的:

0:004> !dumpmt -md 0041387c
EEClass:         004114d4
Module:          00412e94
Name:            ConsoleApplication2.SomeType
mdToken:         02000007
File:                c:\temp\ConsoleApplication1\ConsoleApplication1\bin\Release\ConsoleApplication1.exe
BaseSize:        0xc
ComponentSize:   0x0
Slots in VTable: 7
Number of IFaces in IFaceMap: 0
--------------------------------------
MethodDesc Table
   Entry MethodDe    JIT Name
6d374960 6d076728 PreJIT System.Object.ToString()
6d368790 6d076730 PreJIT System.Object.Equals(System.Object)
6d368360 6d076750 PreJIT System.Object.GetHashCode()
6d3616f0 6d076764 PreJIT System.Object.Finalize()
0041c035 00413874   NONE ConsoleApplication2.SomeType..ctor()
0041c02d 0041385c   NONE ConsoleApplication2.SomeType.InstanceMethod()
0041c031 00413868   NONE ConsoleApplication2.SomeType.TypeMethod()

在该方法已被显式调用它看起来是这样的:

After the methods have been explicitly invoked it looks like this:

0:007> !dumpmt -md 0041387c
EEClass:         004114d4
Module:          00412e94
Name:            ConsoleApplication2.SomeType
mdToken:         02000007
File:            c:\temp\ConsoleApplication1\ConsoleApplication1\bin\Release\ConsoleApplication1.exe
BaseSize:        0xc
ComponentSize:   0x0
Slots in VTable: 7
Number of IFaces in IFaceMap: 0
--------------------------------------
MethodDesc Table
   Entry MethodDe    JIT Name
6d374960 6d076728 PreJIT System.Object.ToString()
6d368790 6d076730 PreJIT System.Object.Equals(System.Object)
6d368360 6d076750 PreJIT System.Object.GetHashCode()
6d3616f0 6d076764 PreJIT System.Object.Finalize()
0041c035 00413874   NONE ConsoleApplication2.SomeType..ctor()
004700e0 0041385c    JIT ConsoleApplication2.SomeType.InstanceMethod()
00470110 00413868    JIT ConsoleApplication2.SomeType.TypeMethod()

即。该方法不JIT编译直到实际调用它们。

I.e. the methods are not JIT compiled until they are actually invoked.

这篇关于是静态方法热切编译(JIT'ed)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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