From 82da8525da746aa939194cedd6dc90278cb05379 Mon Sep 17 00:00:00 2001 From: Fy <1114550440@qq.com> Date: Thu, 23 Apr 2026 17:33:47 +0800 Subject: [PATCH] perf(concat_source): skip Mutex acquire in add() under &mut self MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ConcatSource::add` takes `&mut self`, which already guarantees exclusive access to the inner children Vec. Locking the Mutex was a redundant atomic acquire/release on a hot construction path — rspack's JS code generation calls `add` hundreds of times per chunk (e.g. concatenated module rendering, runtime module assembly, parallel rayon folds). Switch to `Mutex::get_mut()` which is the standard zero-overhead alternative when exclusive access is statically known. All other read-only accessors (`Clone`, `Debug`, `optimized_children`) still acquire the lock. --- src/concat_source.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/concat_source.rs b/src/concat_source.rs index e99af85f..4e77729a 100644 --- a/src/concat_source.rs +++ b/src/concat_source.rs @@ -127,7 +127,14 @@ impl ConcatSource { /// Add a [Source] to concat. pub fn add(&mut self, source: S) { - let children = &mut *self.children.lock().unwrap(); + // `&mut self` guarantees exclusive access, so the Mutex doesn't need to be + // locked here — `get_mut` skips the atomic acquire/release. This matters + // because `add` is called many hundreds of times per chunk in rspack's + // code-generation hot path. + let children = self + .children + .get_mut() + .expect("ConcatSource children mutex poisoned"); if let Some(optimized_children) = self.is_optimized.take() { *children = optimized_children;