Sub topic 12
Deoptimization: When the Bet Fails
This chapter assumes you have read the earlier chapters on type feedback and speculative optimization. You should know that V8's optimizing tiers (Maglev and TurboFan) compile a function using assumptions taken from its feedback vector. You should also be comfortable running Node.js with command-line flags.
What Deoptimization Is
Speculative optimization is a bet. The compiler sees that a + b has only ever received small integers. It then emits machine code that does integer addition and nothing else. To keep that code safe, it adds a guard in front of each assumption. A guard is a cheap check, such as "is this value a Smi?" or "does this object have map M?"
Deoptimization is what happens when a guard fails. The engine throws away the optimized frame and rebuilds the matching interpreter frame. Execution then continues in Ignition bytecode at the exact point where the guard failed. The program still computes the correct result. It just runs slower for a while.
To make this possible, the compiler stores deoptimization data alongside the optimized code. This data maps machine registers and stack slots back to interpreter registers. Values the optimizer never actually built, such as objects removed by escape analysis, are rebuilt from this data when needed.
Seeing It Happen
// Run with: node --trace-opt --trace-deopt add.js
function add(a, b) {
return a + b;
}
for (let i = 0; i < 1000000; i++) {
add(i, i); // feedback says: small integers
}
add('a', 'b'); // breaks the integer assumptionThe trace first shows add being marked and compiled. The string call then triggers a line that mentions a bailout or deoptimization, with a reason such as not a Smi. The exact wording differs between V8 versions. Look for the function name and the reason, not for a fixed format.
Kinds of Deoptimization
| Kind | Trigger | Example |
|---|---|---|
| Eager | A guard in the running optimized code fails | `add('a', 'b')` after integer-only feedback |
| Lazy | Something the code depended on changed elsewhere, so the code is invalidated and deoptimizes when control returns to it | Modifying a prototype that optimized code assumed was stable |
| Soft | The compiler reached code that had no feedback when it was compiled | A branch that never ran before optimization |
Common Mistakes
- Treating one deopt as a bug. A single deopt after warm-up is normal and cheap. V8 updates the feedback and can reoptimize with broader assumptions.
- Ignoring deopt loops. If a function keeps flipping between types, it can be optimized and deoptimized many times. V8 eventually stops trying, and the function stays in a slower tier.
- Feeding mixed types to hot code. Passing
undefinedor a string where a number usually goes, or objects with several shapes, widens the feedback and can trigger deopts.
Exercise
A hot function area(shape) is optimized after seeing only objects built by new Rect(w, h). Later, one call passes { h: 2, w: 3 }. Explain what happens and whether it is eager or lazy deoptimization.
Show answer
The literal adds h before w, so it has a different hidden class from Rect instances. The map check guarding the property access fails while the optimized code is running, which is an eager deoptimization. V8 rebuilds the interpreter frame, finishes the call in Ignition, and records the new map in the feedback. If the function stays hot, it may be reoptimized with polymorphic access.
Exercise
Why can't optimized code simply skip the guard checks to run faster?
Show answer
Without the guards, the specialized machine code would run on values it was not built for and give wrong results or crash. The guards keep the optimization correct, and deoptimization gives the engine a safe way out when they fail.