From a3307734c5c2080a9088cd9e3afe348e5c2633d5 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Mon, 5 Aug 2024 01:26:12 +0000 Subject: [PATCH] perf(sourcemap): reduce string copying in `ConcatSourceMapBuilder` (#4638) Clone `Arc`s for source text instead of creating new `Arc`s and copying the string data. For the shorter strings (names and source filenames) it's cheaper to create a new `Arc` than to clone, presumably because of the overhead of atomic operations involved in `Arc::clone`. --- crates/oxc_sourcemap/src/concat_sourcemap_builder.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/oxc_sourcemap/src/concat_sourcemap_builder.rs b/crates/oxc_sourcemap/src/concat_sourcemap_builder.rs index 5ca2f9d9bf7d7..3ed904d26cb7b 100644 --- a/crates/oxc_sourcemap/src/concat_sourcemap_builder.rs +++ b/crates/oxc_sourcemap/src/concat_sourcemap_builder.rs @@ -49,7 +49,10 @@ impl ConcatSourceMapBuilder { self.sources.extend(sourcemap.get_sources().map(Into::into)); if let Some(source_contents) = &sourcemap.source_contents { - self.source_contents.extend(source_contents.iter().map(AsRef::as_ref).map(Into::into)); + // Clone `Arc` instead of generating a new `Arc` and copying string data because + // source texts are generally long strings. Cost of copying a large string is higher + // than cloning an `Arc`. + self.source_contents.extend(source_contents.iter().map(Arc::clone)); } else { self.source_contents.extend((0..sourcemap.sources.len()).map(|_| Arc::default())); }