内联在这里没有帮助
你现在的代码是
void Log(string message, LogLevel logLevel) {
if (logLevel >= chosenLogLevel)
Console.WriteLine(message);
}
void Foo()
{
Log(SlowFunction(), someLogLevel);
}
内联后变为:
void Foo()
{
var message = SlowFunction();
var logLevel = someLogLevel;
if (logLevel >= chosenLogLevel)
Console.WriteLine(message);
}
logLevel
可以进一步内联,但根据定义,必须在检查之前调用SlowFunction()
。
避免这种情况的一种方法是将message
传递为Func<string>
:
void Log(Func<string> message, LogLevel logLevel) {
if (logLevel >= chosenLogLevel)
Console.WriteLine(message());
}
void Foo()
{
Log(SlowFunction, someLogLevel);
}
标签:AggressiveInlining,Console,Log,logLevel,void,SlowFunction,昂贵,message,MethodImplO
From: https://www.cnblogs.com/chinasoft/p/16986281.html