Sub topic 9
Monomorphic, Polymorphic, and Megamorphic Call Sites
This chapter assumes you have read the earlier chapters on hidden classes, shape transitions, and inline caches. You should already know that an inline cache (IC) stores the hidden class it saw at a property access, along with where that property lives. Here we look at what happens when one access site sees more than one hidden class.
The states of an inline cache
Every property access in your source, such as o.x, is a separate call site with its own inline cache. V8 classifies each cache by how many distinct hidden classes (maps) it has recorded:
| State | Maps seen | How the lookup works |
|---|---|---|
| Uninitialized | 0 | The code has not run yet, so there is nothing cached |
| Monomorphic | 1 | One map check, then a direct load at a known offset |
| Polymorphic | 2 to 4 | The map is compared against a short list of cached entries |
| Megamorphic | more than 4 | The site falls back to a generic, shared lookup cache |
A monomorphic site is the fastest case. The optimizing compiler can turn it into a single guarded memory load. A polymorphic site is still reasonably fast, though each access may compare against several maps. A megamorphic site stops caching per-site entries and uses a global stub cache keyed by map and property name. That lookup is slower, and it gives the optimizing compiler much less to specialize on.
States only move forward. Once a site is megamorphic, it stays megamorphic for the life of that code. Seeing only one shape afterward does not move it back.
Watching a site degrade
function getX(o) {
return o.x;
}
// One shape seen: the property access in getX is monomorphic.
for (let i = 0; i < 100000; i++) {
getX({ x: i });
}
// A second shape: the inline cache becomes polymorphic.
getX({ x: 1, y: 2 });
// Each further distinct shape adds an entry.
getX({ y: 2, x: 1 });
getX({ x: 1, z: 3 });
getX({ x: 1, w: 4 });
// That is five distinct maps, so the o.x site is now megamorphic.Each object literal here has a different hidden class. Even { x: 1, y: 2 } and { y: 2, x: 1 } differ, because insertion order defines the shape. As a result, x can sit at different offsets, and one cached offset no longer covers every case.
Common causes
- Building the same logical object in different places with properties in different orders.
- Adding optional properties conditionally, so some objects have
zand others do not. - Writing generic helpers, such as a
getId(obj)used on users, orders, and products, that receive unrelated shapes. - Mixing class instances with plain object literals at the same site.
Some of these are deliberate and fine. A generic utility is expected to be polymorphic. The cost only matters on hot paths that profiling identifies.
Keeping a site monomorphic
Create objects through one constructor or factory, and initialize every field, even optional ones:
function makePoint(x, y, z = undefined) {
return { x, y, z }; // same keys, same order, every time
}
function getX(o) {
return o.x;
}
let sum = 0;
for (let i = 0; i < 100000; i++) {
sum += getX(makePoint(i, i));
sum += getX(makePoint(i, i, i));
}
console.log(sum); // getX's o.x site sees one mapExercise
A function reads item.price and receives objects from three factories, each producing a consistent but different shape. Later, one call passes an object with properties added in a random order, producing three new shapes. What state does the item.price site end in, and does it recover if the random objects stop arriving?
Show answer
It ends megamorphic. Three shapes make it polymorphic, and three more bring the total to six, which exceeds V8's four-map polymorphic limit. It does not recover, because IC states only move toward more general states for the lifetime of that code.
Exercise
Why is { a: 1, b: 2 } versus { b: 2, a: 1 } enough to turn a monomorphic site polymorphic?
Show answer
Hidden classes are built from the order in which properties are added, so the two literals get different maps, and a sits at a different offset in each. The inline cache must record both maps.