Skip to content

perf(core): defer top-level declaration flattening#13662

Draft
LingyuCoder wants to merge 4 commits intomainfrom
codex/top-level-declarations-multiple
Draft

perf(core): defer top-level declaration flattening#13662
LingyuCoder wants to merge 4 commits intomainfrom
codex/top-level-declarations-multiple

Conversation

@LingyuCoder
Copy link
Copy Markdown
Contributor

Summary

  • replace eager top_level_declarations flattening with a delayed TopLevelDeclarations representation
  • keep Unknown semantics while allowing concatenated modules to carry multiple declaration sets without merging them eagerly
  • update fallback consumers to query declaration knowledge and membership without forcing a flatten step

Why

The previous concatenated-module path eagerly merged large top-level declaration sets during build, which added noticeable cost to the hot path. This change defers flattening until it is actually needed and lets most callers operate on lightweight contains/is_unknown checks.

Impact

  • reduces eager work in ConcatenatedModule::build
  • preserves existing fallback behavior for library/runtime checks
  • keeps code generation as the source of truth for flattened top-level declarations

Validation

  • cargo test -p rspack_core top_level_declarations --lib
  • cargo test -p rspack_core merge_concatenated_build_partials --lib
  • cargo check -p rspack_core -p rspack_plugin_javascript -p rspack_plugin_library -p rspack_plugin_rsc -p rspack_plugin_mf
  • cargo fmt --all --check
  • pnpm run build:binding:dev is blocked in this shell by the local Node.js version (v18.12.1); the branch is being published per user request after local verification

@github-actions github-actions Bot added the team The issue/pr is created by the member of Rspack. label Apr 9, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 9, 2026

⏳ Triggered benchmark: Open

@LingyuCoder LingyuCoder changed the title [codex] perf(core): defer top-level declaration flattening perf(core): defer top-level declaration flattening Apr 9, 2026
@github-actions github-actions Bot added the release: performance release: performance related release(mr only) label Apr 9, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 9, 2026

Merging this PR will degrade performance by 5.12%

🎉 Hooray! codspeed-rust just leveled up to 4.4.1!

A heads-up, this is a breaking change and it might affect your current performance baseline a bit. But here's the exciting part - it's packed with new, cool features and promises improved result stability 🥳!
Curious about what's new? Visit our releases page to delve into all the awesome details about this new version.

⚡ 3 improved benchmarks
❌ 2 regressed benchmarks
✅ 13 untouched benchmarks
🆕 1 new benchmark
⏩ 19 skipped benchmarks1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation rust@persistent_cache_restore@basic-react-development 26.7 ms 26.1 ms +2.33%
Simulation rust@persistent_cache_restore_after_single_file_change@basic-react-development 28.1 ms 27.5 ms +2.28%
🆕 Simulation rust@scan_dependencies@three_module N/A 23.7 ms N/A
Simulation rust@concatenate_module_code_generation 137.1 ms 135 ms +1.54%
Simulation rust@create_concatenate_module 42.4 ms 44.6 ms -5.12%
Simulation rust@create_chunk_ids 10.6 ms 10.9 ms -2.93%

Comparing codex/top-level-declarations-multiple (b1a5b28) with main (6c9c5af)

Open in CodSpeed

Footnotes

  1. 19 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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 9, 2026

Rsdoctor Bundle Diff Analysis

⚠️ Note: The latest commit (071af69073) does not have baseline artifacts. Using commit 5e2adb3ffe for baseline comparison instead. If this seems incorrect, please wait a few minutes and try rerunning the workflow.

Found 6 projects in monorepo, 0 projects with changes.

📊 Quick Summary
Project Total Size Change
popular-libs 1.7 MB 0
react-10k 5.7 MB 0
react-1k 826.2 KB 0
react-5k 2.7 MB 0
rome 984.1 KB 0
ui-components 4.9 MB 0

Generated by Rsdoctor GitHub Action

@LingyuCoder LingyuCoder marked this pull request as ready for review April 9, 2026 07:28
@LingyuCoder LingyuCoder requested a review from ahabhgk as a code owner April 9, 2026 07:28
Copilot AI review requested due to automatic review settings April 9, 2026 07:28
@github-actions

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves concatenated-module build performance by deferring eager flattening of top_level_declarations into a delayed TopLevelDeclarations representation, while updating downstream consumers to query “unknown/contains” without forcing a flatten step.

Changes:

  • Introduce TopLevelDeclarations (Unknown/Single/Multiple) and migrate BuildInfo.top_level_declarations to it.
  • Update concatenated-module build to merge declaration sets without eager flattening (and parallelize some collection work).
  • Update plugin consumers (library/runtime checks, JS parser meta info, inline-startup checks) to use is_unknown()/contains() APIs.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
