Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve cached source data struct #8602

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ rayon = { version = "1.10.0" }
regex = { version = "1.10.4" }
ropey = "1.6.1"
rspack_resolver = { version = "0.3.5", features = ["package_json_raw_json_api"] }
rspack_sources = { version = "=0.3.4" }
rspack_sources = { version = "=0.3.5" }
rustc-hash = { version = "1.1.0" }
serde = { version = "1.0.197" }
serde_json = { version = "1.0.115" }
Expand Down
7 changes: 3 additions & 4 deletions crates/rspack_loader_lightningcss/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,21 @@ impl LightningCssLoader {
}),
}));
let rspack_source_map = SourceMap::new(
None,
mappings,
source_map
.get_sources()
.iter()
.map(|source| source.to_string().into())
.map(ToString::to_string)
.collect::<Vec<_>>(),
source_map
.get_sources_content()
.iter()
.map(|source| source.to_string().into())
.map(ToString::to_string)
.collect::<Vec<_>>(),
source_map
.get_names()
.iter()
.map(|source| source.to_string().into())
.map(ToString::to_string)
.collect::<Vec<_>>(),
);
loader_context.finish_with((content.code, rspack_source_map));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, hash::Hash};
use std::hash::Hash;

use dashmap::DashMap;
use derivative::Derivative;
Expand Down Expand Up @@ -100,8 +100,7 @@ fn eval_source_map_devtool_plugin_render_module_content(
let source = &origin_source.source();

{
let sources = map.sources_mut();
let modules = sources.iter().map(|source| {
let modules = map.sources().iter().map(|source| {
if let Some(stripped) = source.strip_prefix("webpack://") {
let source = make_paths_absolute(compilation.options.context.as_str(), stripped);
let identifier = ModuleIdentifier::from(source.as_str());
Expand Down Expand Up @@ -144,25 +143,16 @@ fn eval_source_map_devtool_plugin_render_module_content(
.collect::<Result<Vec<_>>>()?
}
};
let mut module_filenames =
let module_filenames =
ModuleFilenameHelpers::replace_duplicates(module_filenames, |mut filename, _, n| {
filename.extend(std::iter::repeat('*').take(n));
filename
})
.into_iter()
.map(Some)
.collect::<Vec<Option<_>>>();
for (i, source) in sources.iter_mut().enumerate() {
if let Some(filename) = module_filenames[i].take() {
*source = Cow::from(filename);
}
}
});
map.set_sources(module_filenames);
}

if self.no_sources {
for content in map.sources_content_mut() {
*content = Cow::from(String::default());
}
map.set_sources_content([]);
}
map.set_source_root(self.source_root.clone());
map.set_file(Some(module.identifier().to_string()));
Expand Down
36 changes: 19 additions & 17 deletions crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::{Component, PathBuf};
use std::sync::LazyLock;
use std::sync::{Arc, LazyLock};
use std::{borrow::Cow, path::Path};

use cow_utils::CowUtils;
Expand Down Expand Up @@ -114,7 +114,7 @@ pub struct SourceMapDevToolPlugin {
no_sources: bool,
public_path: Option<String>,
module: bool,
source_root: Option<String>,
source_root: Option<Arc<str>>,
test: Option<AssetConditions>,
include: Option<AssetConditions>,
exclude: Option<AssetConditions>,
Expand Down Expand Up @@ -178,7 +178,7 @@ impl SourceMapDevToolPlugin {
options.no_sources,
options.public_path,
options.module,
options.source_root,
options.source_root.map(Arc::from),
options.test,
options.include,
options.exclude,
Expand Down Expand Up @@ -340,21 +340,23 @@ impl SourceMapDevToolPlugin {
if let Some(source_map) = source_map {
source_map.set_file(Some(filename.clone()));

let sources = source_map.sources_mut();
for source in sources {
let module_or_source = source_map_modules
.get(source.as_ref())
.expect("expected a module or source");
let source_name = module_to_source_name
.get(module_or_source)
.expect("expected a filename at the given index but found None")
.clone();
*source = Cow::from(source_name);
}
source_map.set_sources(
source_map
.sources()
.iter()
.map(|source| {
let module_or_source = source_map_modules
.get(source)
.expect("expected a module or source");
module_to_source_name
.get(module_or_source)
.expect("expected a filename at the given index but found None")
.clone()
})
.collect::<Vec<_>>(),
);
if self.no_sources {
for content in source_map.sources_content_mut() {
*content = Default::default();
}
source_map.set_sources_content([]);
}
if let Some(source_root) = &self.source_root {
source_map.set_source_root(Some(source_root.clone()));
Expand Down
15 changes: 9 additions & 6 deletions crates/rspack_plugin_javascript/src/ast/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,25 @@ pub fn print(
},
}));

Some(rspack_sources::SourceMap::new(
combined_source_map.get_file().map(|file| file.to_string()),
let mut rspack_source_map = rspack_sources::SourceMap::new(
mappings,
combined_source_map
.sources()
.map(|source| source.to_string().into())
.map(ToString::to_string)
.collect::<Vec<_>>(),
combined_source_map
.source_contents()
.map(|source| source.unwrap_or_default().to_string().into())
.map(Option::unwrap_or_default)
.map(ToString::to_string)
.collect::<Vec<_>>(),
combined_source_map
.names()
.map(|source| source.to_string().into())
.map(ToString::to_string)
.collect::<Vec<_>>(),
))
);
rspack_source_map.set_file(combined_source_map.get_file().map(ToString::to_string));

Some(rspack_source_map)
} else {
None
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Object {
main.js,
],
filteredModules: undefined,
hash: 27a83dfdb1b13db8,
hash: e856770ebb8c912f,
id: 909,
idHints: Array [],
initial: true,
Expand Down Expand Up @@ -173,7 +173,7 @@ Object {
errorsCount: 0,
filteredAssets: undefined,
filteredModules: undefined,
hash: 03ddaa1ecb196f90,
hash: 41ef1cd36ef4fa77,
modules: Array [
Object {
assets: Array [],
Expand Down Expand Up @@ -318,7 +318,7 @@ Object {
main.js,
],
filteredModules: undefined,
hash: 20532fb90939aa5e,
hash: 4ef73da0cb0aba9e,
id: 909,
idHints: Array [],
initial: true,
Expand Down Expand Up @@ -685,7 +685,7 @@ Object {
errorsCount: 0,
filteredAssets: undefined,
filteredModules: undefined,
hash: 6522596fd3b60bef,
hash: 496cda88a175362c,
modules: Array [
Object {
assets: Array [],
Expand Down Expand Up @@ -1449,7 +1449,7 @@ Object {
files: Array [
main.js,
],
hash: 27a83dfdb1b13db8,
hash: e856770ebb8c912f,
id: 909,
idHints: Array [],
initial: true,
Expand Down Expand Up @@ -1589,7 +1589,7 @@ Object {
main.js,
],
filteredModules: undefined,
hash: b7f0963e6f214dbb,
hash: 66b6e930b53470c9,
id: 909,
idHints: Array [],
initial: true,
Expand Down Expand Up @@ -1937,7 +1937,7 @@ exports.c = require("./c?c=3");,
errorsCount: 0,
filteredAssets: undefined,
filteredModules: undefined,
hash: 90c6d1b3d1b7dc71,
hash: a530f14a269b849b,
modules: Array [
Object {
assets: Array [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ runtime modules 1.61 KiB
[no exports]
[used exports unknown]

Rspack compiled successfully (69fd6d18d9865780)
Rspack compiled successfully (5764fc946e8a7c40)
`;

exports[`statsOutput statsOutput/builtin-swc-loader-parse-error should print correct stats for 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
```javascript
Array [
Object {
"main.1a8159bf5e440aaa.js": (() => { // webpackBootstrap
"main.1c39818caeb88ee0.js": (() => { // webpackBootstrap
var __webpack_modules__ = ({
"600": (function (module) {
module.exports = "This is hook"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = {
isOverSizeLimit: false,
name: c560fa876f51d750.png,
related: Array [],
size: 14910,
size: 26694,
type: asset,
},
Object {
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/tests/statsAPICases/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = {
entry ./fixtures/a
cjs self exports reference self [585] ./fixtures/a.js

Rspack compiled successfully (03ddaa1ecb196f90)
Rspack compiled successfully (41ef1cd36ef4fa77)
`);
}
};
4 changes: 2 additions & 2 deletions packages/rspack-test-tools/tests/statsAPICases/chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {
chunkB.js,
],
filteredModules: undefined,
hash: 74c3df550823af13,
hash: 500806fc9348e0fd,
id: 250,
idHints: Array [],
initial: false,
Expand Down Expand Up @@ -144,7 +144,7 @@ module.exports = {
main.js,
],
filteredModules: undefined,
hash: 6c90447074d09a02,
hash: 1c34ab7b66cc4db1,
id: 909,
idHints: Array [],
initial: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ __webpack_require__.e = function (chunkId) {
// webpack/runtime/get_full_hash
(() => {
__webpack_require__.h = function () {
return "e87d810d346c2b363096";
return "64d8082da28c92f64bc5";
};

})();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ __webpack_require__.e = function (chunkId) {
// return url for filenames not based on template

// return url for filenames based on template
return "" + chunkId + ".$" + {"\\css\\chunk": "1a3f2ab8f3a1873a05d7","\\js\\chunk": "87660dc75f3fa01a0c41",}[chunkId] + "$.js";
return "" + chunkId + ".$" + {"\\css\\chunk": "7581dee2a69443af41de","\\js\\chunk": "fc8054f62065d9d47766",}[chunkId] + "$.js";
};

})();
Expand All @@ -107,7 +107,7 @@ __webpack_require__.e = function (chunkId) {
// webpack/runtime/get_full_hash
(() => {
__webpack_require__.h = function () {
return "392cf3d15d8b4a583f33";
return "f747eea271634af1678a";
};

})();
Expand Down
Loading
Loading