Skip to content

[codex] Optimize source map streaming hot paths#235

Draft
hardfist wants to merge 4 commits into
mainfrom
codex/callgrind-performance-30
Draft

[codex] Optimize source map streaming hot paths#235
hardfist wants to merge 4 commits into
mainfrom
codex/callgrind-performance-30

Conversation

@hardfist
Copy link
Copy Markdown
Collaborator

@hardfist hardfist commented May 14, 2026

Summary

  • Add a dedicated OriginalSource + ReplaceSource source-map path for column-aware maps.
  • Feed mappings directly into the encoder for the hot OriginalSource case, avoiding the generic chunk callback layer.
  • Add encoder fast paths for common VLQ/no-name mappings and single-line replacement chunks.
  • Keep the non-ASCII and non-OriginalSource behavior on the existing generic path.
  • Keep the earlier borrowed-inner ReplaceSource::source() fast path, ASCII UTF-16 accounting, lazy CachedSourceChunks, and ConcatSource::add() shortcut.

Why

CodSpeed CI did not report an improvement from the previous complex_replace_source_source work because that benchmark is dominated by the unavoidable memory writes for the generated owned string. Local CodSpeed simulation showed Ir improving there, but DL write misses stayed effectively flat.

This update targets complex_replace_source_map instead. The CodSpeed-generated temporary Valgrind profile showed the remaining cost in OriginalSource/ReplaceSource source-map streaming and mappings encoding. For the hot OriginalSource case, we can preserve the same mapping semantics while bypassing the generic Chunks callback path and encoding common no-name mappings directly.

This PR continues to ignore the ReplaceSource::size benchmark as requested.

Local Validation

  • cargo fmt --check
  • cargo test
  • git diff --check
  • cargo check
  • cargo clippy -- -D warnings
  • RUSTDOCFLAGS='-D warnings' cargo doc
  • cargo codspeed build --features codspeed --bench bench
  • codspeed run --mode simulation --skip-upload --repository rstackjs/rspack-sources --profile-folder /tmp/codspeed-current-original-noname -- cargo codspeed run --bench bench

Observed from CodSpeed-generated temporary Valgrind files, using clean baseline 98138ce versus this branch:

  • complex_replace_source_map Ir: 21,239,638 -> 14,279,866 (-32.8%)
  • complex_replace_source_map DL write misses: 6,240 -> 6,191
  • complex_replace_source_map_cached_source_stream_chunks Ir: 11,011,162 -> 11,010,860 (flat after lazy inner chunks)
  • complex_replace_source_source Ir: 1,229,608 -> 789,068 (-35.8%), but this is not the primary CI target because memory writes dominate the modeled time

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 14, 2026

Merging this PR will improve performance by 50%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 12 untouched benchmarks
⏩ 7 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
complex_replace_source_map 9.4 ms 6.2 ms +50%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing codex/callgrind-performance-30 (fcaa1d1) with main (98138ce)

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.

@hardfist hardfist changed the title [codex] Optimize source size and concat hot paths [codex] Optimize ReplaceSource source generation May 14, 2026
@hardfist hardfist changed the title [codex] Optimize ReplaceSource source generation [codex] Optimize source map streaming hot paths May 14, 2026
@SyMind
Copy link
Copy Markdown
Collaborator

SyMind commented May 14, 2026

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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.

2 participants