Sub topic 11
Speculative Optimization: Betting on Past Behavior
This chapter assumes you already know how Ignition runs bytecode, how the tiers above it (Sparkplug, Maglev, TurboFan) are arranged, and how feedback vectors record the types and hidden classes each operation has seen. Here you will see what the optimizing compilers do with that feedback.
The core idea
JavaScript gives the compiler almost no guarantees. In a + b, the operands could be numbers, strings, objects with valueOf, or BigInts. Code that handles every case has to branch on types at every step, and that is slow.
Speculative optimization works around this. The compiler reads the feedback vector and assumes the future will look like the past. If a + b has only ever seen small integers (Smis), the compiler emits a plain machine integer addition. In front of it, the compiler places a cheap guard, which is a check that the assumption still holds.
The compiled code ends up with two parts:
- Guards check the assumptions: "is
aa Smi?" or "doesostill have hidden class M1?" - Specialized code runs once the guards pass. It is written as if the assumptions were facts: an integer add with an overflow check, or a load from a fixed offset inside the object.
Speculation on property access
The same approach makes o.x fast. Suppose the inline cache for o.x is monomorphic, meaning it has seen one hidden class, and that class stores x at offset 12. TurboFan then emits two steps:
- Compare
o's map pointer with the expected map. - Load the value at offset 12.
No dictionary lookup and no prototype walk is needed. With polymorphic feedback, the compiler emits a short chain of map checks, one per known shape. With megamorphic feedback, it usually gives up on specializing and calls a generic lookup.
Watching it happen
// Run with: node --trace-opt speculate.js
function scale(point, factor) {
return point.x * factor + point.y * factor;
}
let sum = 0;
for (let i = 0; i < 1000000; i++) {
sum += scale({ x: i, y: i + 1 }, 2);
}
console.log(sum);The trace prints lines that mention scale being marked and then compiled by Maglev or TurboFan. Every call passed objects of the same shape with Smi values, so the compiler specialized scale for that shape and for integer arithmetic.
Why the bet usually pays
Real programs are mostly stable in their types. A function written to handle points almost always receives points. A guard costs a compare and a branch that the CPU predicts well, while generic code costs type dispatch at every operation. Speculation also opens the door to further optimizations, such as inlining and escape analysis, which depend on knowing concrete types.
Common errors in reasoning
| Misconception | Reality |
|---|---|
| Optimized code skips all type checks | It keeps guards. They are just cheap and few. |
| Once optimized, a function stays optimized | A failed guard throws the optimized code away. The next chapter covers this. |
| More type variety only costs a little | Mixed feedback weakens or blocks specialization. |
Exercise
A function area(s) returns s.w * s.h. It has been called with objects from two different constructors, and w sits at a different offset in each. What does the optimizing compiler most likely emit for s.w?
Show answer
A polymorphic sequence. It checks s's map against each of the two known maps and loads from the matching offset. If neither map matches, it deoptimizes.
Exercise
Why does speculative optimization depend on code first running in a lower tier?
Show answer
The lower tiers fill the feedback vectors. Without recorded types and shapes, the compiler has nothing to base its assumptions on.