gpt4 book ai didi

c# - 如果启用优化,JIT 是否会始终内联此方法?

转载 作者:太空狗 更新时间:2023-10-29 20:48:04 25 4
gpt4 key购买 nike

我不期待明确的是或否。您可能拥有的任何知识我都会考虑作为答案。

private String CalculateCharge(Nullable<Decimal> bill, Nullable<Decimal> rate)
{
return ((bill ?? 0.0m) * (rate ?? 0.0m)).ToString("C");
}

最佳答案

内联是 JIT 的实现细节,而不是 C# 编译器的实现细节。来自 Eric Gunnerson's blog :

The JIT uses a number of heuristics to decide whether a method should be in-lined. The following is a list of the more significant of those (note that this is not exhaustive):

  • Methods that are greater than 32 bytes of IL will not be inlined.
  • Virtual functions are not inlined.
  • Methods that have complex flow control will not be in-lined. Complex flow control is any flow control other than if/then/else; in this case, switch or while.
  • Methods that contain exception-handling blocks are not inlined, though methods that throw exceptions are still candidates for inlining.
  • If any of the method's formal arguments are structs, the method will not be inlined.

尽管您的方法很短而且不是很复杂,所以它可能符合启发式,Nullable<T>struct所以我猜你的方法没有内联。

根据经验,如果内联此方法可以提高性能,则 JIT 将内联此方法;否则不会。但这实际上是 JIT 的一个实现细节,您不应该为此编写代码:

I would carefully consider explicitly coding for these heuristics because they might change in future versions of the JIT. Don't compromise the correctness of the method to attempt to guarantee that it will be inlined.

编辑: 显然关于不内联结构的部分已经过时;更新信息可在 Vance Morrison's blog 找到.

关于c# - 如果启用优化,JIT 是否会始终内联此方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1343019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com