Sub topic 14
Inside the Optimizing Compiler: Inlining and Escape Analysis
This chapter assumes you have read the chapters on type feedback, speculative optimization, and deoptimization. You should also be able to run Node.js scripts with V8 flags. Here we look at two things TurboFan, V8's top-tier optimizing compiler, does with the feedback it collects: inlining and escape analysis.
Inlining: Removing the Call
Inlining replaces a function call with a copy of the called function's body. A call has fixed costs. The engine sets up a stack frame, passes arguments, and jumps to the callee. Removing the call removes those costs. It also puts the caller's and callee's code into one unit that the compiler can optimize as a whole.
TurboFan picks inlining candidates from type feedback. If a call site has only ever called one function, the compiler can speculate that it will keep calling that function and paste the body in place. A guard checks that the target is still the same function. If the guard fails, the code deoptimizes, the same way a failed shape check does.
V8 limits inlining by the size of the bytecode and by a total budget for each optimized function. Small, hot functions get inlined. Large functions or deeply nested call chains often do not.
Escape Analysis: Removing the Allocation
An object escapes when code outside the current function could see it. That happens when you return the object, store it in a global or another object's property, or pass it to a call the compiler did not inline. Escape analysis is the compiler pass that proves an object does not escape.
When the proof succeeds, the compiler can skip allocating the object on the heap. It keeps the object's fields in registers or stack slots instead. This is called scalar replacement. It saves allocation time and gives the garbage collector less work.
The two techniques depend on each other. An object passed to a helper function escapes unless the compiler inlines that helper. After inlining, the compiler can see every use of the object, and the object often no longer escapes.
// Run with: node --trace-turbo-inlining inline.js
function square(n) {
return n * n;
}
function lengthSquared(x, y) {
const p = { x, y }; // temporary object
return square(p.x) + square(p.y);
}
let total = 0;
for (let i = 0; i < 1000000; i++) {
total += lengthSquared(i, i + 1);
}
console.log(total);The trace output should show square being inlined into lengthSquared once lengthSquared gets hot. The object p is never returned or stored, so escape analysis can remove its allocation. The optimized code then works on x and y directly. The exact trace format differs between V8 versions.
Common Ways These Optimizations Fail
| Pattern | Effect |
|---|---|
| Call site sees many different target functions | Feedback is megamorphic, so the call is not inlined |
| Very large callee | Exceeds the inlining size limit |
| Temporary object stored in an outer variable or array | The object escapes and must be allocated |
| Object passed to a function that was not inlined | The compiler must assume it escapes |
Exercise
In lengthSquared, suppose you add the line lastPoint = p; where lastPoint is a module-level variable. What happens to escape analysis for p, and why?
Show answer
p now escapes because code outside lengthSquared can reach it through lastPoint. The compiler can no longer apply scalar replacement, so it must allocate p on the heap on every call. Inlining square still works.
Exercise
Why can inlining a callee let escape analysis remove an object it would otherwise have to allocate?
Show answer
Before inlining, passing the object to the callee counts as an escape because the compiler cannot see what the callee does with it. After inlining, the callee's body is part of the same compilation unit. The compiler can then check every use of the object and prove that it never leaves the function.