Skip to content

Commit c6bcf3b

Browse files
authored
perf: optimize flag dependency usage (#14052)
1 parent 19ac553 commit c6bcf3b

5 files changed

Lines changed: 35 additions & 40 deletions

File tree

crates/rspack_core/src/exports/export_info_setter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::hash_map::Entry;
22

33
use rspack_util::atom::Atom;
44

5-
use super::{ExportInfoData, ExportInfoTargetValue, UsageFilterFnTy, UsageState};
5+
use super::{ExportInfoData, ExportInfoTargetValue, UsageState};
66
use crate::{CanInlineUse, DependencyId, Nullable, RuntimeSpec};
77

88
impl ExportInfoData {
@@ -130,7 +130,7 @@ impl ExportInfoData {
130130

131131
pub fn set_used_conditionally(
132132
&mut self,
133-
condition: UsageFilterFnTy<UsageState>,
133+
condition: impl Fn(&UsageState) -> bool,
134134
new_value: UsageState,
135135
runtime: Option<&RuntimeSpec>,
136136
) -> bool {
@@ -180,7 +180,7 @@ impl ExportInfoData {
180180
let mut changed = false;
181181

182182
if self.set_used_conditionally(
183-
Box::new(|value| value < &UsageState::Unknown),
183+
|value| value < &UsageState::Unknown,
184184
UsageState::Unknown,
185185
runtime,
186186
) {

crates/rspack_core/src/exports/exports_info_setter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
impl ExportsInfoData {
1010
pub fn set_used_for_side_effects_only(&mut self, runtime: Option<&RuntimeSpec>) -> bool {
1111
self.side_effects_only_info_mut().set_used_conditionally(
12-
Box::new(|value| value == &UsageState::Unused),
12+
|value| value == &UsageState::Unused,
1313
UsageState::Used,
1414
runtime,
1515
)
@@ -132,7 +132,7 @@ impl ExportsInfoData {
132132
}
133133
let other_exports_info = self.other_exports_info_mut();
134134
if other_exports_info.set_used_conditionally(
135-
Box::new(|value| value < &UsageState::Unknown),
135+
|value| value < &UsageState::Unknown,
136136
UsageState::Unknown,
137137
runtime,
138138
) {

crates/rspack_core/src/exports/utils.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ impl UsageKey {
178178
}
179179
}
180180

181-
pub type UsageFilterFnTy<T> = Box<dyn Fn(&T) -> bool>;
182-
183181
#[derive(Debug, PartialEq, Copy, Clone, Default, Hash, PartialOrd, Ord, Eq)]
184182
pub enum UsageState {
185183
Unused = 0,

crates/rspack_plugin_javascript/src/plugin/flag_dependency_usage_plugin.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
491491
let last_one = i == len - 1;
492492
if !last_one && let Some(nested_info) = export_info.exports_info() {
493493
let changed_flag = export_info.set_used_conditionally(
494-
Box::new(|used| used == &UsageState::Unused),
494+
|used| used == &UsageState::Unused,
495495
UsageState::OnlyPropertiesUsed,
496496
runtime.as_ref(),
497497
);
@@ -517,7 +517,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
517517
}
518518

519519
let changed_flag = export_info.set_used_conditionally(
520-
Box::new(|v| v != &UsageState::Used),
520+
|v| v != &UsageState::Used,
521521
UsageState::Used,
522522
runtime.as_ref(),
523523
);
@@ -707,30 +707,10 @@ fn collect_active_dependencies(
707707
(block.get_blocks(), block.get_dependencies())
708708
}
709709
};
710-
dependencies.extend(block_dependencies);
711-
for block_id in blocks {
712-
let block = module_graph
713-
.block_by_id(block_id)
714-
.expect("should have block");
715-
if !global && let Some(GroupOptions::Entrypoint(options)) = block.get_group_options() {
716-
let runtime = RuntimeSpec::from_entry_options(options);
717-
q.push((
718-
ModuleOrAsyncDependenciesBlock::AsyncDependenciesBlock(*block_id),
719-
runtime,
720-
true,
721-
));
722-
} else {
723-
queue.push_back(ModuleOrAsyncDependenciesBlock::AsyncDependenciesBlock(
724-
*block_id,
725-
));
726-
}
727-
}
728-
}
729-
730-
let dependencies = dependencies
731-
.into_iter()
732-
.filter_map(|dep_id| {
733-
let connection = module_graph.connection_by_dependency_id(&dep_id)?;
710+
for &dep_id in block_dependencies {
711+
let Some(connection) = module_graph.connection_by_dependency_id(&dep_id) else {
712+
continue;
713+
};
734714
let active_state = connection.active_state(
735715
module_graph,
736716
runtime,
@@ -741,21 +721,38 @@ fn collect_active_dependencies(
741721

742722
match active_state {
743723
ConnectionState::Active(false) => {
744-
return None;
724+
continue;
745725
}
746726
ConnectionState::TransitiveOnly => {
747727
q.push((
748728
ModuleOrAsyncDependenciesBlock::Module(*connection.module_identifier()),
749729
runtime.cloned(),
750730
false,
751731
));
752-
return None;
732+
continue;
753733
}
754734
_ => {}
755735
}
756-
Some((dep_id, *connection.module_identifier()))
757-
})
758-
.collect::<Vec<_>>();
736+
dependencies.push((dep_id, *connection.module_identifier()));
737+
}
738+
for block_id in blocks {
739+
let block = module_graph
740+
.block_by_id(block_id)
741+
.expect("should have block");
742+
if !global && let Some(GroupOptions::Entrypoint(options)) = block.get_group_options() {
743+
let runtime = RuntimeSpec::from_entry_options(options);
744+
q.push((
745+
ModuleOrAsyncDependenciesBlock::AsyncDependenciesBlock(*block_id),
746+
runtime,
747+
true,
748+
));
749+
} else {
750+
queue.push_back(ModuleOrAsyncDependenciesBlock::AsyncDependenciesBlock(
751+
*block_id,
752+
));
753+
}
754+
}
755+
}
759756

760757
(dependencies, q)
761758
}
@@ -846,7 +843,7 @@ fn process_referenced_module_without_nested(
846843
}
847844

848845
let changed_flag = export_info.set_used_conditionally(
849-
Box::new(|v| v != &UsageState::Used),
846+
|v| v != &UsageState::Used,
850847
UsageState::Used,
851848
runtime.as_ref(),
852849
);

crates/rspack_plugin_mf/src/sharing/shared_used_exports_optimizer_plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ async fn optimize_dependencies(
297297
// Mark used exports
298298
for export_info in exports_info_data.exports_mut().values_mut() {
299299
export_info.set_used_conditionally(
300-
Box::new(|used| *used == rspack_core::UsageState::Unknown),
300+
|used| *used == rspack_core::UsageState::Unknown,
301301
rspack_core::UsageState::Unused,
302302
None,
303303
);

0 commit comments

Comments
 (0)