refactor: apply borrow builder pattern to ConcatSourceMapBuilder#366
Merged
Conversation
Mirror the `SourceMapBuilder` change in #361: store `&'a str` internally instead of `Cow<'a, str>` and defer the borrow-vs-own decision to the terminal methods. Adds `into_owned_sourcemap` alongside `into_sourcemap` to match the `SourceMapBuilder` surface. The public API is backward compatible (fields are `pub(crate)`, existing method signatures are unchanged).
Merging this PR will degrade performance by 3.18%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ❌ | from_sourcemaps |
304.9 µs | 317.9 µs | -4.06% |
| ❌ | add_sourcemap_loop |
322.7 µs | 330.3 µs | -2.29% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing refactor/concat-borrow-builder (7c349ae) with main (1bb86f5)
Footnotes
-
5 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. ↩
Merged
Boshen
added a commit
that referenced
this pull request
Jun 19, 2026
#366 switched `ConcatSourceMapBuilder` to borrowed `&'a str` storage, which is optimal when inputs outlive the result. But its main consumer (rolldown's `SourceJoiner`) feeds *owned* `SourceMap<'static>` maps and wants an owned map back, so `into_owned_sourcemap` deep-copied every name/source/sourcesContent string — and `sourcesContent` is the full module text. Store entries as `Cow<'a, str>` again so the builder can absorb owned strings by moving them, and add: * `add_sourcemap_owned(SourceMap<'a>, u32)` / `from_owned_sourcemaps(..)` — consume owned maps and move their `Cow` entries in (no string bytes copied). * `add_sourcemap` (borrowed) and `add_sourcemap_owned` now share an `add_tokens` helper; only how the strings get in differs. `into_sourcemap` moves the accumulated vectors straight out (also cheaper than #366's rebuild for the borrowed case); `into_owned_sourcemap` keeps moved-in owned strings as-is and copies only borrowed ones. The `concat/owned_maps` benchmark (added in #369 as the borrow baseline) is switched to the move-in path here. From the investigation sweep (200 modules): move-in is flat at ~32us regardless of `sourcesContent` size, while borrow + `into_owned_sourcemap` grows linearly (37us @256b, 47us @2KB, 70us @8kb, 542us @32kb) — up to ~16x faster, identical mappings output.
Boshen
added a commit
that referenced
this pull request
Jun 19, 2026
#366 switched `ConcatSourceMapBuilder` to borrowed `&'a str` storage, which is optimal when inputs outlive the result. But its main consumer (rolldown's `SourceJoiner`) feeds *owned* `SourceMap<'static>` maps and wants an owned map back, so `into_owned_sourcemap` deep-copied every name/source/sourcesContent string — and `sourcesContent` is the full module text. Store entries as `Cow<'a, str>` again so the builder can absorb owned strings by moving them, and add: * `add_sourcemap_owned(SourceMap<'a>, u32)` / `from_owned_sourcemaps(..)` — consume owned maps and move their `Cow` entries in (no string bytes copied). * `add_sourcemap` (borrowed) and `add_sourcemap_owned` now share an `add_tokens` helper; only how the strings get in differs. `into_sourcemap` moves the accumulated vectors straight out (also cheaper than #366's rebuild for the borrowed case); `into_owned_sourcemap` keeps moved-in owned strings as-is and copies only borrowed ones. The `concat` benchmark (added in #369 as `ConcatSourceMapBuilder::from_sourcemaps`, the borrow baseline) is switched here to `ConcatSourceMapBuilder::from_owned_sourcemaps`. From the investigation sweep (200 modules): move-in is flat at ~32us regardless of `sourcesContent` size, while borrow + `into_owned_sourcemap` grows linearly (37us @256b, 47us @2KB, 70us @8kb, 542us @32kb) — up to ~16x faster, identical mappings output.
Boshen
added a commit
that referenced
this pull request
Jun 19, 2026
#366 switched `ConcatSourceMapBuilder` to borrowed `&'a str` storage, which is optimal when inputs outlive the result. But its main consumer (rolldown's `SourceJoiner`) feeds *owned* `SourceMap<'static>` maps and wants an owned map back, so `into_owned_sourcemap` deep-copied every name/source/sourcesContent string — and `sourcesContent` is the full module text. Store entries as `Cow<'a, str>` again so the builder can absorb owned strings by moving them, and add: * `add_sourcemap_owned(SourceMap<'a>, u32)` / `from_owned_sourcemaps(..)` — consume owned maps and move their `Cow` entries in (no string bytes copied). * `add_sourcemap` (borrowed) and `add_sourcemap_owned` now share an `add_tokens` helper; only how the strings get in differs. `into_sourcemap` moves the accumulated vectors straight out (also cheaper than #366's rebuild for the borrowed case); `into_owned_sourcemap` keeps moved-in owned strings as-is and copies only borrowed ones. The `concat` benchmark group covers all four entry points: `from_sourcemaps` / `add_sourcemap` (borrow — the baseline added in #369) plus `from_owned_sourcemaps` / `add_sourcemap_owned` (the move-in variants added here). From the investigation sweep (200 modules): move-in is flat at ~32us regardless of `sourcesContent` size, while borrow + `into_owned_sourcemap` grows linearly (37us @256b, 47us @2KB, 70us @8kb, 542us @32kb) — up to ~16x faster, identical mappings output.
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.
Mirror the
SourceMapBuilderchange in #361 forConcatSourceMapBuilder.What
&'a strinternally (names/sources/source_contents) instead ofCow<'a, str>, so concatenation does no string allocation and holds no owned variant during building.add_sourcemapextends directly from the&'a str-yieldingSourceMapgetters, dropping the per-stringCow::Borrowedwrapping.into_sourcemapwraps inCow::Borrowed(zero copy, as before).into_owned_sourcemapcopies once into a'staticOwnedSourceMap, matching theSourceMapBuildersurface.Compatibility
Backward compatible — fields are
pub(crate)and existing method signatures are unchanged; onlyinto_owned_sourcemapis added.Verified by patching
oxcandrolldownat this branch: both build clean and their sourcemap tests pass (incl. rolldown'ssource_joiner::test_concat_sourcemaps, the realConcatSourceMapBuilderconsumer).