Skip to content

perf(hmr): cache chunk versions and avoid redundant allocations - #16673

Merged
JSerFeng merged 1 commit into
mainfrom
perf/hmr-chunk-versions
Nov 22, 2026
Merged

JSerFeng merged 1 commit into
mainfrom
perf/hmr-chunk-versions

Conversation

@JSerFeng

@JSerFeng JSerFeng commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

HotModuleReplacementChunkVersionsRuntimeModule (#16360) attaches to every runtime chunk and has dependent_hash() == true alongside HMR's full_hash runtime module. Previously, each runtime chunk recomputed versions() up to 5–6 times per compilation (create_hash runtime pass, create_hash full-hash pass, runtime_modules_code_generation, CreateChunkAssetsPass, generate_update, and get_update_data) without caching:

  • Every call traversed all reachable chunks via get_all_referenced_chunks, rebuilt a fresh BTreeMap<ChunkId, String>, allocated a heap String per version, and re-serialized the entire 2D JSON array in generate().
  • Every is_versioned() check allocated and discarded a full Vec<ModuleIdentifier> via get_chunk_modules_identifier_by_source_type(...).is_empty().
  • get_hmr_chunk_version callers duplicated ChunkKind::HotUpdate and RuntimeGlobals::HMR_CHUNK_VERSIONS guards across chunk formatters and always queried cgc_runtime_requirements_artifact, even in non-HMR builds.

Changes

  • Fast runtime-chunk check: Add BuildChunkGraphArtifact::is_runtime_chunk backed by the precomputed runtime_chunks set (falling back to Chunk::has_runtime) and reuse it across ChunkGroup::get_loading_chunks, is_versioned, get_hmr_chunk_version, and HotModuleReplacementChunkVersionsRuntimeModule.
  • Unified O(1) version lookup: Fold !compilation.hot, ChunkKind::HotUpdate, is_runtime_chunk, and RuntimeGlobals::HMR_CHUNK_VERSIONS guards into get_hmr_chunk_version so chunk formatters share one entry point and non-HMR builds return immediately without map lookups.
  • Zero-allocation JS module check: Move is_versioned to additional_chunk_runtime_requirements (its sole remaining caller) and replace get_chunk_modules_identifier_by_source_type(...).is_empty() with the short-circuiting has_chunk_module_by_source_type.
  • Cached reachable chunks and versions:
    • Pre-filter (excluding runtime and hot-update chunks) and pre-sort reachable versioned (ChunkId, ChunkUkey) pairs once per HotModuleReplacementChunkVersionsRuntimeModule in a OnceLock<Box<[(ChunkId, ChunkUkey)]>>.
    • Read JS content hashes directly from chunk_hashes_artifact, store versions in a compact sorted Arc<ChunkVersions> (Box<[(ChunkId, SmolStr)]>), and validate cached versions without allocation.
    • Memoize the rendered initial JS table under a single mutex lock across hash and asset passes, and share the Arc<ChunkVersions> directly in get_update_data().
    • Replace BTreeMap lookups in generate_update() with an O(C) two-pointer linear merge and direct JSON string formatting to avoid per-entry temporary String allocations.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T12:00:38.235385Z 88851c0 New commits
🔒 Security Review ✅ Completed 2026-11-19T12:03:12.519718Z 88851c0 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: afc635ccef

ℹ️ 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".

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

📦 Binary Size-limit

Comparing 88851c0 to fix(html): include the filename in HTML parse errors (#16737) by Jiahan Chen

❌ Size increased by 8.00KB from 67.73MB to 67.74MB (⬆️0.01%)

@github-actions

Copy link
Copy Markdown
Contributor

Rsdoctor Bundle Diff Analysis

Found 5 projects in monorepo, 0 projects with changes.

📊 Quick Summary
Project Total Size Gzip Size Change Gzip Change
popular-libs 1.7 MB 545.2 KB 0 0
react-10k 5.4 MB 1.3 MB 0 0
react-1k 827.5 KB 225.2 KB 0 0
react-5k 2.6 MB 670.6 KB 0 0
ui-components 4.7 MB 1.4 MB 0 0

Generated by Rsdoctor GitHub Action

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 57 untouched benchmarks
⏩ 47 skipped benchmarks1


Comparing perf/hmr-chunk-versions (88851c0) with main (109b40d)

Open in CodSpeed

Footnotes

  1. 47 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. ↩

@JSerFeng
JSerFeng force-pushed the perf/hmr-chunk-versions branch from afc635c to 88851c0 Compare November 19, 2026 11:59

@LingyuCoder LingyuCoder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@JSerFeng
JSerFeng merged commit 53a62a5 into main Nov 22, 2026
53 checks passed
@JSerFeng
JSerFeng deleted the perf/hmr-chunk-versions branch November 22, 2026 18:24
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