Jit Inliningを服用する簡潔なコードスニペットを持っていますか?

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

  •  12-12-2019
  •  | 
  •  

質問

Jit Inliningを被るであろう "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 Inliningを被る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
.

RUN:

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