If optimization is enabled, will JIT always embed this method?

I do not expect a definite yes or no. Any knowledge that you have, I will consider as the answer.

private String CalculateCharge(Nullable<Decimal> bill, Nullable<Decimal> rate) { return ((bill ?? 0.0m) * (rate ?? 0.0m)).ToString("C"); } 
+6
c # jit heuristics
source share
1 answer

An attachment is a detail of a JIT implementation, not a C # compiler. From Eric Gunnerson's Blog :

JIT uses several heuristics to decide if there should be a method in the lining. The following is a list of the more significant ones (note that this is not exhaustive):

  • Methods that exceed 32 IL bytes will not be included.
  • Virtual functions are not built-in.
  • Methods that have complex flow control will not be inserted. Complex flow control - any flow control other than if / then / else; in this case, switch or bye.
  • Methods containing exception handling blocks are not inlined, although methods that throw exceptions are still candidates for embedding.
  • If any of the formal arguments of a method is a structure, the method will not be inlined.

Although your method is quite short and not very complicated, therefore it may correspond to heuristics, Nullable<T> is a struct , so I would suggest that your method is not nested.

Typically, if embedding this method improves performance, JIT will inline this method; otherwise it will not be. But this is really a detail of the JIT implementation and nothing needs to be encoded for:

I would carefully consider explicitly coding these heuristics because they may change in future versions of JIT. Do not compromise the correctness of the method to try to ensure that it will be built-in.

EDIT: Apparently, the bit about structures not under construction is out of date; Updated information can be found on the Vance Morrison Blog .

+21
source share

All Articles