perf(zql): comparator fast paths + compareBounds null fix#5611
Draft
Karavil wants to merge 5 commits intorocicorp:mainfrom
Draft
perf(zql): comparator fast paths + compareBounds null fix#5611Karavil wants to merge 5 commits intorocicorp:mainfrom
Karavil wants to merge 5 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. |
bf7598f to
966b10a
Compare
eac60c0 to
a20de33
Compare
added 5 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.
…imization Optimize hot comparison paths in the IVM pipeline: * Add compareStringUTF8Fast for ASCII-fast string comparison with UTF-8 fallback * Reorder compareValues to check strings before nulls (most common type) * Add single-key fast path in makeComparator avoiding loop overhead * Add single-key fast path in makeBoundComparator with fully inlined comparison * Fix compareBounds null handling for nullable database columns
a20de33 to
49b7023
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
Multiple comparator optimizations targeting the hottest paths in the IVM pipeline, plus a bug fix for nullable column comparisons.
Motivation
In a production app with 45 parent rows x ~200 related rows across 135 IVM pipelines, comparator functions are invoked millions of times per page render (every BTree node comparison during index scans calls
makeBoundComparator, and every row sort callsmakeComparator). These optimizations target the innermost loops:compareUTF8processes full UTF-8 encoding even for simple ASCII, adding unnecessary overhead per comparison.makeBoundComparator->compareBounds->compareValues->compareUTF8is a 4-deep call chain on every BTree node visit. Flattening this into a singlecompareBoundValuehelper eliminates 3 function call frames.Changes
data.ts
compareStringUTF8Fast(): ASCII fast-path that compares char codes directly, falling back tocompareUTF8only when non-ASCII characters are encounteredcompareValues()to check strings before nulls (strings are the most common type in practice)makeComparator(): for single-column orderings, return a direct comparator avoiding the loopmemory-source.ts
compareValues().compareValuesasserts type homogeneity (e.g., callsassertString(b)whenais a string), but nullable database columns can producenullvsstringcomparisons that trigger assertion failures.compareBoundValue()helper: merges sentinel handling (minValue/maxValue), null guards, and type-specific comparison (string viacompareStringUTF8Fast, number via subtraction) into a single function. Well within V8 TurboFan's ~460-bytecode inlining threshold, so it gets inlined at call sites.makeBoundComparator()to delegate tocompareBoundValue: single-key path is ~2 lines instead of 35, multi-key path also usescompareBoundValuedirectly (replacing the oldcompareBoundswrapper).Expected Performance Impact
These changes primarily reduce CPU time in BTree index scans. In profiling:
compareUTF8calls dropped significantly due to ASCII fast-path (most string comparisons resolve without the UTF-8 library call)compareBoundValueflattens the 4-deep call chain into 1 function that V8 inlines, eliminating frame overhead across thousands of BTree node visits per fetchCombined with other IVM optimizations in this series, contributed to reducing page freeze from ~7.7s to <1s across the full optimization series.
Testing
compareStringUTF8Fast(ASCII, Unicode, empty, prefix, property-based)makeComparatorsingle-key path (asc, desc, reverse, equality)compareValuesproperty test updated to useMath.sign(the ASCII fast-path returns char code differences rather than -1/0/1, but the sign is always correct)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)