2

My question is why does the C# compiler not allow inlining of the C# MSIL functions. I'm aware the JIT will inline the actual X86 assembly in some cases but I'm asking about the actual MSIL "assembly" code.

Why does the C# compiler not offer these types of optimisation?

Is it because there would be minimal to no gain? Or it simply has never been implemented?

5
  • stackoverflow.com/questions/20955717/inline-msil-cil Commented Jan 7, 2018 at 1:22
  • Hi Slai, that is not the same thing. I'm asking why the C# compiler doesn't automatically do this for us, not how to do it manually. Thanks though. Commented Jan 7, 2018 at 1:25
  • Why is this being downvoted? There is a single question which could have a concise answer of "It wasn't done because of x", suggest an edit or somewhere better to ask the question if you don't believe it is suitable. Commented Jan 7, 2018 at 1:45
  • 1
    Here is a question for you: what will be the benefit of that if the function is never used at runtime? Commented Jan 7, 2018 at 2:25
  • There would be no benefit, there are obviously pros and cons to function inlining. If its used thousands of times it would increase your code size, but if it can also improve performance in C++ programs in many cases. Commented Jan 7, 2018 at 3:47

1 Answer 1

5

The responses to a similar question for the Java compiler's optimizations when translating to JVM bytecode seem to be applicable. A compiler from a high-level language (C# or Java) to an intermediate language (CIL/MSIL or JVM bytecode) might not want to optimize its emitted code because:

Eric Lippert's blog post on the C# compiler's /optimize flag supports the notion that the compiler prefers to do less optimization, leaving it for the .NET JIT:

These are very straightforward optimizations; there’s no inlining of IL, no loop unrolling, no interprocedural analysis whatsoever. We let the jitter team worry about optimizing the heck out of the code when it is actually spit into machine code; that’s the place where you can get real wins.

Sign up to request clarification or add additional context in comments.

4 Comments

Related: the clang front-end for LLVM (ahead-of-time compiler for C and C++) can optionally do some optimization of the LLVM-IR before feeding it to the LLVM back-end. But mostly it's the LLVM back-end that does the heavy lifting optimization for the target CPU architecture.
Would you say this answer is still accurate in 2022? c python released recently showed there was large gains in optimising the interpreter (which I would argue is in the same space as optimising the interpreted language itself)
@rollsch Given the decisions on how .NET chooses to do its optimizations have already been set for about 20 years, I don't think new data about Python would be likely to introduce changes.
"It's always been this way" isn't really an answer. Things that have been static for long periods of time get changed all the time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.