From 372316bf87bf5bd2e0d57c412bd9ead6de6d09ea 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): `ConcatSourceMapBuilder` extend `source_contents` in separate loop (#4634) Small optimization to source map concatenation. Check if input sourcemap has `source_contents` once, rather than on each turn of the loop. --- crates/oxc_sourcemap/src/concat_sourcemap_builder.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/oxc_sourcemap/src/concat_sourcemap_builder.rs b/crates/oxc_sourcemap/src/concat_sourcemap_builder.rs index 120c236ffe211..5ca2f9d9bf7d7 100644 --- a/crates/oxc_sourcemap/src/concat_sourcemap_builder.rs +++ b/crates/oxc_sourcemap/src/concat_sourcemap_builder.rs @@ -46,11 +46,12 @@ impl ConcatSourceMapBuilder { } // Extend `sources` and `source_contents`. - self.sources.reserve(sourcemap.sources.len()); - for (index, source) in sourcemap.get_sources().enumerate() { - let source_content = sourcemap.get_source_content(index as u32).unwrap_or_default(); - self.sources.push(source.into()); - self.source_contents.push(source_content.into()); + 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)); + } else { + self.source_contents.extend((0..sourcemap.sources.len()).map(|_| Arc::default())); } // Extend `names`.