If optimizations are enabled will the JIT always inline this method?

后端 未结 1 1046
無奈伤痛
無奈伤痛 2021-02-09 07:37

I am not expecting a definite yes or no. Any knowledge you might have I will consider as an answer.

private String CalculateCharge(Nullable bill,          


        
1条回答
  •  情深已故
    2021-02-09 08:22

    Inlining is an implementation detail of the JIT, not of the C# compiler. From 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.

    Although your method is quite short and not very complex so it might match the heuristics, Nullable is a struct so I'd guess your method is not inlined.

    As a rule of thumb, if inlining this method improves performance, the JIT will inline this method; otherwise it will not. But this is really an implementation detail of the JIT and nothing you should code for:

    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.

    EDIT: Apparently the bit about structs not being inlined is out-of-date; updated information can be found at Vance Morrison's blog.

    0 讨论(0)
提交回复
热议问题