Photo by Florian Olivo on Unsplash
JIT Inline Heuristics: When Compilers Decide Not to Inline
Function inlining is one of the most impactful optimizations a JIT compiler can perform. By replacing a function call with the function’s body, the compiler eliminates call overhead and unlocks further optimization opportunities. But inlining comes with real costs, and JIT compilers need sophisticated heuristics to decide when to inline and when to hold back.
The Case for Inlining
Inlining eliminates the mechanical cost of function calls: pushing arguments onto the stack, jumping to a new instruction address, executing the function prologue, then reversing the process on return. For small, frequently called functions, this overhead can dominate execution time.
More importantly, inlining exposes optimization opportunities that cross function boundaries. Once the callee’s code is embedded in the caller, the optimizer can perform constant propagation, dead code elimination, and register allocation across what were previously separate compilation units. A function that checks a constant flag can be inlined and then optimized away entirely if the flag is known at the call site.
Why Compilers Resist Inlining Everything
The naive approach would be to inline aggressively, but JIT compilers have good reasons to be selective. Code size grows with every inline decision. If a 50-byte function is called from ten places and all those calls are inlined, the compiled code expands by 500 bytes. This bloat has cascading effects.
Larger compiled methods consume more instruction cache. When hot code no longer fits in the L1 instruction cache, the CPU must fetch instructions from slower cache levels or main memory. The performance win from eliminating a call can be erased by instruction cache misses.
Compilation time matters in a JIT context. Unlike ahead-of-time compilers that can afford expensive optimization passes, JIT compilers operate with a limited time budget. Every byte of code that needs optimization, register allocation, and instruction selection adds to compilation latency. If a method balloons from inlining, the JIT may hit its time budget and bail out to a less optimized tier, or the application may stall waiting for compilation to finish.
Larger methods also stress the register allocator. With more code in a single method, the allocator has more live variables to juggle across a longer stretch of instructions. If the allocator runs out of registers, it spills variables to the stack, adding memory traffic that can negate inlining benefits.
Heuristics in Practice
JIT compilers use multi-factor heuristics to guide inline decisions. Common factors include the size of the callee, the depth of the inline chain, the frequency of the call site, and the type profile.
Size thresholds are the most straightforward. A function under a certain bytecode or IR size is a strong candidate for inlining, especially if it’s called from a hot loop. Many JITs set different thresholds depending on optimization tier: aggressive inlining at higher tiers, conservative at lower tiers.
Call site frequency matters. A method called once per request might not justify inlining, but a method called in an inner loop processing millions of elements almost certainly does. Profiling data guides this: the JIT observes which call sites are hot and prioritizes those for inlining.
Inline depth limits prevent runaway expansion. If function A inlines B, and B would inline C, and C would inline D, the compiler stops at some depth to avoid exponential code growth. Typical limits are in the range of three to seven levels.
Type profiling adds another dimension. In dynamically typed or polymorphic code, a call site might dispatch to different implementations. If profiling shows a call site is monomorphic—always calling the same method—the JIT can inline and speculate. If the site is polymorphic, the JIT may inline the common case and emit a guard that falls back to a generic path if a different type appears.
Balancing Act
The tension is between local and global optimization. Inlining improves local code quality but degrades global characteristics like instruction cache utilization and compilation cost. The best heuristics are adaptive: they tune aggressiveness based on available time, code size budgets, and observed performance.
Modern JITs continuously refine their heuristics, sometimes recompiling methods with different inline decisions if performance counters suggest the initial choice was suboptimal. This feedback loop turns inlining from a one-time decision into a dynamic optimization that evolves as the program runs.
The result is a complex balancing act where the compiler weighs immediate gains against systemic costs, guided by heuristics tuned over years of production workloads. When those heuristics work well, the application runs fast. When they misfire, a seemingly small function call can become a hidden performance cliff.