3

This could be either for the .NET or Mono compilers.

I know that under certain conditions the compiler can inline functions (e.g. small, single call site, etc.) as an optimization. However, if the function is public, then it needs to have an external entry point.

In these cases, is duplicated code generated (public entry point which isn't used by the internal code since the internal code uses the inlined version), or does marking it as public prevent the compiler from doing such an optimization?

asked Sep 18, 2013 at 21:47
3
  • 2
    I'm quite sure public doesn't affect inlining in any way. And I'm really not sure what "external entry point" is supposed to mean. Commented Sep 18, 2013 at 22:22
  • Write out a small class, compile it once with public and once with private, and then compare the IL. That'll give you the definitive answer... Commented Sep 18, 2013 at 23:13
  • 2
    The IL is completely useless to answer any kind of optimization or performance question - always check the generated machine code from JIT. Commented Sep 19, 2013 at 8:00

1 Answer 1

4

I found this interesting article on C# inline methods.

Scope of a method doesn't appear to change when a function becomes an inline candidate. Here is a summary of that article.

How to determine what get’s inlined? The short answer is that you can’t. The MSDN article "Writing High-Performance Managed Applications : A Primer" gives the following guidance:

  1. Methods that are greater than 32 bytes of IL will not be inlined.
  2. Virtual functions are not inlined.
  3. Methods that have complex flow control will not be in-lined. Complex flow control is any flow control other than if/then/else; in this case, switch or while.
  4. Methods that contain exception-handling blocks are not inlined, though methods that throw exceptions are still candidates for inlining.
  5. If any of the method’s formal arguments are structs, the method will not be inlined.

There is no inline keyword in C#, but there is an inline compiler directive that was introduced in .NET 4.5 called MethodImplOptions.AggressiveInlining

It can be used like this.

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int Method2()
{
// ... Aggressive inlining.
return "one".Length + "two".Length + "three".Length +
 "four".Length + "five".Length + "six".Length +
 "seven".Length + "eight".Length + "nine".Length +
 "ten".Length;
}

You can't know for sure if the compiler will inline a function. This isn't feature C# developers have control over.

answered Sep 30, 2013 at 1:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.