Skip to content

Conversation

@CPunisher
Copy link
Contributor

@CPunisher CPunisher commented Jan 4, 2026

Summary

This PR optimizes the JavaScript parser by reducing unnecessary cloning of AST nodes, specifically during member expression extraction and info gathering. It introduces a reference-oriented approach using a new ExprRef type to handle AST nodes without taking ownership or cloning.

Key Changes

  • Introduced ExprRef<'ast>: Added a new enum in crates/rspack_plugin_javascript/src/visitors/dependency/parser/ast.rs that holds references to various SWC AST expression types.
  • Refactored Parser Methods:
    • Updated get_member_expression_info, extract_member_expression_chain, and get_member_expression_info_from_expr to accept ExprRef<'ast> or &'ast Expr instead of owned Expr.
    • These methods now return types with lifetimes ('ast) that reference the original AST.
  • Optimized Data Structures:
    • ExtractedMemberExpressionChainData, MemberExpressionInfo, and CallExpressionInfo now use lifetime parameters to hold references to AST nodes (e.g., &'ast CallExpr) instead of owned copies.
  • Removed clone() and take(): Replaced expensive expr.clone() and expr.obj.take() calls with reference-based lookups in the visitor's hot path (walk.rs).
  • Trait Implementations: Added RootName implementation for ExprRef<'_> to support root name extraction from references.

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@netlify
Copy link

netlify bot commented Jan 4, 2026

Deploy Preview for rspack canceled.

Name Link
🔨 Latest commit 26f66fa
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/695b4cb3b99e7100089df974

@github-actions github-actions bot added team The issue/pr is created by the member of Rspack. release: refactor labels Jan 4, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2026

Rsdoctor Bundle Diff Analysis

Found 5 projects in monorepo, 0 projects with changes.

📊 Quick Summary
Project Total Size Change
react-10k 5.7 MB 0
react-1k 825.4 KB 0
react-5k 2.7 MB 0
rome 984.3 KB 0
ui-components 2.1 MB 0

Generated by Rsdoctor GitHub Action

@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2026

📝 Benchmark detail: Open

Name Base (2026-01-04 11c4d87) Current Change
10000_big_production-mode_disable-minimize + exec 23.6 s ± 533 ms 23.6 s ± 631 ms -0.18 %
10000_development-mode + exec 1.31 s ± 25 ms 1.27 s ± 17 ms -2.53 %
10000_development-mode_hmr + stats 228 ms ± 4.9 ms 230 ms ± 24 ms +0.94 %
10000_development-mode_noop-loader + exec 2.22 s ± 80 ms 2.19 s ± 166 ms -1.47 %
10000_production-mode + exec 1.33 s ± 41 ms 1.32 s ± 45 ms -0.87 %
10000_production-mode_persistent-cold + exec 1.48 s ± 17 ms 1.46 s ± 26 ms -1.40 %
10000_production-mode_persistent-hot + exec 1.03 s ± 21 ms 1.02 s ± 31 ms -1.13 %
arco-pro_development-mode + exec 1.57 s ± 98 ms 1.44 s ± 115 ms -8.34 %
arco-pro_development-mode_hmr + stats 35 ms ± 0.37 ms 35 ms ± 0.72 ms -0.21 %
arco-pro_production-mode + exec 2.89 s ± 114 ms 2.8 s ± 27 ms -3.39 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 2.98 s ± 120 ms 2.87 s ± 210 ms -3.77 %
arco-pro_production-mode_persistent-cold + exec 2.99 s ± 60 ms 2.92 s ± 104 ms -2.36 %
arco-pro_production-mode_persistent-hot + exec 1.66 s ± 23 ms 1.59 s ± 46 ms -3.94 %
arco-pro_production-mode_traverse-chunk-modules + exec 2.96 s ± 98 ms 2.8 s ± 60 ms -5.39 %
large-dyn-imports_development-mode + exec 1.62 s ± 46 ms 1.58 s ± 36 ms -2.21 %
large-dyn-imports_production-mode + exec 1.75 s ± 23 ms 1.71 s ± 44 ms -2.67 %
threejs_development-mode_10x + exec 1.32 s ± 19 ms 1.26 s ± 26 ms -4.68 %
threejs_development-mode_10x_hmr + stats 239 ms ± 5.2 ms 236 ms ± 16 ms -1.26 %
threejs_production-mode_10x + exec 3.92 s ± 219 ms 3.82 s ± 21 ms -2.57 %
threejs_production-mode_10x_persistent-cold + exec 4.02 s ± 27 ms 4.02 s ± 216 ms +0.14 %
threejs_production-mode_10x_persistent-hot + exec 3.5 s ± 67 ms 3.47 s ± 159 ms -1.00 %
10000_big_production-mode_disable-minimize + rss memory 5334 MiB ± 69.9 MiB 5383 MiB ± 122 MiB +0.91 %
10000_development-mode + rss memory 589 MiB ± 18.6 MiB 583 MiB ± 28.2 MiB -1.08 %
10000_development-mode_hmr + rss memory 753 MiB ± 13 MiB 742 MiB ± 11.7 MiB -1.42 %
10000_development-mode_noop-loader + rss memory 865 MiB ± 19.4 MiB 874 MiB ± 17.5 MiB +1.09 %
10000_production-mode + rss memory 614 MiB ± 49.6 MiB 609 MiB ± 28.8 MiB -0.92 %
10000_production-mode_persistent-cold + rss memory 713 MiB ± 22.1 MiB 711 MiB ± 17.3 MiB -0.32 %
10000_production-mode_persistent-hot + rss memory 692 MiB ± 29 MiB 689 MiB ± 49.5 MiB -0.48 %
arco-pro_development-mode + rss memory 475 MiB ± 11.8 MiB 508 MiB ± 42.8 MiB +6.95 %
arco-pro_development-mode_hmr + rss memory 394 MiB ± 11 MiB 439 MiB ± 9.79 MiB +11.49 %
arco-pro_production-mode + rss memory 593 MiB ± 48.9 MiB 632 MiB ± 31.5 MiB +6.62 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 633 MiB ± 59.9 MiB 648 MiB ± 36.4 MiB +2.41 %
arco-pro_production-mode_persistent-cold + rss memory 664 MiB ± 19.6 MiB 698 MiB ± 33 MiB +5.15 %
arco-pro_production-mode_persistent-hot + rss memory 506 MiB ± 75.5 MiB 491 MiB ± 55.8 MiB -2.97 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 601 MiB ± 66.2 MiB 646 MiB ± 56.3 MiB +7.51 %
large-dyn-imports_development-mode + rss memory 590 MiB ± 11.6 MiB 592 MiB ± 7.51 MiB +0.29 %
large-dyn-imports_production-mode + rss memory 515 MiB ± 7.4 MiB 515 MiB ± 11.3 MiB -0.04 %
threejs_development-mode_10x + rss memory 524 MiB ± 14.2 MiB 524 MiB ± 19.9 MiB -0.01 %
threejs_development-mode_10x_hmr + rss memory 770 MiB ± 23.7 MiB 769 MiB ± 55.3 MiB -0.12 %
threejs_production-mode_10x + rss memory 691 MiB ± 129 MiB 696 MiB ± 116 MiB +0.70 %
threejs_production-mode_10x_persistent-cold + rss memory 758 MiB ± 36.7 MiB 743 MiB ± 25.3 MiB -1.98 %
threejs_production-mode_10x_persistent-hot + rss memory 602 MiB ± 22.4 MiB 587 MiB ± 30 MiB -2.49 %

@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2026

📦 Binary Size-limit

Comparing 26f66fa to refactor: simplify export info types by removing MaybeDynamicTargetExportInfo (#12601) by harpsealjs

🎉 Size decreased by 2.75KB from 47.87MB to 47.87MB (⬇️0.01%)

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 4, 2026

CodSpeed Performance Report

Merging #12622 will not alter performance

Comparing 01-04-refactor/expr-ref (26f66fa) with main (b7f85d2)

Summary

✅ 16 untouched
⏩ 1 skipped1

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports.

@CPunisher CPunisher requested a review from ahabhgk January 5, 2026 05:25
@CPunisher CPunisher marked this pull request as ready for review January 5, 2026 05:25
Copilot AI review requested due to automatic review settings January 5, 2026 05:25
Copy link
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 optimizes the JavaScript parser by eliminating expensive cloning operations on AST nodes. It introduces a reference-oriented approach using a new ExprRef<'ast> type that holds references to SWC AST expression types instead of taking ownership, significantly reducing memory allocations and improving performance during AST traversal and dependency analysis.

Key Changes

  • Introduced ExprRef<'ast> enum to hold references to various SWC AST expression types
  • Refactored parser methods to accept references instead of owned values, eliminating clone() and take() calls
  • Updated data structures with lifetime parameters to support reference-based operations

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/rspack_plugin_javascript/src/visitors/dependency/parser/ast.rs New file introducing the ExprRef<'ast> enum with all expression variants and From trait implementation for conversion from &Expr
crates/rspack_plugin_javascript/src/visitors/dependency/parser/mod.rs Updated method signatures to use ExprRef<'ast> and lifetime parameters; modified data structures to hold references; removed util::take::Take import; added RootName trait implementation for ExprRef
crates/rspack_plugin_javascript/src/visitors/dependency/parser/walk.rs Replaced expensive clone() calls with ExprRef references in hot path methods; updated function call sites to pass references instead of clones
crates/rspack_plugin_javascript/src/visitors/dependency/parser/call_hooks_name.rs Updated to use ExprRef::Member instead of cloning expressions
crates/rspack_plugin_javascript/src/visitors/dependency/mod.rs Added export of ast::* to make ExprRef publicly available
crates/rspack_plugin_javascript/src/utils/eval/eval_member_expr.rs Updated to use ExprRef::Member instead of cloning member expressions
crates/rspack_plugin_javascript/src/parser_plugin/url_plugin.rs Refactored is_meta_url to use ExprRef and pattern matching instead of method chaining
crates/rspack_plugin_javascript/src/parser_plugin/import_meta_plugin.rs Updated to use ExprRef::Member instead of cloning expressions
Comments suppressed due to low confidence (1)

crates/rspack_plugin_javascript/src/visitors/dependency/parser/mod.rs:90

  • The updated ExtractedMemberExpressionChainData struct now includes a lifetime parameter but lacks updated documentation explaining the lifetime and how it relates to the new reference-based approach. Since this is a public API that has changed significantly, the documentation should clarify that the object field now holds a reference to the AST rather than an owned copy.
#[derive(Debug)]
pub struct ExtractedMemberExpressionChainData<'ast> {
  pub object: ExprRef<'ast>,
  pub members: Vec<Atom>,
  pub members_optionals: Vec<bool>,
  pub member_ranges: Vec<Span>,
}

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

@CPunisher CPunisher enabled auto-merge (squash) January 5, 2026 06:06
@CPunisher CPunisher merged commit ce45c6f into main Jan 5, 2026
77 of 79 checks passed
@CPunisher CPunisher deleted the 01-04-refactor/expr-ref branch January 5, 2026 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: refactor team The issue/pr is created by the member of Rspack.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants