34

Possible Duplicate:
Inline functions in C#?

In c++ we can force function inlining.

Is this also possible in c#? sometimes, and when the method is small, it gets inlined automatically. But is it possible force inlining functions in c#/.Net?

Community
  • 1
  • 1
Chris
  • 4,325
  • 11
  • 51
  • 70
  • As mentioned, duplicate. Also, don't. The JIT compiler is really good at this and has a really good algorithm. You'll get better performance by leaving it alone. – marr75 Sep 06 '12 at 15:59
  • 3
    @marr75 (As of .net 4.0) The inlining algorithm is pretty dumb. It quite often makes bad decisions. Being able to manually override them would be quite nice. – CodesInChaos Sep 06 '12 at 16:02
  • @CodesInChaos Agree to disagree, has always worked well for me. Corner cases more often require datastructure changes than microoptimizations in most of my uses. – marr75 Sep 06 '12 at 16:06
  • Some versions of that algorithm did totally suck. I can't find the more modern literature I remember reading last year. If I find it I'll post it. – marr75 Sep 06 '12 at 16:08
  • Real quick guys, does anyone know a way to force the opposite? (i.e. for a function to NOT be inlined?) – BrainSlugs83 Mar 06 '14 at 20:09
  • 4
    @Eregrith Ironically googling "C# request inline" now brings this as the top answer – Pharap Aug 24 '15 at 16:10
  • 3
    Here's a use-case for you: if you inline code that checks a license or something like that then any hacker now has a lot more code to chop out than simply the one method everything calls. – CAD bloke May 03 '17 at 23:29

1 Answers1

79

Sort of. It's not under your direct control to turn on for sure. It's never inlined in the IL - it's only done by the JIT.

You can explicitly force a method to not be inlined using MethodImplAttribute

[MethodImpl(MethodImplOptions.NoInlining)]
public void Foo() { ... }

You can also sort of "request" inlining as of .NET 4.5:

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Foo() { ... }

... but you can't force it. (Prior to .NET 4.5, that enum value didn't exist. See the .NET 4 documentation, for example.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    Nice, didn't know about the new enum value. That could be useful in some of my high performance code. – CodesInChaos Sep 06 '12 at 16:03
  • Why does it not offer forced inlining and only offers suggestive / requests? – WDUK Jan 06 '18 at 06:18
  • @WDUK because it cannot offer to force inlining. In no language I know can inlining be **forced**; e.g. how do you think should a recursive function be inlined? Therefore, in C#, C++ etc. the most the programmer can do is to give a **hint** to the compiler that this method should probably inlined, **if possible**. – Thomas Flinkow Mar 07 '18 at 13:29
  • 8
    @ThomasFlinkow i would have thought if you request a forceful inline, it will either simply fail to compile or produce the wrong desired result, and that would be on the developer to decide when to and when not to force it. – WDUK Mar 07 '18 at 20:49