Master the fundamental concepts of jit compilation through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksDynamic languages call methods with unknown targets. Inline caching stores the last seen callee at the call site; monomorphic sites jump direct after one comparison. JSC and V8 use ICs for obj.method() dispatch.
Call site stub checks receiver's class/map against cached class. Match: jump to cached function. Miss: call runtime to resolve and patch cache.
cLoading…
Production compilers embed this step inside a longer pipeline. GCC flows through cpp, cc1, assembly, and ld; Clang uses the driver, Sema, LLVM IR passes, and a target backend. LLVM bitcode, JVM bytecode, and WASM are other familiar IRs at the same layer. The exercise isolates one pass so you can test it alone before chaining it to the next stage.
You will implement inline caching for dynamic dispatch. This exercise asks you to patch call sites with class checks and direct jumps to cached callee code on the monomorphic fast path.
Simulate inline caches: the technique that makes method calls fast in JavaScript, Smalltalk and Ruby VMs. Each call site remembers the receiver types it has seen and jumps straight to the cached method when the type matches; you'll model the full state machine from uninitialised to monomorphic, polymorphic and megamorphic, with a cost model that shows why megamorphic sites are slow.
cLoading…
Each site holds an ordered list of cached types (at most 4):
| State | Entries | On a call with receiver type T |
|---|---|---|
uninit | 0 | miss: cache T → mono |
mono | 1 | T cached → hit; otherwise miss: append T → poly |
poly | 2-4 | T cached → hit; otherwise miss: append T if fewer than 4 entries, else the site becomes mega |
mega | - | every call does a generic lookup; the cache is never consulted again |
T in the list (1 for the first entry, 2 for the second, …): the checks are sequential.One line per call that changes something (every miss, and the call that turns a site megamorphic):
cLoading…
Then one line per site, in order of first appearance:
cLoading…
P = hits × 100 / calls, truncated. Finish with total cost=C.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover a site reaching 4 entries and then going megamorphic, hits at every position of a polymorphic cache, and several independent sites.