perf(runtime): read schema graph and subschema caches without locking - #132
Merged
Merged
Conversation
`_schema_at` took `spec.graph_lock` on every lookup to read the plain `Dict` `spec.subschemas`, which serialized concurrent decoders. Separately, `_schema_graph` read `spec.graphs` outside the lock while another thread could write it under the lock, which is a data race on a plain `Dict`. Both caches now live in a `SchemaCache` with `@atomic` fields that hold immutable `Dict` snapshots. Hits are lock-free atomic loads. A miss takes `graph_lock`, re-reads the current snapshot, returns an entry a racing writer already published, and otherwise copies, inserts, and publishes the copy. Graphs are still compiled once per direction (double-checked under the lock). The `Spec` keyword constructor that generated modules call is unchanged. With 4 threads on Julia 1.12, an uncontended `_schema_at` hit dropped from 36 to 24 ns, and 4 contending tasks went from 118-146 to 9-11 ns per call amortized. Add tests for cache identity across directions and a cold-cache multi-threaded stress test over the runtime and a generated client's decoding. CI now sets JULIA_NUM_THREADS=4 so the stress test runs threaded. Closes #128
tanmaykm
marked this pull request as ready for review
October 1, 2026 06:02
nkottary
approved these changes
Oct 1, 2026
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.
Closes #128.
_schema_attookspec.graph_lockon every lookup to read the plainDictspec.subschemas, which serialized concurrent decoders._schema_graphcalledhaskey(spec.graphs, ...)outside the lock while another thread could be writing to it under the lock.Both caches now live in a
SchemaCachewith@atomicfields that hold immutableDictsnapshots:graph_lock, re-reads the current snapshot, and returns any entry a racing writer has already published. Otherwise it copies the snapshot, inserts, and publishes the copy. A publishedDictis never mutated.Speckeyword constructor that generated modules call is unchanged, so no contract bump is needed.Rough numbers on Julia 1.12 with 4 threads:
_schema_athit went from 36 ns to 24 ns.Tests
JULIA_NUM_THREADS: 4is now set for the test job, so the stress test runs threaded (Pkg.testforwards the thread count). This applies to every test file.AGENTS.mddocuments it.Possible follow-ups (minor review notes)
graph_lockis held by another task. That would catch a future change that adds the lock back to the hit path.