Skip to content

perf(zql): cache primary index key, pkConstraint, and index lookups#5610

Draft
Karavil wants to merge 4 commits intorocicorp:mainfrom
goblinshq:perf/index-caching
Draft

perf(zql): cache primary index key, pkConstraint, and index lookups#5610
Karavil wants to merge 4 commits intorocicorp:mainfrom
goblinshq:perf/index-caching

Conversation

@Karavil
Copy link
Contributor

@Karavil Karavil commented Feb 25, 2026

Note: This PR is part of an upstream contribution effort from the Goblins team (@goblinshq). Co-authored with Claude by Anthropic.

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:

  1. Primary index key: #getPrimaryIndex() calls JSON.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.
  2. PK constraint: primaryKeyConstraintFromFilters() recomputes the primary key constraint from filters on every fetch, even though filters are fixed at connection time.
  3. Index lookup: #getOrCreateIndex() is called with the same sort+constraint parameters repeatedly for the same connection. The JSON.stringify inside it adds further serialization overhead.

Changes

  • Cache #primaryIndexKey (JSON.stringify of primary index sort) in the constructor, used in #getPrimaryIndex() and index initialization
  • Cache pkConstraint (primaryKeyConstraintFromFilters result) on Connection during connect(), read from cache in #fetch()
  • Cache #getOrCreateIndex results per connection via indexCache Map, keyed by constraint shape + sort

Expected Performance Impact

#getPrimaryIndex() and #getOrCreateIndex() are called on every fetch. For 135 IVM pipelines processing ~200 rows each, caching eliminates thousands of redundant JSON.stringify calls and map lookups per page render. The per-connection index cache also avoids repeated string key computation for the index map.

Testing

  • All 56 existing memory-source tests pass
  • All 154 existing source tests pass

Stack Order

This PR is part of a stacked series of IVM performance optimizations. Merge in order:

  1. perf(zql): reduce allocations with frozen sentinel and object reuse #5609 - Allocation reduction
  2. perf(zql): cache primary index key, pkConstraint, and index lookups #5610 (this PR) - Index caching
  3. perf(zql): comparator fast paths + compareBounds null fix #5611 - Comparator fast paths
  4. perf(zql): fuse fetch pipeline, add PK fast path, reduce allocations #5612 - Fetch pipeline fusion

Independent PRs (no conflicts): #5607 (BTree iterators), #5608 (Join optimizations)

@vercel
Copy link

vercel bot commented Feb 25, 2026

Someone is attempting to deploy a commit to the Rocicorp Team on Vercel.

A member of the Team first needs to authorize it.

Alp 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.
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.

1 participant