An Inline Cache Isn't Just a Cache
4 days ago
- #SpiderMonkey
- #Inline Caching
- #JavaScript Optimization
- Inline caching is more than just a cache; it's a dynamic method for optimizing code execution by modifying call sites based on runtime behavior.
- Originating from Smalltalk-80, inline caches start as method lookups but self-modify to directly call the resolved method after the first invocation, assuming future calls will be similar.
- SpiderMonkey uses inline caches extensively for operations like property accesses, arithmetic, and method calls, generating stubs with guards for efficient execution paths.
- An example with JavaScript's addition operation shows how inline caches handle different types (e.g., Int32+Int32, String+String) by building chains of stubs for observed cases.
- CacheIR is SpiderMonkey's current state-of-the-art for inline caching, abstracting IC details to share across compilers within the engine.
- Inline caching has evolved significantly since its 1983 origins, with systems like LIL in 2004 showing advanced stub architectures similar to modern implementations.