Skip to content

Commit ac8b64a

Browse files
committed
perf: box large compilation artifacts
1 parent df598e4 commit ac8b64a

65 files changed

Lines changed: 2492 additions & 255 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,10 @@ trivial_numeric_casts = "warn"
501501
unused_import_braces = "warn"
502502
unused_lifetimes = "warn"
503503
unused_macro_rules = "warn"
504+
large_assignments = "warn"
504505

505506
[workspace.lints.rust.unexpected_cfgs]
506-
check-cfg = ['cfg(allocative)', 'cfg(codspeed)', 'cfg(dylint_lib, values("rspack_collection_hasher"))']
507+
check-cfg = ['cfg(allocative)', 'cfg(codspeed)', 'cfg(dylint_lib, values("rspack_collection_hasher", "rspack_large_stack_type"))']
507508
level = "warn"
508509

509510
[workspace.lints.clippy]
@@ -531,7 +532,9 @@ index_refutable_slice = "warn"
531532
inefficient_to_string = "warn"
532533
invalid_upcast_comparisons = "warn"
533534
iter_not_returning_iterator = "warn"
535+
large_futures = "warn"
534536
large_stack_arrays = "warn"
537+
large_stack_frames = "warn"
535538
large_types_passed_by_value = "warn"
536539
macro_use_imports = "warn"
537540
manual_ok_or = "warn"

clippy.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
allow-dbg-in-tests = true
22
allow-unwrap-in-tests = true
33

