rspack icon indicating copy to clipboard operation
rspack copied to clipboard

Optimize string concatenation in hot paths

Open Copilot opened this issue 4 months ago • 4 comments

Identified and fixed inefficient string concatenation patterns causing unnecessary allocations in frequently-executed code paths.

Changes

  • compile_boolean_matcher.rs: Pre-allocate string capacity and use push_str() instead of += operator when building single-character regex patterns
  • dependencies_block.rs: Replace fold with += with idiomatic map().collect::<String>() pattern for dependency ID generation
  • eval/mod.rs: Replace manual loop with iterator-based collection for template string evaluation

Example

Before:

let mut single_char_items: String = String::new();
for item in items {
  if item.chars().count() == 1 {
    single_char_items += &item;  // Repeated allocations
  }
}

After:

let mut single_char_items = String::with_capacity(count_of_single_char_items);
for item in items {
  if item.chars().count() == 1 {
    single_char_items.push_str(&item);  // No reallocation
  }
}

Additional Observations

Minor optimization opportunities remain but are out of scope for this focused change:

  • .chars().count() == 1 could use early termination
  • Some HashMap/HashSet instances lack capacity hints
Original prompt

Identify and suggest improvements to slow or inefficient code


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Nov 01 '25 10:11 Copilot

Deploy Preview for rspack canceled.

Name Link
Latest commit be810f7eb2d0dbeb11c7e927ded39849700d8a37
Latest deploy log https://app.netlify.com/projects/rspack/deploys/6905e143e1046600089f403c

netlify[bot] avatar Nov 01 '25 10:11 netlify[bot]

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Nov 01 '25 10:11 CLAassistant

📦 Binary Size-limit

Comparing be810f7 to chore(deps): update dependency @biomejs/biome to ^2.3.2 (#12055) by renovate[bot]

🙈 Size remains the same at 47.85MB

github-actions[bot] avatar Nov 03 '25 00:11 github-actions[bot]

CodSpeed Performance Report

Merging #12058 will not alter performance

Comparing copilot/identify-inefficient-code (be810f7) with main (d808219)

Summary

✅ 17 untouched

codspeed-hq[bot] avatar Nov 03 '25 00:11 codspeed-hq[bot]