crates/rspack_core/src/module.rs Adds TopLevelDeclarations and migrates BuildInfo.top_level_declarations to the new representation, plus unit tests.
crates/rspack_core/src/concatenated_module.rs Refactors concatenated-module build to merge partials (including deferred top-level decls) and adds tests for merge behavior.
crates/rspack_core/src/external_module.rs Initializes external modules with TopLevelDeclarations::Single(Default::default()).
crates/rspack_plugin_javascript/src/parser_plugin/javascript_meta_info_plugin.rs Writes parser-discovered top-level decls via TopLevelDeclarations APIs.
crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs Optimizes connection collection with parallel iteration; minor chunk-graph loop optimization.
crates/rspack_plugin_javascript/src/plugin/mod.rs Updates inline-startup bailout logic to treat “unknown” top-level decls correctly.
crates/rspack_plugin_library/src/assign_library_plugin.rs Updates runtime bailout logic to use TopLevelDeclarations semantics.
crates/rspack_plugin_mf/src/container/container_entry_module.rs Initializes MF container entry modules with TopLevelDeclarations::Single(Default::default()).
crates/rspack_plugin_rsc/src/rsc_entry_module.rs Initializes RSC entry modules with TopLevelDeclarations::Single(Default::default()).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/rspack_core/src/module.rs
Comment thread crates/rspack_core/src/concatenated_module.rs Outdated
Comment thread crates/rspack_plugin_javascript/src/plugin/mod.rs Outdated
Comment thread crates/rspack_plugin_library/src/assign_library_plugin.rs Outdated
Copy link
Copy Markdown

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

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: 04518a4a16

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

Comment thread crates/rspack_core/src/module.rs
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 9, 2026

📦 Binary Size-limit

