Techniquecore
Inline caching
Also called inline cache, Deutsch-Schiffman inline cache.
Caches at each call site the receiver type and method found last time and checks the type on the next call before using it.
An inline cache stores, at one call site, the receiver class that the site saw last time and the method that the lookup found for it. On the next call the code checks that the receiver has the same class, and if so it calls the cached method directly, without a lookup. Deutsch and Schiffman introduced inline caches in their Smalltalk-80 system in 1984. The idea works because most call sites in object-oriented programs see receivers of one class only. The cache is a guard plus a stored answer, so it gives the same result as a full lookup as long as the guard is checked and the stored answer is thrown away when the classes change. The polymorphic inline cache extends it to sites with several classes, and hidden classes give a class-like key to objects in languages without classes.
In plain words
In an object-oriented language, a call such as shape.area() must find the right area method for the kind of object in shape. Finding it means searching the class of the object and then its parent classes. That search is slow if it happens on every call.
At most places in a program, the object is of the same kind every time. So each call place keeps a small note: "last time the object was a Circle, and the method was Circle's area". On the next call, the code checks that the object is again a Circle. If yes, it jumps straight to that method. If no, it does the full search and writes a new note. If someone changes a class while the program runs, the notes that depend on it are erased.
When it applies
- Calls are dispatched at run time on the class of the receiver, as in Smalltalk, SELF, JavaScript, Python or Ruby, and a full lookup walks a chain of method tables or a hash table.
- Most call sites are monomorphic: they see one receiver class. In SELF programs, Hölzle measured that 93.6% of the send sites that were executed saw one receiver type, 5.1% saw two, and 0.5% saw more than four (thesis, Figure 3-4). These are measurements on his benchmark programs, not a law.
- The code at the site can be changed at run time, because it is generated by a JIT compiler or is bytecode that the interpreter may rewrite.
- The receiver class can be read quickly from the object, from a class pointer, a hidden class or a tag.
The template
The full lookup
Each class has a method table and a parent , which may be empty. The chain of is . The full lookup is
LOOKUP(K, s)
for each class D in the chain of K, from K upward
if s is in methods(D) then return methods(D)[s]
return the "message not understood" handler
Its result depends only on the method tables and parent links of the classes in the chain of .
The cache at a site
A send site with selector holds a pair , or the empty state.
SEND(site, r, args)
L1 K ← class(r)
L2 if site is not empty and K = site.K̂ then return CALL(site.m̂, r, args) ▷ hit
L3 m ← LOOKUP(K, s) ▷ miss
L4 site.K̂ ← K; site.m̂ ← m
L5 return CALL(m, r, args)
In the Smalltalk-80 system the site is a call instruction in the generated code. The site calls directly, and the test of line L2 is in the prologue of , which compares the class of the receiver with , stored next to the call. On a miss, the prologue calls the lookup routine, which rewrites the call instruction to point to the new method. An empty site calls the lookup routine directly.
Invalidation
When the program changes a method table or a parent link at run time, a stored answer can become wrong. The rule is:
When the method table or the parent of a class changes, empty each site whose cached class has in its chain.
A simpler and coarser rule is to empty every site with the changed selector, or every site in the program. Coarser rules are correct too, because an empty site is always correct.
Why it is correct
Invariant. For every site with selector that is not empty, under the current method tables and parent links.
Claim. If the invariant holds before a send, then SEND calls , and the invariant still holds after line L4.
Proof. On a hit, , so the method called is by the invariant. On a miss, the method called is by line L3, and line L4 stores a pair that satisfies the invariant. Other sites do not change.
Claim. The invalidation rule keeps the invariant when a class changes.
Proof. Let the method table or the parent of change. Take a site that is not empty after the change. By the rule, is not in the chain of , as computed before the change. The chain of is found by following parent links from , and none of these links belongs to , so the chain is the same after the change. LOOKUP reads only the method tables of the classes in the chain, and none of them is , so is the same before and after. So the invariant still holds for the site. Sites that the rule empties satisfy the invariant trivially.
Together, by induction over the steps of the program, every send calls the method that a full lookup would find. The guard of line L2 is necessary: it is the only reason that a hit is correct. The cache is a memoization of LOOKUP with one entry, keyed by the class, and the invalidation rule removes the entries whose input changed.
A worked example
Take three classes. Object defines print and hash. Point, whose parent is Object, defines x and print. ColorPoint, whose parent is Point, defines color. One site sends print to five receivers in turn.
| call | receiver class | state before | result | method called | state after |
|---|---|---|---|---|---|
| 1 | Point | empty | miss | Point.print | (Point, Point.print) |
| 2 | Point | (Point, Point.print) | hit | Point.print | same |
| 3 | ColorPoint | (Point, Point.print) | miss | Point.print | (ColorPoint, Point.print) |
| 4 | ColorPoint | (ColorPoint, Point.print) | hit | Point.print | same |
| 5 | Point | (ColorPoint, Point.print) | miss | Point.print | (Point, Point.print) |
Three of the five calls miss, although all of them call the same method, because the key is the exact class. The miss on call 3 walks two classes in LOOKUP, ColorPoint and then Point.
Now the program adds a method print to ColorPoint. The chain of Point is Point, Object, which does not hold ColorPoint, so the site with state (Point, Point.print) stays filled, and it is still right. A site with state (ColorPoint, Point.print) would be emptied, because its next correct answer is ColorPoint.print.
Classic examples
- Smalltalk-80. Deutsch and Schiffman translated bytecode to native code and put an inline cache at every send. Smalltalk systems also keep a global lookup cache, a hash table keyed by class and selector, which a miss tries before a full lookup.
- SELF. SELF has no classes, but objects that were cloned from the same prototype share a map, and the map is the key of the inline cache. SELF later replaced the one-entry cache by the polymorphic inline cache, whose entries also give the receiver types for type feedback.
- JavaScript engines. The V8 engine puts an inline cache at each property access such as
o.x. The key is the hidden class ofo, and the stored answer is the offset ofxin the object, not a method. - Bytecode interpreters. An interpreter can keep a cache entry next to each instruction in the bytecode and rewrite the instruction to a faster form after it has seen the operand types. This is called quickening, and CPython 3.11 uses it in its specializing interpreter.
Variations
- Check at the call site or in the callee. The check can be in the code of the site, before the call, or in the prologue of the method, as in Smalltalk-80. The prologue form keeps the site small and shares one check per method.
- Polymorphic and megamorphic sites. A site that sees a few classes keeps a short list of pairs, which is the polymorphic inline cache. A site that sees many classes gives up and always does a lookup, often in a global hash table.
- Caching other answers. The same pattern caches the offset of a field, the result of a global variable lookup, or the result of an
instanceoftest. Each is a stored answer behind a guard on the input it depends on. - Static devirtualization instead. If class hierarchy analysis proves that a site has one target, the compiler can call it directly with no guard, which is devirtualization. It still needs invalidation if new classes can be loaded.
Pitfalls
- A guard that tests too little. The guard must test exactly the input that the stored answer depends on. Testing only that the receiver is "some subclass of Point" is wrong if a subclass overrides the method.
- No invalidation. Without it, a site keeps calling an old method after the program redefines it, which is wrong in a language that allows changes at run time. In JavaScript, a change to an object on the prototype chain is such a change.
- Thrashing. A site that sees two classes in turn misses on every call, as calls 3 and 5 above show for one change each. This is the reason for the polymorphic inline cache.
- Patching code that runs on other threads. Rewriting a call instruction while another thread runs it must be atomic, or the other thread can see half of the new instruction.
- Counting hits as speed. A high hit rate makes a send cheaper, but the gain on a whole program depends on the program and the machine, and must be measured.
Exercises
- Run the worked example again with the receivers Point, ColorPoint, Point, ColorPoint, Point, ColorPoint. How many misses are there? How many with a cache keyed by the method found instead of by the class, and why is that cache wrong?
- Prove that the coarse rule "empty every site with the changed selector" keeps the invariant when only a method table changes. Why is it not enough when a parent link changes?
- In JavaScript, a property read
o.xcan findxon the prototype ofo. What must the guard of an inline cache for this read test, and what change must empty it? - Design the data at a send site in a bytecode interpreter that has no JIT compiler. Where do the cached class and method go, and what does the interpreter do on a miss?
- Give an invariant and an invalidation rule for an inline cache of global variable reads, where a global can be redefined at any time.
References
- L. Peter Deutsch and Allan M. Schiffman, Efficient implementation of the Smalltalk-80 system, POPL 1984, 297-302, the inline cache of a send site, with the class check in the prologue of the called method and the rebinding of the call on a miss
- Urs Hölzle, Adaptive optimization for SELF: reconciling high performance with exploratory programming, PhD thesis, Stanford University, 1994 (report STAN-CS-TR-94-1520), Chapter 3, Section 3.3 (inline caches) and Section 3.4 with Figure 3-4 (the number of receiver types seen at send sites of SELF programs)
- Urs Hölzle, Craig Chambers and David Ungar, Optimizing dynamically-typed object-oriented languages with polymorphic inline caches, ECOOP 1991, LNCS 512, 21-38, the description of the monomorphic inline cache and its cost at polymorphic sites, which motivates the polymorphic inline cache
- Craig Chambers, David Ungar and Elgin Lee, An efficient implementation of SELF, a dynamically-typed object-oriented language based on prototypes, OOPSLA 1989, 49-70, maps as the key of the inline cache in a language with prototypes and no classes
- Wonsun Ahn, Jiho Choi, Thomas Shull, María J. Garzarán and Josep Torrellas, Improving JavaScript performance by deconstructing the type system, PLDI 2014, 496-507, the inline caches of the V8 engine for property access, keyed by hidden classes
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag EPC3SV Edit this page History Report an error Markdown