Welcome to MSDN Blogs Sign in | Join | Help

More on inlining...

Just came across this:

 

Method Inlining

There is a cost associated with method calls; arguments need to be pushed on the stack or stored in registers, the method prolog and epilog need to be executed and so on. The cost of these calls can be avoided for certain methods by simply moving the method body of the method being called into the body of the caller. This is called Method In-lining. The JIT uses a number of heuristics to decide whether a method should be in-lined. The following is a list of the more significant of those (note that this is not exhaustive):

  • Methods that are greater than 32 bytes of IL will not be inlined.
  • Virtual functions are not inlined.
  • 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.
  • Methods that contain exception-handling blocks are not inlined, though methods that throw exceptions are still candidates for inlining.
  • If any of the method's formal arguments are structs, the method will not be inlined.

I would carefully consider explicitly coding for these heuristics because they might change in future versions of the JIT. Don't compromise the correctness of the method to attempt to guarantee that it will be inlined. It is interesting to note that the inline and __inline keywords in C++ do not guarantee that the compiler will inline a method (though __forceinline does).

Property get and set methods are generally good candidates for inlining, since all they do is typically initialize private data members.

HINT   Don't compromise the correctness of a method in an attempt to guarantee inlining.

From Writing High Performance Managed Applications

Published Thursday, January 29, 2004 3:55 PM by ericgu
Filed under: ,

Comments

Thursday, January 29, 2004 8:20 PM by Jeff Key

# .NET method inlining

Friday, January 30, 2004 4:09 AM by Paul's Imaginary Friend

# CLR Inlining

Friday, January 30, 2004 6:27 AM by Jim Argeropoulos

# re: More on inlining...

You may also check out Rico's recent post on inlining http://weblogs.asp.net/ricom/archive/2004/01/14/58703.aspx
Monday, February 09, 2004 5:18 AM by moo

# re: More on inlining...

Why dont you inline small switch-case constructs?

Why was 32 bytes chosen as an IL size for inlining?

Are small loops not unrolled and inlined or is any loop just not inlined?

Why are struct parameters not inlined?

I get the impression that alot of code that could be inlined wont be.

Monday, February 09, 2004 1:50 PM by Eric

# re: More on inlining...

Moo, I'll see if I can get somebody on the runtime to address those concerns...
Monday, February 09, 2004 3:56 PM by Rico Mariani

# re: More on inlining...

I'm not on the JIT team so I can't really answer definitively. But the overarching theme here is that the JIT isn't like a regular compiler -- it has serious time and space constraints because it's running, after all, in the same process as the application it is jitting for. As a result of that certain heuristics were chosen to trade off compilation speed for code quality.

There's a certain irony here because you can imagine that in fact *better* compliation might be possible when JITing because we know all the details of the target machine, no guessing, and you could even imagine capturing information about what has happened so far, or what happened last time, to do a better job jitting. But all of these factors are competing against the clock... high speed compilation in the JIT is also very valuable.

So in the end there's a natural tension and you make choices like the 32 bytes of IL.

To get the history of why those particular choices were made, you'd want a JIT person to speak. My guess would be that they'd often say it's all a comprimise.

However you could imagine for instance doing a lot more heavy lifting when ngen'ing than when jitting. But then of course you get the other trade off that if the compiler does totally different things there might be different bugs, which would really be annoying...

But I digress...
Monday, February 09, 2004 5:17 PM by moo

# re: More on inlining...

Sure its a compromise but there is more than one Runtime out there for different architectures. Server and Workstation, the difference mainly being the garbage collector running on a different CPU right?

How do these different versions differ on the JIT?
Tuesday, June 01, 2004 1:26 PM by 仪表

# re: More on inlining...

runtime to address those concerns?it dosn't work!
Saturday, December 18, 2004 10:51 AM by MBA

# MBA

Helpful For MBA Fans.
Thursday, March 29, 2007 11:00 PM by robubu » Worlds Apart - JVMs and CLIs

# robubu » Worlds Apart - JVMs and CLIs

PingBack from http://robubu.com/?p=27

# robubu - the technical weblog of Rob Yates » Worlds Apart - C# and Java

PingBack from http://robubu.com/?p=28

Friday, September 14, 2007 5:32 AM by NeatCoding

# [Daily Issue] Spiegare il Method In-Lining

[Daily Issue] Spiegare il Method In-Lining

# Scott Hanselman's Computer Zen - Release IS NOT Debug: 64bit Optimizations and C# Method Inlining in Release Build Call Stacks

Thursday, October 25, 2007 4:48 PM by ASPInsiders

# Release IS NOT Debug: 64bit Optimizations and C# Method Inlining in Release Build Call Stacks

Just a reminder: Release builds are not Debug builds. Seems obvious, but it's worth saying again. Release

Tuesday, April 29, 2008 6:25 PM by <T>homas' Blog

# Debugging Release Builds

Vi builder altid vores release builds med /DEBUG:PDBONLY og /optimize+. Ved at benytte /DEBUG:PDBONLY

New Comments to this post are disabled
 
Page view tracker