Repository navigation
chasm-jit: let the host cache compiled DEX between runs - #131
Open
Wavesonics wants to merge 3 commits into
Open
Wavesonics wants to merge 3 commits into
Wavesonics wants to merge 3 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #129.
Adds an
instanceoverload that takes aJitCache, 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 existinginstanceis unchanged._2, ...) is now the first name no compiled module in the store already uses, read from the store's module instances (ModuleCompiledFramesexposespackageName). 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.array()atposition(), which this relies on.putmay defer the write. Entries are loaded as code, so the store must be private to the app.buildconfigplugin, 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-jitdevice 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.runtime-jithost tests (4) cover the entry format: round trip with several DEX files and chunk starts, truncation, trailing bytes, another package.lintKotlinandcheckKotlinAbipass.Not done, possible follow-ups:
getreturning aByteBufferso hosts can memory-map entries, and a streamingput.This branches from
main, separately from #130. The two touch neighboring lines inCompile.kt, so whichever merges second needs a small rebase.