perf(zql): cache primary index key, pkConstraint, and index lookups#5610
Draft
Karavil wants to merge 4 commits intorocicorp:mainfrom
Draft
perf(zql): cache primary index key, pkConstraint, and index lookups#5610Karavil wants to merge 4 commits intorocicorp:mainfrom
Karavil wants to merge 4 commits intorocicorp:mainfrom
Conversation
|
Someone is attempting to deploy a commit to the Rocicorp Team on Vercel. A member of the Team first needs to authorize it. |
d0076f5 to
0210424
Compare
This was referenced Feb 25, 2026
a0b573c to
6969e3b
Compare
added 4 commits
February 25, 2026 06:10
…euse
Two allocation reduction optimizations for the IVM push hot path:
1. Shared EMPTY_RELATIONSHIPS sentinel: Replace per-node {} allocation
with a frozen shared object, reducing GC pressure during fetch and push.
2. Reuse outputChange objects in genPush: Pre-allocate reusable objects
and mutate row fields before yielding, instead of creating new objects
per connection.
Object reuse is safe because filterPush consumers are synchronous within
the generator chain.
Cache frequently recomputed values to avoid repeated JSON.stringify and map lookups on hot paths: - Cache #primaryIndexKey in constructor (avoid JSON.stringify per call) - Cache pkConstraint on Connection (avoid recomputing from filters) - Cache #getOrCreateIndex results per connection (avoid repeated lookups) Part of IVM pipeline perf optimizations that reduced page freeze from ~7.7s to <1s in a production app.
6969e3b to
a8d5eca
Compare
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.
Summary
Cache frequently recomputed values in MemorySource to avoid repeated serialization and map lookups on hot paths.
Motivation
Three values are recomputed on every
#fetch()call despite being constant for a given connection:#getPrimaryIndex()callsJSON.stringify(this.#primaryIndexSort)on every invocation. With 135 pipelines, this runs thousands of times per page render for a value that never changes after construction.primaryKeyConstraintFromFilters()recomputes the primary key constraint from filters on every fetch, even though filters are fixed at connection time.#getOrCreateIndex()is called with the same sort+constraint parameters repeatedly for the same connection. TheJSON.stringifyinside it adds further serialization overhead.Changes
#primaryIndexKey(JSON.stringifyof primary index sort) in the constructor, used in#getPrimaryIndex()and index initializationpkConstraint(primaryKeyConstraintFromFiltersresult) onConnectionduringconnect(), read from cache in#fetch()#getOrCreateIndexresults per connection viaindexCacheMap, keyed by constraint shape + sortExpected Performance Impact
#getPrimaryIndex()and#getOrCreateIndex()are called on every fetch. For 135 IVM pipelines processing ~200 rows each, caching eliminates thousands of redundantJSON.stringifycalls and map lookups per page render. The per-connection index cache also avoids repeated string key computation for the index map.Testing
Stack Order
This PR is part of a stacked series of IVM performance optimizations. Merge in order:
Independent PRs (no conflicts): #5607 (BTree iterators), #5608 (Join optimizations)