perf(shared): class-based BTree iterators#5607
Draft
Karavil wants to merge 1 commit intorocicorp:mainfrom
Draft
Conversation
Class-based iterators (BTreeForwardIterator/BTreeReverseIterator) store traversal state in instance properties instead of closure variables, which V8 optimizes better for hot iteration paths. Part of IVM pipeline perf optimizations that reduced page freeze from ~7.7s to <1s in a production app. * Add BTreeForwardIterator<K> class with next() and [Symbol.iterator]() * Add BTreeReverseIterator<K> class with next() and [Symbol.iterator]() * Modify valuesFrom() to return BTreeForwardIterator * Modify valuesFromReversed() to return BTreeReverseIterator * Keep iterator() helper for empty-tree case
|
Someone is attempting to deploy a commit to the Rocicorp Team on Vercel. A member of the Team first needs to authorize it. |
This was referenced Feb 25, 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.
Summary
Replace closure-based BTree iterators with class-based
BTreeForwardIteratorandBTreeReverseIterator.Motivation
BTree iterators are on the hot path for every index scan in the IVM pipeline. In a workload with 135 IVM pipelines each scanning ~200 rows, these iterators are instantiated and advanced thousands of times per page render.
The current implementation uses closures (
iterator(() => {...})) which capture mutable state in closure variables. V8 optimizes property access on class instances better than captured closure variables, particularly for hot iteration paths where the JIT compiler can inline property access more effectively.Changes
BTreeForwardIterator<K>class implementingIterableIterator<K>with traversal state (_nodeQueue,_nodeIndex,_leaf,_i) as instance propertiesBTreeReverseIterator<K>class implementingIterableIterator<K>with the same patternvaluesFrom()to return a class instance instead of a closure-based iteratorvaluesFromReversed()to return a class instanceiterator()helper for the empty-tree edge case (wherefindPathreturns undefined)Expected Performance Impact
Each BTree scan creates one iterator and calls
.next()for every row visited. With class-based iterators, V8 can create a hidden class for the iterator and optimize property access into direct memory offsets, whereas closure variable access goes through the scope chain. The improvement is per-.next()call, compounding across thousands of iterations per page render.Testing
[Symbol.iterator]returns self