Jit Inliningを服用する簡潔なコードスニペットを持っていますか?
質問
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();
}
}
. 所属していません StackOverflow