Skip to content

Commit 5817ce6

Browse files
committed
use depth as priority
1 parent 7dc9daa commit 5817ce6

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,29 @@ 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 = FxHashMap::default();
96+
module_graph.traverse_edges_from_entries_bfs(
97+
entries.iter().copied(),
98+
|parent, node| {
99+
if let Some((parent, _)) = parent {
100+
let parent_depth = *module_depth
101+
.get(&parent.module)
102+
.context("Module depth not found")?;
103+
module_depth.entry(node.module).or_insert(parent_depth + 1);
104+
} else {
105+
module_depth.insert(node.module, 0);
106+
};
107+
108+
Ok(GraphTraversalAction::Continue)
109+
},
110+
)?;
111+
module_depth
112+
};
113+
91114
// For each module, the indices in the bitmap store which merge group entry modules
92115
// transitively import that module. The bitmap can be treated as an opaque value, merging
93116
// all modules with the same bitmap.
@@ -121,7 +144,10 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
121144

122145
let mut next_index = 0u32;
123146
let visit_count = module_graph.traverse_edges_fixed_point_with_priority(
124-
entries.iter().map(|e| (*e, 0)),
147+
entries
148+
.iter()
149+
.map(|e| Ok((*e, *module_depth.get(e).context("Module depth not found")?)))
150+
.collect::<Result<Vec<_>>>()?,
125151
&mut (),
126152
|parent_info: Option<(&'_ SingleModuleGraphModuleNode, &'_ RefData)>,
127153
node: &'_ SingleModuleGraphModuleNode,
@@ -196,7 +222,13 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
196222
}
197223
})
198224
},
199-
|_, _| Ok(0),
225+
|successor, _| {
226+
// Invert the ordering here. High priority values get visited first, and we want to
227+
// visit the low-depth nodes first, as we are propagating bitmaps downwards.
228+
Ok(-*module_depth
229+
.get(&successor.module)
230+
.context("Module depth not found")?)
231+
},
200232
)?;
201233

202234
drop(inner_span);

0 commit comments

Comments
 (0)