Sub topic 10
Type Feedback: How the Engine Learns From Running Code
Earlier chapters showed that inline caches remember which hidden classes reached a property access, and that tiered compilation moves hot functions into faster tiers. This chapter covers the part that connects them: type feedback. Type feedback is the record of what actually happened while code ran: which shapes appeared, which numbers were added, which functions were called. The optimizing tiers rely on this record when they decide what code to generate.
This chapter assumes you know what bytecode and the Ignition interpreter are, how hidden classes and inline caches work, and how to run Node.js with command-line flags.
Why an Engine Needs Feedback at All
Source code alone tells a JavaScript engine very little. In a + b, the + could be an integer addition, a floating-point addition, string concatenation, BigInt arithmetic, or a call to valueOf on an object. A static compiler would have to generate code for all of these cases. A JIT compiler can do better because it compiles later, after the program has already run for a while. If a + b has only ever seen small integers, the compiler can generate one integer add plus a check that the inputs really are integers.
Collecting that knowledge has to cost very little, because every unoptimized function pays for it. V8 handles this with a compact per-function data structure that the interpreter and baseline code update as they execute.
Feedback Vectors and Feedback Slots
When V8 compiles a function to bytecode, it also builds a description of every operation in that function that could benefit from feedback. Each such operation gets a numbered feedback slot. At run time, the slots live in an array called the feedback vector, which is attached to the function.
You can see the slot numbers in Ignition's bytecode:
// Run with: node --print-bytecode --print-bytecode-filter=add feedback.js
function add(a, b) {
return a + b;
}
for (let i = 0; i < 10; i++) {
add(i, i);
}The relevant part of the output looks like this (exact formatting varies between V8 versions):
Ldar a1
Add a0, [0]
ReturnAdd a0, [0] means "add argument a0 to the accumulator, and record what you saw in feedback slot 0." Property loads, stores, calls, and comparisons carry slot indices in the same way:
// Run with: node --print-bytecode --print-bytecode-filter=area feedback2.js
function area(rect) {
return rect.width * rect.height;
}
for (let i = 0; i < 10; i++) {
area({ width: i, height: 2 });
} GetNamedProperty a0, [0], [0]
Star0
GetNamedProperty a0, [1], [2]
Mul r0, [4]
ReturnEach GetNamedProperty has its own load slot, and Mul has an arithmetic slot. In these instructions the first bracketed operand is a constant-pool index for the property name, and the last one is the feedback slot. The inline caches from the previous chapters keep their state in exactly these load and store slots.
What Each Kind of Slot Records
Different operations record different things.
| Operation | Example bytecode | What the slot records | How the optimizer uses it |
|---|---|---|---|
| Property load or store | `GetNamedProperty`, `SetNamedProperty` | Hidden classes seen, with a handler per class (the inline cache) | Emit a map check plus a direct offset load |
| Keyed access | `GetKeyedProperty` | Maps and elements kinds seen, or "megamorphic" | Specialize array indexing for one elements kind |
| Arithmetic | `Add`, `Mul`, `Sub` | A hint such as SignedSmall, Number, String, BigInt, or Any | Use integer or float machine instructions |
| Comparison | `TestLessThan`, `TestEqual` | A hint such as SignedSmall, Number, String, or Any | Emit a direct numeric or pointer comparison |
| Call | `CallProperty`, `CallUndefinedReceiver` | The target function, if there was only one, and a call count | Decide whether to inline the callee |
| Literal creation | `CreateArrayLiteral` | An allocation site that tracks elements kind transitions | Pre-allocate arrays in the right elements kind |
Call counts are easy to overlook. If a call site has always invoked the same function and has done so many times, the optimizer can treat it as a strong candidate for inlining. A call site with many different targets loses that option.
Feedback Only Moves Toward More General States
Every slot follows a lattice: an ordered set of states where an update can only move a slot toward a more general state, never back toward a narrower one. Here is a simplified version of V8's arithmetic hint lattice:
"SignedSmall" means small integers that fit V8's Smi representation. "Oddball" covers values such as undefined, null, true, and false. Once a single call to add passes a floating-point value, the slot moves to Number. It stays there even if every later call uses integers.
Monotonic updates keep feedback cheap. Each update is a single merge with the current state, so the engine never has to store history. They also prevent oscillation: without this rule, an operation that alternated between two types could flip back and forth and trigger repeated recompilation. The cost is that one unusual value can permanently widen the feedback for that slot.
When Feedback Exists
V8 does not allocate a feedback vector the moment a function is created. Many functions run once or never, so V8 uses lazy feedback allocation. A function starts with a small placeholder and gets a full feedback vector only after it has used up some of its execution budget. Until then, its operations run without recording anything.
This matters when you interpret experiments. A function called two or three times may have no feedback at all, and therefore nothing an optimizing tier could use. That is one reason the examples in this tutorial run loops with thousands of iterations.
Closures created from the same function literal generally share one feedback vector through a shared feedback cell. Ten thousand (x) => x.id callbacks created by the same enclosing code therefore build up feedback together. This normally helps, but it also means that a callback reused with different object shapes pollutes the feedback for every copy.
Edge Cases That Distort Feedback
Feedback describes the past, so anything unusual in the past ends up in it.
- Warm-up pollution. Startup code, test fixtures, or configuration parsing may call a hot function with odd values such as
null, strings, or objects with extra properties. Those calls can widen the slots before the steady-state workload even starts. - Rare branches. A slot inside a branch that has never run stays at None. The optimizer has nothing to specialize on there, and V8 typically compiles such a branch as an exit back to the interpreter. The next chapters cover what happens when that exit is taken.
- Polymorphic data. Code that processes a mix of record types honestly sees several shapes. In that case the feedback is accurate, and the fix, if one is needed, is to change the data layout.
- Feedback can be discarded. Under memory pressure, V8 can flush bytecode for functions that have not run for a long time, and their feedback goes with it. A function that becomes hot again starts learning from scratch.
function scale(v, factor) {
return v * factor;
}
// Warm-up: one call with a non-integer factor.
scale(10, 0.5);
// Steady state: integers only.
let total = 0;
for (let i = 0; i < 1000000; i++) {
total += scale(i, 2);
}
console.log(total);
// The Mul slot in scale already recorded Number during warm-up,
// so optimized code uses float multiplication, not integer multiplication.This program works correctly in every case. Feedback only affects which code the engine generates, never the results. For most programs a widened slot costs very little, so measure before you restructure code around it.
Exercise
In the bytecode Mul r0, [4], what does [4] refer to, and what kind of information will be stored there?
Show answer
It is the index of a feedback slot in the function's feedback vector. Because it is an arithmetic operation, the slot stores a binary-operation hint, such as SignedSmall, Number, String, BigInt, or Any, that summarizes the operand types seen so far.
Exercise
A function sum(a, b) is called 50,000 times with integers, once with 1.5, and then 50,000 more times with integers. What does its arithmetic slot record at the end, and why doesn't it go back to SignedSmall?
Show answer
It records Number. Feedback slots move only toward more general states in a lattice, so a single floating-point input permanently widens the hint from SignedSmall to Number. This keeps updates cheap and avoids flip-flopping between states.
Exercise
Two modules call the same helper pluck(obj) { return obj.name; }. Module A always passes objects of one shape, and module B always passes objects of a different single shape. Why can the load in pluck still end up polymorphic?
Show answer
Feedback is stored per function in that function's feedback vector, not per caller. Both modules' calls update the same load slot in pluck, so it records two hidden classes and becomes polymorphic even though each caller is monomorphic on its own.