C
C#2mo ago
cypherpotato

Why ins't inlining working here?

Just doing an experiment. The following code are not inlined by the compiler:
class Program
{
static void Main(string[] args)
{
int k = DoFoo(10, 20);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int DoFoo(int i, int b)
{
return i + b;
}
}
class Program
{
static void Main(string[] args)
{
int k = DoFoo(10, 20);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int DoFoo(int i, int b)
{
return i + b;
}
}
https://sharplab.io/#gist:692cc1af66c352c2e8138dad6f033959
SharpLab
C#/VB/F# compiler playground.
5 Replies
Aaron
Aaron2mo ago
inlining is done by the JIT not by the C# compiler
ACiDCA7
ACiDCA72mo ago
if you select jit asm, you see that it removes the mothod call and just returns
Jimmacle
Jimmacle2mo ago
also, this doesn't necessarily force the JIT to inline anything
ACiDCA7
ACiDCA72mo ago
if you eg put a console.writeline below where you use k you gonna see that it already passes the precalculated value
Austin Wise
Austin Wise2mo ago
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.
Want results from more Discord servers?
Add your server