Skip to content

perf(source-map): lazy-build sourceGapPrefix on first range query - #110

Merged
luojiyin1987 merged 1 commit into
masterfrom
perf/lazy-source-gap-prefix
Sep 7, 2026
Merged

luojiyin1987 merged 1 commit into
masterfrom
perf/lazy-source-gap-prefix

Conversation

@luojiyin1987

@luojiyin1987 luojiyin1987 commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

What

Lazy-build sourceGapPrefix — defer buildSourceGapPrefix() from indexNode() to first multi-segment getSourceRange() call.

Why

buildSourceGapPrefix() was eagerly called in indexNode() for every mapped node, even single-segment nodes whose prefix is trivially [0]. Each call allocates a number[] with one entry per segment. For documents with many text nodes this adds up.

The prefix is only needed by getSourceRange() (not getRaw() or getFieldSourceRange()), and only when the queried range spans multiple segments.

Changes

  • SegmentRangeOptions.sourceGapPrefix → getSourceGapPrefix: () => number[] (lazy getter).
  • resolveSegmentRange(): short-circuit when startSegmentIndex === endSegmentIndex; call getter only for multi-segment ranges.
  • getSourceRange(): pass a lazy getter that builds and caches the prefix on first call.
  • indexNode(): remove eager sourceGapPrefixes.set(...).

Coverage

Caller prefix built?
getRaw() never
getFieldSourceRange() never
getSourceRange() single-segment never
getSourceRange() multi-segment once, cached

Verification

  • 1961/1961 tests pass

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
luojiyin1987 force-pushed the perf/lazy-source-gap-prefix branch from 22c9cda to 7ac0529 Compare September 7, 2026 03:56
@luojiyin1987
luojiyin1987 merged commit 97fef63 into master Sep 7, 2026
14 checks passed
@luojiyin1987
luojiyin1987 deleted the perf/lazy-source-gap-prefix branch September 7, 2026 03:58
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