Why ins't inlining working here?
Just doing an experiment. The following code are not inlined by the compiler:
https://sharplab.io/#gist:692cc1af66c352c2e8138dad6f033959
SharpLab
C#/VB/F# compiler playground.
5 Replies
inlining is done by the JIT
not by the C# compiler
if you select jit asm, you see that it removes the mothod call and just returns
also, this doesn't necessarily force the JIT to inline anything
if you eg put a console.writeline below where you use k you gonna see that it already passes the precalculated value
As others have mentioned, if you change the program to use the value returned by the function (to prevent dead code elimination) and view the JIT Asm, you can see the inlining take effect. Here is a modified version of your program that does that:
https://sharplab.io/#v2:C4LghgzgtgPgAgJgIwFgBQcAMACOSAsA3OlrkgHQBKArgHbACWUApuQMID2UADgwDbMATgGUhANwYBjZhGJoSCMgHZ0Ab3TZNZAGzYG9bAFkw+gBR5MAbQC62MIIDmEAJQat6tFq+4l2ACIcAGIcHKZImAA0do4Q5AAyzLQOwAAWznJeAL5umjnYlobMqRwAJgCSPHymhcXllQDy3IwctLEAgg4OgjIQDGLMZbR8+voOztZ5eLr6wP5BIaYzelFLAEaunu55XnC+DNgA1NirGVrZaJlAA===
SharpLab
C#/VB/F# compiler playground.