JIT 인라인을 발생시키는 간결한 코드 조각을 가질 수 있습니까?

StackOverflow https://stackoverflow.com//questions/12686850

  •  12-12-2019
  •  | 
  •  

문제

JIT 인라인이 발생하는 "Hello World" 크기의 C# 코드 조각을 생성하려고 합니다.지금까지 나는 이것을 가지고 있습니다 :

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine( GetAssembly().FullName );
        Console.ReadLine();
    }

    static Assembly GetAssembly()
    {
        return System.Reflection.Assembly.GetCallingAssembly();
    }
}

Visual Studio에서 "릴리스"-"모든 CPU" 및 "디버깅 없이 실행"으로 컴파일합니다.내 샘플 프로그램 어셈블리의 이름이 매우 명확하게 표시됩니다. GetAssembly() 인라인되지 않았습니다. Main(), 그렇지 않으면 표시됩니다 mscorlib 어셈블리 이름.

JIT 인라인이 발생하는 일부 C# 코드 조각을 작성하려면 어떻게 해야 하나요?

도움이 되었습니까?

해결책

물론입니다. 예를 들면 다음과 같습니다.

using System;

class Test
{
    static void Main()
    {
        CallThrow();
    }

    static void CallThrow()
    {
        Throw();
    }

    static void Throw()
    {
        // Add a condition to try to disuade the JIT
        // compiler from inlining *this* method. Could
        // do this with attributes...
        if (DateTime.Today.Year > 1000)
        {
            throw new Exception();
        }
    }
}

릴리스와 유사한 모드로 컴파일하십시오.

csc /o+ /debug- Test.cs

달리다:

c:\Users\Jon\Test>test

Unhandled Exception: System.Exception: Exception of type 'System.Exception' was
thrown.
   at Test.Throw()
   at Test.Main()

스택 추적을 참고하세요. 마치 Throw 직접 전화를 받았다. Main, 왜냐하면 코드는 CallThrow 인라인되었습니다.

다른 팁

인라인에 대한 이해가 잘못된 것 같습니다.만약에 GetAssembly 인라인되었더라도 여전히 프로그램 이름이 표시됩니다.

인라인은 다음을 의미합니다."함수 호출 위치에서 함수 본문을 사용하십시오."인라인 GetAssembly 다음과 같은 코드가 생성됩니다.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(System.Reflection.Assembly.GetCallingAssembly()
                                                    .FullName);
        Console.ReadLine();
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top