Comparing b1a5b28 to chore(ci): add node 24 to release canary and debug workflows (#13656) by pshu

❌ Size increased by 28.66KB from 49.35MB to 49.37MB (⬆️0.06%)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 9, 2026

📝 Benchmark detail: Open

Name Base (4d38fae) Current Change
10000_big_production-mode_disable-minimize + exec 14 s ± 197 ms 14.1 s ± 118 ms +0.59 %
10000_development-mode + exec 954 ms ± 45 ms 968 ms ± 63 ms +1.47 %
10000_development-mode_hmr + stats 177 ms ± 5.5 ms 178 ms ± 12 ms +0.25 %
10000_development-mode_noop-loader + exec 1.99 s ± 177 ms 1.97 s ± 70 ms -0.95 %
10000_production-mode + exec 1.06 s ± 52 ms 1.07 s ± 32 ms +1.49 %
10000_production-mode_persistent-cold + exec 1.24 s ± 35 ms 1.24 s ± 24 ms +0.11 %
10000_production-mode_persistent-hot + exec 908 ms ± 11 ms 918 ms ± 24 ms +1.10 %
10000_production-mode_source-map + exec 1.23 s ± 22 ms 1.24 s ± 17 ms +0.44 %
arco-pro_development-mode + exec 1.33 s ± 87 ms 1.34 s ± 66 ms +0.44 %
arco-pro_development-mode_hmr + stats 37 ms ± 0.88 ms 37 ms ± 1.3 ms +0.94 %
arco-pro_production-mode + exec 2.47 s ± 82 ms 2.51 s ± 101 ms +1.84 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 2.53 s ± 79 ms 2.58 s ± 92 ms +1.90 %
arco-pro_production-mode_persistent-cold + exec 2.55 s ± 35 ms 2.57 s ± 76 ms +0.87 %
arco-pro_production-mode_persistent-hot + exec 1.38 s ± 21 ms 1.38 s ± 41 ms -0.26 %
arco-pro_production-mode_source-map + exec 2.89 s ± 41 ms 2.9 s ± 90 ms +0.28 %
arco-pro_production-mode_traverse-chunk-modules + exec 2.5 s ± 36 ms 2.5 s ± 103 ms -0.01 %
bundled-threejs_development-mode + exec 181 ms ± 3.6 ms 181 ms ± 3.3 ms 0.00 %
bundled-threejs_production-mode + exec 210 ms ± 6.4 ms 214 ms ± 8.2 ms +2.29 %
large-dyn-imports_development-mode + exec 1.18 s ± 30 ms 1.19 s ± 30 ms +0.66 %
large-dyn-imports_production-mode + exec 1.31 s ± 11 ms 1.34 s ± 40 ms +2.80 %
threejs_development-mode_10x + exec 828 ms ± 11 ms 827 ms ± 32 ms -0.05 %
threejs_development-mode_10x_hmr + stats 119 ms ± 7.5 ms 120 ms ± 6.7 ms +0.87 %
threejs_production-mode_10x + exec 3.02 s ± 17 ms 3.05 s ± 155 ms +0.79 %
threejs_production-mode_10x_persistent-cold + exec 3.18 s ± 149 ms 3.18 s ± 150 ms +0.20 %
threejs_production-mode_10x_persistent-hot + exec 2.54 s ± 30 ms 2.54 s ± 35 ms +0.06 %
threejs_production-mode_10x_source-map + exec 3.79 s ± 30 ms 3.79 s ± 31 ms 0.00 %
10000_big_production-mode_disable-minimize + rss memory 1970 MiB ± 49.6 MiB 1941 MiB ± 34.7 MiB -1.50 %
10000_development-mode + rss memory 576 MiB ± 3.36 MiB 575 MiB ± 8.47 MiB -0.17 %
10000_development-mode_hmr + rss memory 801 MiB ± 40.8 MiB 813 MiB ± 27.7 MiB +1.52 %
10000_development-mode_noop-loader + rss memory 868 MiB ± 25 MiB 864 MiB ± 7.21 MiB -0.45 %
10000_production-mode + rss memory 507 MiB ± 14.7 MiB 513 MiB ± 19 MiB +1.25 %
10000_production-mode_persistent-cold + rss memory 690 MiB ± 8.38 MiB 691 MiB ± 8.43 MiB +0.27 %
10000_production-mode_persistent-hot + rss memory 666 MiB ± 5.41 MiB 672 MiB ± 15.3 MiB +0.92 %
10000_production-mode_source-map + rss memory 535 MiB ± 7.74 MiB 538 MiB ± 12.9 MiB +0.61 %
arco-pro_development-mode + rss memory 475 MiB ± 5.74 MiB 475 MiB ± 2.43 MiB -0.02 %
arco-pro_development-mode_hmr + rss memory 491 MiB ± 15.5 MiB 495 MiB ± 7.49 MiB +0.67 %
arco-pro_production-mode + rss memory 656 MiB ± 12.3 MiB 654 MiB ± 28.7 MiB -0.27 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 650 MiB ± 52.1 MiB 677 MiB ± 9.59 MiB +4.14 %
arco-pro_production-mode_persistent-cold + rss memory 722 MiB ± 13.1 MiB 725 MiB ± 9.42 MiB +0.51 %
arco-pro_production-mode_persistent-hot + rss memory 527 MiB ± 9.59 MiB 524 MiB ± 29.1 MiB -0.60 %
arco-pro_production-mode_source-map + rss memory 752 MiB ± 20.4 MiB 750 MiB ± 12.4 MiB -0.36 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 651 MiB ± 9.56 MiB 659 MiB ± 8.76 MiB +1.16 %
bundled-threejs_development-mode + rss memory 160 MiB ± 6.83 MiB 159 MiB ± 6.94 MiB -0.45 %
bundled-threejs_production-mode + rss memory 178 MiB ± 3.79 MiB 184 MiB ± 9.19 MiB +3.45 %
large-dyn-imports_development-mode + rss memory 562 MiB ± 20.8 MiB 568 MiB ± 14.9 MiB +1.03 %
large-dyn-imports_production-mode + rss memory 441 MiB ± 12.4 MiB 449 MiB ± 5.17 MiB +1.81 %
threejs_development-mode_10x + rss memory 515 MiB ± 16.7 MiB 528 MiB ± 13.2 MiB +2.41 %
threejs_development-mode_10x_hmr + rss memory 666 MiB ± 27.5 MiB 668 MiB ± 16.1 MiB +0.20 %
threejs_production-mode_10x + rss memory 685 MiB ± 17.3 MiB 670 MiB ± 122 MiB -2.20 %
threejs_production-mode_10x_persistent-cold + rss memory 806 MiB ± 14.9 MiB 821 MiB ± 6.46 MiB +1.90 %
threejs_production-mode_10x_persistent-hot + rss memory 579 MiB ± 15.9 MiB 582 MiB ± 9.67 MiB +0.66 %
threejs_production-mode_10x_source-map + rss memory 783 MiB ± 36.8 MiB 794 MiB ± 24.2 MiB +1.48 %

@LingyuCoder LingyuCoder marked this pull request as draft April 9, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: performance release: performance related release(mr only) team The issue/pr is created by the member of Rspack.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants