Skip to content

chasm-jit: let the host cache compiled DEX between runs - #131

Open
Wavesonics wants to merge 3 commits into
CharlieTap:mainfrom
Wavesonics:jit-compilation-cache
Open

Wavesonics wants to merge 3 commits into
CharlieTap:mainfrom
Wavesonics:jit-compilation-cache

Conversation

@Wavesonics

@Wavesonics Wavesonics commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #129.

Adds an instance overload that takes a JitCache, a get/put interface the host implements over whatever storage it likes, so a module chasm has compiled before is loaded from its stored DEX instead of being compiled again. The existing instance is unchanged.

  • Key: the package name the compilation already uses. It is a SHA-256 of the module bytes, the DEX options and, now, the chasm version, so an entry is only ever read back for the code it holds, and an upgrade simply uses new keys. Modules without bytes aren't cached.
  • Package names per store: the occurrence suffix (_2, ...) is now the first name no compiled module in the store already uses, read from the store's module instances (ModuleCompiledFrames exposes packageName). Compiled stack traces are resolved per store, so that's the scope that needs distinct names. A module reloaded into a new store reuses its entry; a second instance in the same store gets a name and an entry of its own.
  • Entry: chasm owns the format: package name, installer, inline memory, chunk starts and the DEX files. A truncated or corrupt entry, or one whose DEX fails to load, is compiled again and replaced. Exceptions from the host's cache are ignored.
  • A hit loads the DEX straight from the entry without copying it. Every Android release from 8.1 on reads a heap buffer from array() at position(), which this relies on.
  • Host's side: chasm never removes entries, so eviction is up to the host. Both calls run on the instantiating thread, and put may defer the write. Entries are loaded as code, so the store must be private to the app.
  • The version comes from the buildconfig plugin, using the version the build publishes.

Measured on an x86_64 API 34 emulator with Harper's grammar checker (Rust, 7.6 MB, 3,574 functions): compiling takes 10,483 ms without an entry and 81 ms with one. Decoding the module takes another 570 ms either way, and the entry is 16.6 MB. Sampling a 12 s compiled load showed 94% of it in compilation (86% DEX lowering, 8% frontend), with class loading too small to register.

Testing:

  • chasm-jit device tests (271) cover the cache against real stores: reuse in a new store, an entry per instance in one store, corrupt and unloadable entries replaced, a throwing cache ignored.
  • New runtime-jit host tests (4) cover the entry format: round trip with several DEX files and chunk starts, truncation, trailing bytes, another package.
  • lintKotlin and checkKotlinAbi pass.

Not done, possible follow-ups: get returning a ByteBuffer so hosts can memory-map entries, and a streaming put.

This branches from main, separately from #130. The two touch neighboring lines in Compile.kt, so whichever merges second needs a small rebase.

instance lowered every module to DEX each time it was called, although
the output for a given module and store configuration never changes.
For a large module this dominates instantiation: Harper's grammar
checker (Rust, 7.6 MB, 3,574 functions) spends about 10.5 s of a 12 s
load in the frontend and DEX lowering on an x86_64 API 34 emulator.

Add an instance overload that takes a JitCache, a get/put interface the
host implements over whatever storage it likes. Chasm owns the keys and
the entry format. The key is the package name the compilation already
derives from a SHA-256 of the module bytes and the DEX options, so a
second instance of a module in one process, which gets a suffixed
package name, has an entry of its own and stack trace attribution is
unchanged. Modules without bytes are never cached.

An entry records the chasm version, the package name, the installer,
inline memory, chunk starts and the DEX files. An entry from another
version, one that is truncated or corrupt, or one whose DEX fails to
load is compiled again and replaced. Exceptions from the host's cache
are ignored. The version is the one the build publishes, generated into
runtime-jit, so builds that share a version string, such as snapshots,
must not share a cache. A hit loads the DEX straight from the entry
without copying it.

On the same emulator, compiling Harper takes 10,483 ms without an entry
and 81 ms with one. The entry is 16.6 MB.

runtime-jit gains a device test source set for the cache, and joins the
device test matrix. Fixes CharlieTap#129.
Package names took an occurrence suffix per process, so every reload of
a module in one process used a new cache key: a host that reloads a
module N times wrote N full entries and missed the cache on each reload
the first time it happened.

Two instances of a module only need distinct package names when they
share a store, the scope in which compiled stack traces are resolved,
so track occurrences per store. A module instantiated in a new store
now reuses its entry, and a second instance in the same store still
gets a name and an entry of its own.

Decoding bounds each count by what the rest of the entry could hold,
measured after the count is read, so a corrupt entry decodes to null
rather than throwing or allocating far more than its size. JitCache
documents that hosts own eviction, that both calls run on the
instantiating thread, and how long chasm reads a returned array.
The cache key was the package name, and the chasm version was only
checked inside the entry, so after an upgrade every module's first
lookup read a whole stale entry just to reject it. Digest the chasm
version into the package name instead: a new version uses new keys,
and the entry drops its version field.

Package name suffixes came from a per-store side map that only ever
counted up. Take the first name that no compiled module in the store
already uses, read from the store's own module instances, so an
instantiation that fails after compiling no longer moves its retry to
a new name and cache key.

Generate the version with the buildconfig plugin the build already
uses, load from a single CachedCompilation, and encode without sizing
the buffer by hand. The cache tests now run against real stores in
chasm-jit, and the entry format is covered by host tests, so
runtime-jit needs no device test suite of its own.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chasm-jit: let the host cache compiled DEX between runs

1 participant