4+
pass-by-value-size-limit = 256
5+
stack-size-threshold = 65536
6+
47
disallowed-methods = [
58

69
{ path = "str::to_ascii_lowercase", reason = "To avoid memory allocation, use `cow_utils::CowUtils::cow_to_ascii_lowercase` instead." },

crates/rspack_core/src/artifacts/code_generate_cache_artifact.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ impl CodeGenerateCacheArtifact {
4646
&self,
4747
job: &CodeGenerationJob,
4848
generator: G,
49-
) -> (Result<CodeGenerationResult>, bool)
49+
) -> (Result<Box<CodeGenerationResult>>, bool)
5050
where
5151
G: FnOnce() -> F,
52-
F: Future<Output = Result<CodeGenerationResult>>,
52+
F: Future<Output = Result<Box<CodeGenerationResult>>>,
5353
{
5454
let Some(storage) = &self.storage else {
5555
let res = generator().await;
@@ -63,11 +63,11 @@ impl CodeGenerateCacheArtifact {
6363
self.runtime_mode
6464
));
6565
if let Some(value) = storage.get(&cache_key) {
66-
(Ok(value), true)
66+
(Ok(Box::new(value)), true)
6767
} else {
6868
match generator().await {
6969
Ok(res) => {
70-
storage.set(cache_key, res.clone());
70+
storage.set(cache_key, (*res).clone());
7171
(Ok(res), false)
7272
}
7373
Err(err) => (Err(err), false),

crates/rspack_core/src/artifacts/code_generation_results.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl CodeGenerationResults {
224224
pub fn insert(
225225
&mut self,
226226
module_identifier: ModuleIdentifier,
227-
codegen_res: CodeGenerationResult,
227+
codegen_res: Box<CodeGenerationResult>,
228228
runtimes: impl IntoIterator<Item = RuntimeSpec>,
229229
) {
230230
let codegen_res_id = codegen_res.id;

crates/rspack_core/src/artifacts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<T: ArtifactExt> ArtifactExt for Box<T> {
7373

7474
// Implementation for BindingCell<T> - used when "napi" feature is enabled
7575
#[cfg(feature = "napi")]
76-
impl<T: ArtifactExt + Into<crate::BindingCell<T>>> ArtifactExt for crate::BindingCell<T> {
76+
impl<T: ArtifactExt> ArtifactExt for crate::BindingCell<T> {
7777
const PASS: IncrementalPasses = T::PASS;
7878

7979
fn recover(incremental: &Incremental, new: &mut Self, old: &mut Self) {

crates/rspack_core/src/binding/cell.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ mod napi_binding {
222222
}
223223

224224
impl BindingCell<CodeGenerationResults> {
225-
pub fn new(code_generation_results: CodeGenerationResults) -> Self {
226-
let boxed = Box::new(code_generation_results);
225+
pub fn new(boxed: Box<CodeGenerationResults>) -> Self {
227226
let ptr = boxed.as_ref() as *const CodeGenerationResults as *mut CodeGenerationResults;
228227
let heap = Arc::new(Heap {
229228
variant: HeapVariant::CodeGenerationResults(boxed),
@@ -233,15 +232,14 @@ mod napi_binding {
233232
}
234233
}
235234

236-
impl From<CodeGenerationResults> for BindingCell<CodeGenerationResults> {
237-
fn from(code_generation_results: CodeGenerationResults) -> Self {
235+
impl From<Box<CodeGenerationResults>> for BindingCell<CodeGenerationResults> {
236+
fn from(code_generation_results: Box<CodeGenerationResults>) -> Self {
238237
Self::new(code_generation_results)
239238
}
240239
}
241240

242241
impl BindingCell<CodeGenerationResult> {
243-
pub fn new(code_generation_result: CodeGenerationResult) -> Self {
244-
let boxed = Box::new(code_generation_result);
242+
pub fn new(boxed: Box<CodeGenerationResult>) -> Self {
245243
let ptr = boxed.as_ref() as *const CodeGenerationResult as *mut CodeGenerationResult;
246244
let heap = Arc::new(Heap {
247245
variant: HeapVariant::CodeGenerationResult(boxed),
@@ -251,8 +249,8 @@ mod napi_binding {
251249
}
252250
}
253251

254-
impl From<CodeGenerationResult> for BindingCell<CodeGenerationResult> {
255-
fn from(code_generation_result: CodeGenerationResult) -> Self {
252+
impl From<Box<CodeGenerationResult>> for BindingCell<CodeGenerationResult> {
253+
fn from(code_generation_result: Box<CodeGenerationResult>) -> Self {
256254
Self::new(code_generation_result)
257255
}
258256
}
@@ -339,6 +337,18 @@ mod napi_binding {
339337
}
340338
}
341339

340+
impl Clone for BindingCell<CodeGenerationResults> {
341+
fn clone(&self) -> Self {
342+
Box::new(self.as_ref().clone()).into()
343+
}
344+
}
345+
346+
impl Clone for BindingCell<CodeGenerationResult> {
347+
fn clone(&self) -> Self {
348+
Box::new(self.as_ref().clone()).into()
349+
}
350+
}
351+
342352
impl<T: ?Sized + Hash> Hash for BindingCell<T> {
343353
fn hash<H: Hasher>(&self, state: &mut H) {
344354
self.as_ref().hash(state);

crates/rspack_core/src/cache/persistent/build_dependencies/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,24 @@ impl BuildDeps {
101101
/// Validate build dependencies
102102
///
103103
/// If any build dependencies have changed, this method will return an invalid result.
104-
pub async fn validate(&mut self, storage: &dyn Storage) -> Result<BuildDepsValidationResult> {
104+
pub async fn validate(
105+
&mut self,
106+
storage: &dyn Storage,
107+
) -> Result<Box<BuildDepsValidationResult>> {
105108
let (_, modified_files, removed_files, no_changed_files) = self
106109
.snapshot
107110
.calc_modified_paths(storage, SnapshotScope::BUILD)
108111
.await?;
109112

110113
if !modified_files.is_empty() || !removed_files.is_empty() {
111-
return Ok(BuildDepsValidationResult::Invalid {
114+
return Ok(Box::new(BuildDepsValidationResult::Invalid {
112115
modified_files,
113116
removed_files,
114-
});
117+
}));
115118
}
116119
let tracked_files = no_changed_files.len();
117120
self.added = no_changed_files;
118-
Ok(BuildDepsValidationResult::Valid { tracked_files })
121+
Ok(Box::new(BuildDepsValidationResult::Valid { tracked_files }))
119122
}
120123
}
121124

@@ -216,7 +219,7 @@ mod test {
216219
.await
217220
.expect("should validate success");
218221
assert!(matches!(
219-
validate_result,
222+
validate_result.as_ref(),
220223
BuildDepsValidationResult::Invalid { .. }
221224
));
222225
storage.reset(scope);

crates/rspack_core/src/cache/persistent/context.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,30 @@ impl CacheContext {
7171
pub async fn load_build_deps(&mut self, build_deps: &mut BuildDeps) {
7272
let start = self.logger().time("validate build dependencies");
7373
match build_deps.validate(&*self.storage).await {
74-
Ok(BuildDepsValidationResult::Valid { tracked_files }) => {
75-
self.invalid = false;
76-
self.logger().info(format!(
77-
"build dependencies are valid ({tracked_files} tracked)"
78-
));
79-
}
80-
Ok(BuildDepsValidationResult::Invalid {
81-
modified_files,
82-
removed_files,
83-
}) => {
84-
self.invalid = true;
85-
self.load_failed = true;
86-
let reason = format_path_changes(&modified_files, &removed_files);
87-
self.logger().warn(format!(
88-
"persistent cache invalidated because build dependencies changed:\n{reason}"
89-
));
90-
if self.readonly {
91-
self
92-
.logger()
93-
.warn("persistent cache is readonly, stale entries will not be rewritten");
74+
Ok(result) => match result.as_ref() {
75+
BuildDepsValidationResult::Valid { tracked_files } => {
76+
self.invalid = false;
77+
self.logger().info(format!(
78+
"build dependencies are valid ({tracked_files} tracked)"
79+
));
9480
}
95-
}
81+
BuildDepsValidationResult::Invalid {
82+
modified_files,
83+
removed_files,
84+
} => {
85+
self.invalid = true;
86+
self.load_failed = true;
87+
let reason = format_path_changes(&modified_files, &removed_files);
88+
self.logger().warn(format!(
89+
"persistent cache invalidated because build dependencies changed:\n{reason}"
90+
));
91+
if self.readonly {
92+
self
93+
.logger()
94+
.warn("persistent cache is readonly, stale entries will not be rewritten");
95+
}
96+
}
97+
},
9698
Err(err) => {
9799
self.load_failed = true;
98100
self
@@ -229,7 +231,7 @@ impl CacheContext {
229231
/// Returns `None` and resets the occasion's scope when the cache is
230232
/// invalid or recovery fails.
231233
#[tracing::instrument("Cache::Context::load_occasion", skip_all)]
232-
pub async fn load_occasion<O: Occasion>(&mut self, occasion: &O) -> Option<O::Artifact> {
234+
pub async fn load_occasion<O: Occasion>(&mut self, occasion: &O) -> Option<Box<O::Artifact>> {
233235
if !self.load_failed {
234236
let start = self
235237
.logger()

crates/rspack_core/src/cache/persistent/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl Cache for PersistentCache {
211211
}
212212

213213
if let Some(artifact) = self.ctx.load_occasion(&self.make_occasion).await {
214-
*compilation.build_module_graph_artifact = artifact;
214+
compilation.build_module_graph_artifact = artifact.into();
215215
for (module, _) in compilation
216216
.build_module_graph_artifact
217217
.get_module_graph()
@@ -253,14 +253,16 @@ impl Cache for PersistentCache {
253253

254254
async fn after_process_assets(&mut self, compilation: &Compilation) {
255255
if let Some(artifact) = &compilation.minimize_persistent_cache_artifact {
256-
self.ctx.save_occasion(&self.minimize_occasion, artifact);
256+
self
257+
.ctx
258+
.save_occasion(&self.minimize_occasion, artifact.as_ref());
257259
}
258260
if compilation.use_source_map_dev_tool_plugin_cache
259261
&& let Some(artifact) = &compilation.source_map_dev_tool_plugin_cache_artifact
260262
{
261263
self
262264
.ctx
263-
.save_occasion(&self.source_map_dev_tool_plugin_occasion, artifact);
265+
.save_occasion(&self.source_map_dev_tool_plugin_occasion, artifact.as_ref());
264266
}
265267
}
266268

crates/rspack_core/src/cache/persistent/occasion/devtool/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,10 @@ impl Occasion for SourceMapDevToolPluginOccasion {
278278
}
279279

280280
#[tracing::instrument(name = "Cache::Occasion::SourceMap::recovery", skip_all)]
281-
async fn recovery(&self, storage: &dyn Storage) -> Result<SourceMapDevToolPluginCacheArtifact> {
281+
async fn recovery(
282+
&self,
283+
storage: &dyn Storage,
284+
) -> Result<Box<SourceMapDevToolPluginCacheArtifact>> {
282285
let items = storage.load(SCOPE).await?;
283286
let entries = items
284287
.into_par_iter()
@@ -312,10 +315,10 @@ impl Occasion for SourceMapDevToolPluginOccasion {
312315
"recovered {} source map persistent cache entries",
313316
entries.len()
314317
);
315-
Ok(SourceMapDevToolPluginCacheArtifact {
318+
Ok(Box::new(SourceMapDevToolPluginCacheArtifact {
316319
entries,
317320
pending_writes: Vec::new(),
318321
pending_removes: Vec::new(),
319-
})
322+
}))
320323
}
321324
}

0 commit comments

Comments
 (0)