Skip to content

Commit

Permalink
fix: performance degradation for auxiliary files (#6278)
Browse files Browse the repository at this point in the history
* fix: performance issues for auxiliary files

* refactor: no need deconstruction

* fix: chunk ukey will changed before hmr

* perf: reading perf

* fix: cargo lint
  • Loading branch information
SyMind authored Apr 18, 2024
1 parent 4b81c67 commit 9b79e85
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
41 changes: 24 additions & 17 deletions crates/rspack_plugin_devtool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ impl SourceMapDevToolPlugin {
async fn map_assets(
&self,
compilation: &Compilation,
file_to_chunk: &HashMap<String, &Chunk>,
raw_assets: Vec<(String, &CompilationAsset)>,
) -> Result<Vec<MappedAsset>> {
let output_options = &compilation.options.output;
Expand Down Expand Up @@ -396,16 +397,6 @@ impl SourceMapDevToolPlugin {
mapped_buffer.push((filename.to_owned(), code_buffer, source_map_buffer));
}

let mut file_to_chunk: HashMap<String, &Chunk> = HashMap::default();
for chunk in compilation.chunk_by_ukey.values() {
for file in &chunk.files {
file_to_chunk.insert(file.clone(), chunk);
}
for file in &chunk.auxiliary_files {
file_to_chunk.insert(file.clone(), chunk);
}
}

let mut mapped_asstes: Vec<MappedAsset> = Vec::with_capacity(raw_assets.len());
for (filename, code_buffer, source_map_buffer) in mapped_buffer {
let mut asset = compilation
Expand Down Expand Up @@ -551,10 +542,25 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
let logger = compilation.get_logger("rspack.SourceMapDevToolPlugin");
let start = logger.time("collect source maps");

// use to read
let mut file_to_chunk: HashMap<String, &Chunk> = HashMap::default();
// use to write
let mut file_to_chunk_ukey: HashMap<String, ChunkUkey> = HashMap::default();
for chunk in compilation.chunk_by_ukey.values() {
for file in &chunk.files {
file_to_chunk.insert(file.clone(), chunk);
file_to_chunk_ukey.insert(file.clone(), chunk.ukey);
}
for file in &chunk.auxiliary_files {
file_to_chunk.insert(file.clone(), chunk);
file_to_chunk_ukey.insert(file.clone(), chunk.ukey);
}
}

let mapped_asstes = self
.mapped_assets_cache
.use_cache(compilation.assets(), |assets| {
self.map_assets(compilation, assets)
self.map_assets(compilation, &file_to_chunk, assets)
})
.await?;

Expand All @@ -566,18 +572,19 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
let MappedAsset {
asset: (source_filename, mut source_asset),
source_map,
} = mapped_asset.clone();
} = mapped_asset;
if let Some(asset) = compilation.assets_mut().remove(&source_filename) {
source_asset.info = asset.info;
}
compilation.emit_asset(source_filename.to_owned(), source_asset.clone());
if let Some((source_map_filename, source_map_asset)) = source_map {
compilation.emit_asset(source_map_filename.to_owned(), source_map_asset.clone());
compilation.chunk_by_ukey.values_mut().for_each(|v| {
if v.files.contains(&source_filename) {
v.auxiliary_files.insert(source_map_filename.to_owned());
}
});

let chunk_ukey = file_to_chunk_ukey.get(&source_filename);
let chunk = chunk_ukey.map(|ukey| compilation.chunk_by_ukey.expect_get_mut(ukey));
if let Some(chunk) = chunk {
chunk.auxiliary_files.insert(source_map_filename.to_owned());
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_plugin_devtool/src/mapped_assets_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ impl MappedAssetsCache {
let mut mapped_asstes: Vec<MappedAsset> = Vec::with_capacity(assets.len());
let mut vanilla_assets = Vec::with_capacity(assets.len());
for (filename, vanilla_asset) in assets {
if let Some((_, MappedAsset { asset, source_map })) = self.0.remove(filename) {
if let Some((_, mapped_asset)) = self.0.remove(filename) {
if !vanilla_asset.info.version.is_empty()
&& vanilla_asset.info.version == asset.1.info.version
&& vanilla_asset.info.version == mapped_asset.asset.1.info.version
{
mapped_asstes.push(MappedAsset { asset, source_map });
mapped_asstes.push(mapped_asset);
continue;
}
}
Expand Down

3 comments on commit 9b79e85

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs, self-hosted, Linux, ci ❌ failure
_selftest, ubuntu-latest ✅ success
nx, ubuntu-latest ❌ failure
rspress, ubuntu-latest ✅ success
rsbuild, ubuntu-latest ✅ success
compat, ubuntu-latest ✅ success
examples, ubuntu-latest ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-04-18 0a88b52) Current Change
10000_development-mode + exec 2.69 s ± 28 ms 2.71 s ± 14 ms +0.56 %
10000_development-mode_hmr + exec 699 ms ± 7.2 ms 703 ms ± 5.5 ms +0.62 %
10000_production-mode + exec 2.57 s ± 16 ms 2.58 s ± 49 ms +0.19 %
arco-pro_development-mode + exec 2.51 s ± 78 ms 2.5 s ± 76 ms -0.40 %
arco-pro_development-mode_hmr + exec 430 ms ± 2 ms 430 ms ± 3 ms +0.13 %
arco-pro_development-mode_hmr_intercept-plugin + exec 441 ms ± 1.9 ms 442 ms ± 2 ms +0.26 %
arco-pro_development-mode_intercept-plugin + exec 3.3 s ± 63 ms 3.33 s ± 63 ms +0.84 %
arco-pro_production-mode + exec 4.04 s ± 88 ms 4.05 s ± 101 ms +0.32 %
arco-pro_production-mode_intercept-plugin + exec 4.84 s ± 84 ms 4.85 s ± 83 ms +0.34 %
threejs_development-mode_10x + exec 2.06 s ± 21 ms 2.06 s ± 21 ms +0.07 %
threejs_development-mode_10x_hmr + exec 749 ms ± 5.4 ms 749 ms ± 6.1 ms -0.03 %
threejs_production-mode_10x + exec 5.25 s ± 62 ms 5.2 s ± 45 ms -0.85 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs, self-hosted, Linux, ci ❌ failure
_selftest, ubuntu-latest ✅ success
nx, ubuntu-latest ✅ success
rspress, ubuntu-latest ✅ success
rsbuild, ubuntu-latest ✅ success
compat, ubuntu-latest ✅ success
examples, ubuntu-latest ✅ success

Please sign in to comment.