Sub topic 8
Inline Caches: Remembering Where Properties Live
This chapter assumes you know how V8 turns source into bytecode and runs it in Ignition. It also assumes you know that hidden classes, which V8 calls maps, describe an object's layout. Here you will see how the engine uses that layout to skip repeated property lookups.
The cost of a plain property lookup
Take o.x. Without any caching, the engine has to do this on every access:
- Read the hidden class of
o. - Search that hidden class's property descriptors for
x. - Find the slot offset where
xis stored. - Load the value from that offset, or walk the prototype chain if
xis missing.
The search in step 2 is the slow part. In a hot loop, the same line of code usually sees objects with the same hidden class every time, so it finds the same offset every time.
Caching the answer at the call site
An inline cache (IC) is a small record attached to one specific property access in the code. It remembers the hidden class it saw last and the offset it found for that class. The next time that access runs, the engine compares the object's hidden class with the cached one. If they match, which is called a cache hit, it loads straight from the cached offset. If they differ, which is called a miss, it does the full lookup and updates the cache.
Each IC belongs to one call site, meaning one location in the source code. The same property name used in two different functions gets two separate caches.
A toy inline cache you can run
The script below models hidden classes and a single-entry load IC in plain JavaScript. It counts how many slow lookups happen.
let slowLookups = 0;
class Shape {
constructor(entries) {
this.offsets = new Map(entries); // property name -> slot index
}
}
function makeObject(shape, slots) {
return { shape, slots };
}
function slowLookup(obj, name) {
slowLookups++;
return obj.shape.offsets.get(name);
}
function createLoadIC(name) {
let cachedShape = null;
let cachedOffset = -1;
return function load(obj) {
if (obj.shape === cachedShape) {
return obj.slots[cachedOffset]; // hit: no search
}
const offset = slowLookup(obj, name); // miss
cachedShape = obj.shape;
cachedOffset = offset;
return obj.slots[offset];
};
}
const xyShape = new Shape([['x', 0], ['y', 1]]);
const yxShape = new Shape([['y', 0], ['x', 1]]);
const loadX = createLoadIC('x');
for (let i = 0; i < 1000; i++) {
loadX(makeObject(xyShape, [i, 0]));
}
console.log('Same shape, 1000 loads:', slowLookups); // 1
slowLookups = 0;
for (let i = 0; i < 1000; i++) {
const shape = i % 2 === 0 ? xyShape : yxShape;
const slots = shape === xyShape ? [i, 0] : [0, i];
loadX(makeObject(shape, slots));
}
console.log('Alternating shapes, 1000 loads:', slowLookups); // 1000When every object has the same shape, only the first load misses. When two shapes alternate, a cache that holds a single entry misses on every load. Real engines handle this case by storing several entries, which the next chapter covers.
Common mistakes
| Mistake | Why it hurts the IC |
|---|---|
| Building objects with properties in different orders | The objects get different hidden classes, so one call site sees several shapes |
| Adding properties conditionally after construction | Some objects move to new hidden classes and stop matching the cache |
| Treating ICs as global | Caches are per call site, so a hot function's ICs depend only on what that function receives |
Exercise
A function area(r) reads r.width and r.height. Every rectangle it receives comes from the same constructor. How many inline caches does this function's code contain for these reads, and how many misses do you expect after many calls?
Show answer
It has two ICs, one for the r.width access and one for the r.height access. Every object shares one hidden class, so each IC misses once on the first call and hits after that, for two misses in total.
Exercise
In the toy script, what change to createLoadIC would stop the alternating loop from missing on every load?
Show answer
Store more than one pair of shape and offset, for example in a small array or Map keyed by shape. Check every stored entry before falling back to slowLookup. With both shapes cached, the alternating loop has only two misses.