Skip to content

Commit 0a81c72

Browse files
authored
Turbopack: use depth as priority for merged module info (#83909)
module_count: 63896 visit_count of the `traverse_edges_fixed_point_with_priority`: - before: 277.620 - with +depth: 1.250.882 - with -depth (this PR): 184.697
1 parent 7fb8bfe commit 0a81c72

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

turbopack/crates/turbopack-core/src/module_graph/chunk_group_info.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraphRef) -> Result<Vc<Chunk
405405

406406
// First, compute the depth for each module in the graph
407407
let module_depth: FxHashMap<ResolvedVc<Box<dyn Module>>, usize> = {
408-
let mut module_depth = FxHashMap::default();
408+
let mut module_depth =
409+
FxHashMap::with_capacity_and_hasher(module_count, Default::default());
409410
graph.traverse_edges_from_entries_bfs(
410411
entries.iter().flat_map(|e| e.entries()),
411412
|parent, node| {

turbopack/crates/turbopack-core/src/module_graph/merged_modules.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
8888
.flat_map(|g| g.entries())
8989
.collect::<Vec<_>>();
9090

91+
// First, compute the depth for each module in the graph
92+
let module_depth = {
93+
let _inner_span = tracing::info_span!("compute depth").entered();
94+
95+
let mut module_depth =
96+
FxHashMap::with_capacity_and_hasher(module_count, Default::default());
97+
module_graph.traverse_edges_from_entries_bfs(
98+
entries.iter().copied(),
99+
|parent, node| {
100+
if let Some((parent, _)) = parent {
101+
let parent_depth = *module_depth
102+
.get(&parent.module)
103+
.context("Module depth not found")?;
104+
module_depth.entry(node.module).or_insert(parent_depth + 1);
105+
} else {
106+
module_depth.insert(node.module, 0);
107+
};
108+
109+
Ok(GraphTraversalAction::Continue)
110+
},
111+
)?;
112+
module_depth
113+
};
114+
91115
// For each module, the indices in the bitmap store which merge group entry modules
92116
// transitively import that module. The bitmap can be treated as an opaque value, merging
93117
// all modules with the same bitmap.
@@ -121,7 +145,10 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
121145

122146
let mut next_index = 0u32;
123147
let visit_count = module_graph.traverse_edges_fixed_point_with_priority(
124-
entries.iter().map(|e| (*e, 0)),
148+
entries
149+
.iter()
150+
.map(|e| Ok((*e, -*module_depth.get(e).context("Module depth not found")?)))
151+
.collect::<Result<Vec<_>>>()?,
125152
&mut (),
126153
|parent_info: Option<(&'_ SingleModuleGraphModuleNode, &'_ RefData)>,
127154
node: &'_ SingleModuleGraphModuleNode,
@@ -196,7 +223,13 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
196223
}
197224
})
198225
},
199-
|_, _| Ok(0),
226+
|successor, _| {
227+
// Invert the ordering here. High priority values get visited first, and we want to
228+
// visit the low-depth nodes first, as we are propagating bitmaps downwards.
229+
Ok(-*module_depth
230+
.get(&successor.module)
231+
.context("Module depth not found")?)
232+
},
200233
)?;
201234

202235
drop(inner_span);

0 commit comments

Comments
 (0)