Optimize string concatenation in hot paths
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 usepush_str()instead of+=operator when building single-character regex patterns -
dependencies_block.rs: Replacefoldwith+=with idiomaticmap().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() == 1could use early termination - Some
HashMap/HashSetinstances 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.
Deploy Preview for rspack canceled.
| Name | Link |
|---|---|
| Latest commit | be810f7eb2d0dbeb11c7e927ded39849700d8a37 |
| Latest deploy log | https://app.netlify.com/projects/rspack/deploys/6905e143e1046600089f403c |
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.
📦 Binary Size-limit
Comparing
be810f7to chore(deps): update dependency @biomejs/biome to ^2.3.2 (#12055) by renovate[bot]
🙈 Size remains the same at 47.85MB
CodSpeed Performance Report
Merging #12058 will not alter performance
Comparing copilot/identify-inefficient-code (be810f7) with main (d808219)
Summary
✅ 17 untouched