Skip to content

Commit 3be48da

Browse files
authored
perf: rspack sources memory (#12038)
1 parent dc3e644 commit 3be48da

File tree

23 files changed

+1219
-1193
lines changed

23 files changed

+1219
-1193
lines changed

Cargo.lock

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ regex-syntax = { version = "0.8.5", default-features = false, features =
8383
regress = { version = "0.10.4", default-features = false, features = ["pattern"] }
8484
ropey = { version = "1.6.1", default-features = false }
8585
rspack_resolver = { features = ["package_json_raw_json_api", "yarn_pnp"], version = "0.6.4", default-features = false }
86-
rspack_sources = { version = "=0.4.13", default-features = false }
86+
rspack_sources = { version = "0.4.14", default-features = false }
8787
rustc-hash = { version = "2.1.0", default-features = false }
8888
ryu-js = { version = "1.0.2", default-features = false }
8989
scopeguard = { version = "1.2.0", default-features = false }
@@ -99,6 +99,7 @@ sugar_path = { version = "1.2.0", default-features = false, features =
9999
syn = { version = "2.0.95", default-features = false }
100100
termcolor = { version = "1.4.1", default-features = false }
101101
textwrap = { version = "0.16.1", default-features = false }
102+
thread_local = { version = "1.1.9", default-features = false }
102103
tokio = { version = "1.42.0", default-features = false, features = ["rt", "rt-multi-thread"] }
103104
toml = { version = "0.8.19", default-features = false, features = ["parse", "display"] }
104105
tracing = { version = "0.1.41", default-features = false, features = ["max_level_trace", "release_max_level_trace"] }

crates/rspack_binding_api/src/source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{hash::Hash, sync::Arc};
22

33
use napi_derive::napi;
44
use rspack_core::rspack_sources::{
5-
BoxSource, CachedSource, ConcatSource, MapOptions, OriginalSource, RawBufferSource,
5+
BoxSource, CachedSource, ConcatSource, MapOptions, ObjectPool, OriginalSource, RawBufferSource,
66
RawStringSource, ReplaceSource, Source, SourceExt, SourceMap, SourceMapSource, SourceValue,
77
WithoutOriginalOptions,
88
};
@@ -101,7 +101,7 @@ impl From<JsSourceToJs> for BoxSource {
101101
}
102102

103103
fn to_webpack_map(source: &dyn Source) -> Result<Option<String>> {
104-
let map = source.map(&MapOptions::default());
104+
let map = source.map(&ObjectPool::default(), &MapOptions::default());
105105

106106
map.map(|m| m.to_json()).transpose().to_napi_result()
107107
}

crates/rspack_cacheable/src/with/as_preset/rspack_sources/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use rkyv::{
66
};
77
use rspack_macros::enable_cacheable as cacheable;
88
use rspack_sources::{
9-
BoxSource, RawBufferSource, Source, SourceExt, SourceMap, SourceMapSource, WithoutOriginalOptions,
9+
BoxSource, ObjectPool, RawBufferSource, Source, SourceExt, SourceMap, SourceMapSource,
10+
WithoutOriginalOptions,
1011
};
1112

1213
use super::AsPreset;
@@ -42,7 +43,7 @@ where
4243
field: &BoxSource,
4344
serializer: &mut S,
4445
) -> Result<Self::Resolver, SerializeError> {
45-
let map = match field.map(&Default::default()) {
46+
let map = match field.map(&ObjectPool::default(), &Default::default()) {
4647
Some(map) => Some(
4748
map
4849
.to_json()

crates/rspack_cacheable_test/tests/with/as_preset/rspack_sources.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rspack_cacheable::{enable_cacheable as cacheable, from_bytes, to_bytes, with::AsPreset};
2-
use rspack_sources::{BoxSource, RawBufferSource, RawStringSource, SourceExt};
2+
use rspack_sources::{BoxSource, ObjectPool, RawBufferSource, RawStringSource, SourceExt};
33

44
#[cacheable]
55
#[derive(Debug)]
@@ -12,8 +12,8 @@ fn test_rspack_source() {
1212
let new_data: Data = from_bytes(&bytes, &()).unwrap();
1313
assert_eq!(data.0.buffer(), new_data.0.buffer());
1414
assert_eq!(
15-
data.0.map(&Default::default()),
16-
new_data.0.map(&Default::default())
15+
data.0.map(&ObjectPool::default(), &Default::default()),
16+
new_data.0.map(&ObjectPool::default(), &Default::default())
1717
);
1818
}
1919

crates/rspack_core/src/utils/extract_source_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,9 @@ pub async fn extract_source_map(
351351
}
352352

353353
// Build the final SourceMap using setter methods - consume resolved_sources to avoid cloning
354-
let (sources_vec, sources_content_vec): (Vec<String>, Vec<String>) = resolved_sources
354+
let (sources_vec, sources_content_vec): (Vec<String>, Vec<Arc<str>>) = resolved_sources
355355
.into_iter()
356-
.map(|(url, content)| (url, content.unwrap_or_default()))
356+
.map(|(url, content)| (url, Arc::from(content.unwrap_or_default())))
357357
.unzip();
358358

359359
source_map.set_sources(sources_vec);

crates/rspack_javascript_compiler/src/compiler/stringify.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ impl JavaScriptCompiler {
156156
.collect::<Vec<_>>(),
157157
combined_source_map
158158
.source_contents()
159-
.flatten()
160-
.map(ToString::to_string)
159+
.map(|byte_str| Arc::from(byte_str.map(ToString::to_string).unwrap_or_default()))
161160
.collect::<Vec<_>>(),
162161
combined_source_map
163162
.names()

crates/rspack_loader_lightningcss/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl LightningCssLoader {
241241
source_map
242242
.get_sources_content()
243243
.iter()
244-
.map(ToString::to_string)
244+
.map(|source_content| Arc::from(source_content.to_string()))
245245
.collect::<Vec<_>>(),
246246
source_map
247247
.get_names()

crates/rspack_plugin_devtool/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ rspack_util = { workspace = true }
2727
rustc-hash = { workspace = true }
2828
simd-json = { workspace = true }
2929
sugar_path = { workspace = true }
30+
thread_local = { workspace = true }
3031
tracing = { workspace = true }
3132

3233
[package.metadata.cargo-shear]

crates/rspack_plugin_devtool/src/eval_source_map_dev_tool_plugin.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rspack_core::{
77
ChunkGraph, ChunkInitFragments, ChunkUkey, Compilation,
88
CompilationAdditionalModuleRuntimeRequirements, CompilationParams, CompilerCompilation, Filename,
99
Module, ModuleIdentifier, PathData, Plugin, RuntimeGlobals,
10-
rspack_sources::{BoxSource, MapOptions, RawStringSource, Source, SourceExt},
10+
rspack_sources::{BoxSource, MapOptions, ObjectPool, RawStringSource, Source, SourceExt},
1111
};
1212
use rspack_error::Result;
1313
use rspack_hash::{RspackHash, RspackHashDigest};
@@ -107,7 +107,9 @@ async fn eval_source_map_devtool_plugin_render_module_content(
107107
if let Some(cached_source) = self.cache.get(module_hash) {
108108
render_source.source = cached_source.value().clone();
109109
return Ok(());
110-
} else if let Some(mut map) = origin_source.map(&MapOptions::new(self.columns)) {
110+
} else if let Some(mut map) =
111+
origin_source.map(&ObjectPool::default(), &MapOptions::new(self.columns))
112+
{
111113
let source = {
112114
let source = origin_source.source().into_string_lossy();
113115

0 commit comments

Comments
 (0)