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

Comments (16)

  1. Jim Argeropoulos says:

    You may also check out Rico’s recent post on inlining http://weblogs.asp.net/ricom/archive/2004/01/14/58703.aspx

  2. moo says:

    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.

  3. Eric says:

    Moo, I’ll see if I can get somebody on the runtime to address those concerns…

  4. Rico Mariani says:

    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…

  5. moo says:

    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?

  6. 仪表 says:

    runtime to address those concerns?it dosn’t work!

  7. MBA says:

    Helpful For MBA Fans.

  8. NeatCoding says:

    [Daily Issue] Spiegare il Method In-Lining

  9. ASPInsiders says:

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

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