Sub topic 10
Type Feedback: How the Engine Learns From Running Code
This chapter assumes you have read the earlier chapters on bytecode interpreters, hidden classes, and inline caches. You should also be comfortable with JavaScript's dynamic typing. An inline cache records the shapes it sees at one property access. Type feedback is the general version of that idea. It applies the same kind of recording to every operation whose behavior depends on the types it receives at run time.
Why the engine needs feedback
The expression a + b could mean integer addition, floating-point addition, string concatenation, BigInt addition, or a call to valueOf. The source text alone cannot tell the engine which one will happen. An ahead-of-time compiler would have to emit code that handles every case. V8 doesn't guess. It watches the code run and records what actually happens.
Feedback vectors
A feedback vector is a per-function array of slots. Each slot belongs to one operation in the function's bytecode that could benefit from feedback, such as a property load, a property store, a call, an arithmetic operation, or a comparison. When Ignition executes that bytecode, it updates the slot.
| Bytecode operation | What the slot records |
|---|---|
| Property load `o.x` | Hidden classes seen and where `x` lives in each |
| Binary operation `a + b` | A type hint such as SignedSmall, Number, String, or Any |
| Call `f()` | The target function, if it is always the same one |
| Comparison `a < b` | The kinds of operands compared |
V8 allocates feedback vectors lazily, so a function that runs only once or twice may never get one. This saves memory for the many functions that never become hot.
Feedback only gets more general
Each slot moves through a small set of states, called a lattice, and it only ever moves toward more general states. A binary operation slot starts with no feedback. It becomes SignedSmall when it sees small integers, Number after it sees a double, and Any after it sees mixed types. It never goes back to a more specific state.
function add(a, b) {
return a + b;
}
add(1, 2); // slot: SignedSmall (small integers)
add(1.5, 2); // slot widens to Number
add('a', 'b'); // slot widens to Any: strings mixed with numbers
add(3, 4); // slot stays Any; integers again do not narrow it
console.log(add(10, 20)); // 30The last call is the trap. Even if the program only ever adds integers from now on, this slot stays permanently general. The optimizing compiler reads it and has to emit generic addition code.
What happens with the feedback
When a function becomes hot, Maglev or TurboFan reads its feedback vector. A slot that says "SignedSmall" lets the compiler emit a single integer add instruction, guarded by a type check. A slot that has never executed carries no feedback. The compiler usually treats that path as unlikely and may leave it out entirely. The next chapter, on speculative optimization, covers how those guards work and what they cost.
Exercises
Exercise
A function scale(x) is called 50,000 times with small integers, once with null, then 50,000 more times with small integers. What does its arithmetic feedback slot most likely end up recording, and why?
Show answer
It records a more general hint than SignedSmall, such as NumberOrOddball, where null counts as an "oddball" value. Feedback only moves toward more general states, so the later integer calls cannot narrow it back.
Exercise
Why does V8 allocate feedback vectors lazily instead of creating one for every function when the function is created?
Show answer
Most functions run only a few times and never reach an optimizing tier, so allocating vectors for them would waste memory. Lazy allocation spends memory only on functions that run often enough for the feedback to be useful.