Skip to content

Commit 18f392f

Browse files
authored
perf: Enable more Clippy performance checks and reduce redundant clones (#13069)
Enable clippy perf rules
1 parent a55ea7a commit 18f392f

File tree

30 files changed

+116
-99
lines changed

30 files changed

+116
-99
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ verbose_file_reads = "warn"
508508
#mem_forget = "warn"
509509

510510
# performance
511+
iter_filter_is_ok = "warn"
512+
iter_filter_is_some = "warn"
513+
iter_with_drain = "warn"
511514
redundant_clone = "warn"
512515
regex_creation_in_loops = "warn"
513516

crates/rspack_binding_api/src/compilation/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Diagnostics {
183183
i += 1;
184184
}
185185

186-
for diagnostic in to_insert.drain(..) {
186+
for diagnostic in to_insert {
187187
new_diagnostics.push(diagnostic);
188188
}
189189

crates/rspack_core/src/artifacts/build_chunk_graph_artifact.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,16 @@ impl ArtifactExt for BuildChunkGraphArtifact {
244244
fn recover(_incremental: &crate::incremental::Incremental, new: &mut Self, old: &mut Self) {
245245
new.code_splitter = mem::take(&mut old.code_splitter);
246246
rayon::scope(|s| {
247-
s.spawn(|_| new.chunk_by_ukey = old.chunk_by_ukey.clone());
248-
s.spawn(|_| new.chunk_graph = old.chunk_graph.clone());
249-
s.spawn(|_| new.chunk_group_by_ukey = old.chunk_group_by_ukey.clone());
247+
s.spawn(|_| new.chunk_by_ukey.clone_from(&old.chunk_by_ukey));
248+
s.spawn(|_| new.chunk_graph.clone_from(&old.chunk_graph));
249+
s.spawn(|_| new.chunk_group_by_ukey.clone_from(&old.chunk_group_by_ukey));
250250

251-
s.spawn(|_| new.async_entrypoints = old.async_entrypoints.clone());
252-
s.spawn(|_| new.named_chunk_groups = old.named_chunk_groups.clone());
253-
s.spawn(|_| new.named_chunks = old.named_chunks.clone());
251+
s.spawn(|_| new.async_entrypoints.clone_from(&old.async_entrypoints));
252+
s.spawn(|_| new.named_chunk_groups.clone_from(&old.named_chunk_groups));
253+
s.spawn(|_| new.named_chunks.clone_from(&old.named_chunks));
254254
s.spawn(|_| {
255-
new.entrypoints = old.entrypoints.clone();
256-
new.module_idx = old.module_idx.clone();
255+
new.entrypoints.clone_from(&old.entrypoints);
256+
new.module_idx.clone_from(&old.module_idx);
257257
});
258258
});
259259
}

crates/rspack_core/src/chunk_graph/chunk_graph_chunk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,6 @@ impl ChunkGraph {
11601160

11611161
None
11621162
})
1163-
.unwrap_or(module.source_types(module_graph).iter().copied().collect())
1163+
.unwrap_or_else(|| module.source_types(module_graph).iter().copied().collect())
11641164
}
11651165
}

crates/rspack_core/src/compilation/assign_runtime_ids/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl PassExt for AssignRuntimeIdsPass {
2727
Some(EntryRuntime::String(s)) => Some(s.to_owned()),
2828
_ => None,
2929
})
30-
.or(entrypoint.name().map(|n| n.to_string()));
30+
.or_else(|| entrypoint.name().map(|n| n.to_string()));
3131
if let (Some(runtime), Some(chunk)) = (
3232
runtime,
3333
chunk_by_ukey.get(&entrypoint.get_runtime_chunk(chunk_group_by_ukey)),

crates/rspack_core/src/compilation/build_chunk_graph/incremental.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl CodeSplitter {
460460

461461
let cgi = self.chunk_group_infos.expect_get_mut(&cgi_ukey);
462462
let group_ukey = cgi.chunk_group;
463-
cgi.skipped_items = cache_result.skipped_modules.clone();
463+
cgi.skipped_items.clone_from(&cache_result.skipped_modules);
464464

465465
let chunk_graph = &mut compilation.build_chunk_graph_artifact.chunk_graph;
466466
for module in &cache_result.modules {
@@ -475,8 +475,12 @@ impl CodeSplitter {
475475
.build_chunk_graph_artifact
476476
.chunk_group_by_ukey
477477
.expect_get_mut(&group_ukey);
478-
group.module_pre_order_indices = cache_result.pre_order_indices.clone();
479-
group.module_post_order_indices = cache_result.post_order_indices.clone();
478+
group
479+
.module_pre_order_indices
480+
.clone_from(&cache_result.pre_order_indices);
481+
group
482+
.module_post_order_indices
483+
.clone_from(&cache_result.post_order_indices);
480484

481485
for block in &cache_result.outgoings {
482486
self.make_chunk_group(

crates/rspack_core/src/compilation/build_module_graph/graph_updater/cutout/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,11 @@ impl Cutout {
132132
}
133133
}
134134
// add entry dependencies
135-
for dep in next_entry_dependencies
136-
.difference(&entry_dependencies)
137-
.copied()
138-
.collect::<Vec<_>>()
139-
{
140-
build_deps.insert((dep, None));
141-
entry_dependencies.insert(dep);
135+
for dep in next_entry_dependencies.iter() {
136+
if !entry_dependencies.contains(dep) {
137+
build_deps.insert((*dep, None));
138+
entry_dependencies.insert(*dep);
139+
}
142140
}
143141
artifact.entry_dependencies = entry_dependencies;
144142

crates/rspack_core/src/compilation/build_module_graph/graph_updater/repair/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl Task<TaskContext> for BuildResultTask {
182182

183183
{
184184
let mgm = module_graph.module_graph_module_by_identifier_mut(&module.identifier());
185-
mgm.all_dependencies = all_dependencies.clone();
185+
mgm.all_dependencies.clone_from(&all_dependencies);
186186
}
187187

188188
let module_identifier = module.identifier();

crates/rspack_core/src/compilation/build_module_graph/graph_updater/repair/lazy.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ impl Task<TaskContext> for ProcessUnlazyDependenciesTask {
2828
.module_to_lazy_make
2929
.get_lazy_dependencies(&original_module_identifier)
3030
.expect("only module has lazy dependencies should run into ProcessUnlazyDependenciesTask");
31-
let requested_deps: Vec<DependencyId> = lazy_dependencies
32-
.requested_lazy_dependencies(&forwarded_ids)
33-
.into_iter()
34-
.collect();
3531

3632
let module_graph = &mut context.artifact.module_graph;
37-
let dependencies_to_process: Vec<DependencyId> = requested_deps
33+
let dependencies_to_process: Vec<DependencyId> = lazy_dependencies
34+
.requested_lazy_dependencies(&forwarded_ids)
3835
.into_iter()
3936
.filter(|dep| {
4037
let dep = module_graph.dependency_by_id_mut(dep);

crates/rspack_core/src/compilation/build_module_graph/module_executor/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Task<ExecutorTaskContext> for EntryTask {
4646
// not exist, generate a new dependency
4747
let dep = Box::new(LoaderImportDependency::new(
4848
meta.request.clone(),
49-
origin_module_context.unwrap_or(Context::from("")),
49+
origin_module_context.unwrap_or_else(|| Context::from("")),
5050
));
5151
let dep_id = *dep.id();
5252

0 commit comments

Comments
 (0)