perf(source-map): lazy-build sourceGapPrefix on first range query - #110
Merged
Merged
Conversation
buildSourceGapPrefix() was eagerly called in indexNode() for every mapped node, even single-segment nodes whose prefix is trivially [0]. Defer to first use in getSourceRange() and skip entirely for single-segment ranges (startSegmentIndex === endSegmentIndex) where the gap check is irrelevant.
luojiyin1987
force-pushed
the
perf/lazy-source-gap-prefix
branch
from
September 7, 2026 03:56
22c9cda to
7ac0529
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.
What
Lazy-build
sourceGapPrefix— deferbuildSourceGapPrefix()fromindexNode()to first multi-segmentgetSourceRange()call.Why
buildSourceGapPrefix()was eagerly called inindexNode()for every mapped node, even single-segment nodes whose prefix is trivially[0]. Each call allocates anumber[]with one entry per segment. For documents with many text nodes this adds up.The prefix is only needed by
getSourceRange()(notgetRaw()orgetFieldSourceRange()), and only when the queried range spans multiple segments.Changes
SegmentRangeOptions.sourceGapPrefix→getSourceGapPrefix: () => number[](lazy getter).resolveSegmentRange(): short-circuit whenstartSegmentIndex === endSegmentIndex; call getter only for multi-segment ranges.getSourceRange(): pass a lazy getter that builds and caches the prefix on first call.indexNode(): remove eagersourceGapPrefixes.set(...).Coverage
getRaw()getFieldSourceRange()getSourceRange()single-segmentgetSourceRange()multi-segmentVerification