Skip to content

feat(chunk): add pre-encoded SourceMapChunk and stitch_chunks (esbuild model)#372

Draft
Boshen wants to merge 1 commit into
mainfrom
perf/sourcemap-chunk-stitch
Draft

feat(chunk): add pre-encoded SourceMapChunk and stitch_chunks (esbuild model)#372
Boshen wants to merge 1 commit into
mainfrom
perf/sourcemap-chunk-stitch

Conversation

@Boshen

@Boshen Boshen commented Jun 20, 2026

Copy link
Copy Markdown
Member

Summary

First step toward an esbuild-model source map pipeline: build the mappings VLQ once and join maps by stitching pre-encoded bytes, instead of materializing a combined Vec<Token> and re-serializing.

SourceMapChunk mirrors esbuild's sourcemap.Chunk — a map's mappings already encoded to VLQ plus the small delta-carry state needed to splice it. stitch_chunks re-encodes only each chunk's first segment (and first tail name) against the running state, then copies the rest of the bytes verbatim → O(modules) work, not O(tokens).

API

  • SourceMapState — delta-carry state of a mappings stream
  • SourceMap::to_chunk() — encode mappings once, capture seam metadata
  • stitch_chunks(&[(&SourceMapChunk, line_offset)]) — O(modules) join of pre-encoded chunks
  • encode_vlq_diff made pub(crate) to reuse the exact VLQ primitive

Correctness

stitch_chunks output is asserted byte-identical to the trusted path ConcatSourceMapBuilder::from_sourcemaps(...).into_sourcemap().to_json().mappings:

  • 4 curated cases (single chunk; no names; name in first-segment vs tail; 3-way mixed)
  • a 2000-iteration deterministic fuzz (seeded LCG, no rand dependency) over random module counts, token streams, names, and line offsets

Notes

  • Invariant: each chunk's first token must carry a source (a code generator always emits a start-of-file mapping; esbuild assumes the same). The fuzz respects it.
  • ConcatSourceMapBuilder already appends names/sources wholesale (no cross-module dedup) — same as esbuild — so the stitch is byte-identical with no names tradeoff.
  • Infrastructure only / behavior-preserving: nothing produces SourceMapChunk yet. Next: a Mappings::{Tokens,Encoded} representation so to_json_string short-circuits and SourceJoiner stitches, then route no-chain modules through to_chunk.

Draft — opening for early review of the stitch math and API shape.

Introduce the esbuild "Chunk" model: a source map whose `mappings` are
already VLQ-encoded, plus the minimal delta-carry state needed to splice
it into a larger map. `stitch_chunks` joins chunks by re-encoding only
each chunk's first segment (and first tail name) against the running
state, then copying the rest of the bytes verbatim — making concat
O(modules) instead of O(tokens), with no combined `Vec<Token>` and no
separate serialize pass.

- `SourceMapState`: delta-carry state of a mappings stream
- `SourceMap::to_chunk()`: encode mappings once, capture seam metadata
- `stitch_chunks()`: O(modules) join of pre-encoded chunks
- `encode_vlq_diff` made `pub(crate)` to reuse the exact VLQ primitive

Validated byte-for-byte against `ConcatSourceMapBuilder` + serialize via
4 curated cases and a 2000-iteration deterministic fuzz. Infrastructure
only — nothing produces `SourceMapChunk` yet.
@codspeed-hq

codspeed-hq Bot commented Jun 20, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 2.03%

❌ 2 regressed benchmarks
✅ 14 untouched benchmarks
⏩ 7 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
lookup_table[real_small] 1.3 µs 1.4 µs -2.13%
lookup_table[real_medium] 1.5 µs 1.5 µs -1.93%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing perf/sourcemap-chunk-stitch (19f9868) with main (296f357)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

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