@@ -32,7 +32,7 @@ use crate::{
3232 merged_modules:: { MergedModuleInfo , compute_merged_modules} ,
3333 module_batches:: { ModuleBatchesGraph , compute_module_batches} ,
3434 style_groups:: { StyleGroups , StyleGroupsConfig , compute_style_groups} ,
35- traced_di_graph:: { TracedDiGraph , iter_graphs_neighbors_rev } ,
35+ traced_di_graph:: TracedDiGraph ,
3636 } ,
3737 reference:: primary_chunkable_referenced_modules,
3838 resolve:: ExportUsage ,
@@ -406,7 +406,7 @@ impl SingleModuleGraph {
406406 pub fn read ( self : & ReadRef < SingleModuleGraph > ) -> ModuleGraphRef {
407407 ModuleGraphRef {
408408 graphs : vec ! [ self . clone( ) ] ,
409- ignore_visited_module : true ,
409+ skip_visited_module_children : true ,
410410 }
411411 }
412412
@@ -807,7 +807,7 @@ impl ModuleGraph {
807807 let async_modules_info = self . async_module_info ( ) . await ?;
808808
809809 let entry = graph_ref. get_entry ( module) ?;
810- let referenced_modules = iter_graphs_neighbors_rev ( & graphs[ entry . graph_idx ( ) ] . graph , entry)
810+ let referenced_modules = iter_graphs_neighbors_rev ( graphs, entry)
811811 . filter ( |( edge_idx, _) | {
812812 let ty = graphs[ entry. graph_idx ( ) ]
813813 . graph
@@ -831,7 +831,7 @@ impl ModuleGraph {
831831 pub async fn read_graphs ( self : Vc < ModuleGraph > ) -> Result < ModuleGraphRef > {
832832 Ok ( ModuleGraphRef {
833833 graphs : self . await ?. graphs . iter ( ) . try_join ( ) . await ?,
834- ignore_visited_module : false ,
834+ skip_visited_module_children : false ,
835835 } )
836836 }
837837}
@@ -842,7 +842,7 @@ pub struct ModuleGraphRef {
842842 pub graphs : Vec < ReadRef < SingleModuleGraph > > ,
843843 // Whether to simply ignore SingleModuleGraphNode::VisitedModule during traversals. For single
844844 // module graph usecases, this is what you want. For the whole graph, there should be an error.
845- ignore_visited_module : bool ,
845+ skip_visited_module_children : bool ,
846846}
847847
848848impl ModuleGraphRef {
@@ -899,7 +899,7 @@ impl ModuleGraphRef {
899899 mut visit_postorder : impl FnMut ( ResolvedVc < Box < dyn Module > > , & mut S ) -> Result < ( ) > ,
900900 ) -> Result < ( ) > {
901901 let graphs = & self . graphs ;
902- let ignore_visited_module = self . ignore_visited_module ;
902+ let skip_visited_module_children = self . skip_visited_module_children ;
903903
904904 let entries = entries. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
905905
@@ -928,20 +928,14 @@ impl ModuleGraphRef {
928928 }
929929 stack. push ( ( Pass :: Visit , current) ) ;
930930 if action == GraphTraversalAction :: Continue
931- && !( ignore_visited_module
931+ && !( skip_visited_module_children
932932 && matches ! ( current_node, SingleModuleGraphNode :: VisitedModule { .. } ) )
933933 {
934- let graph = & graphs[ current. graph_idx ( ) ] . graph ;
935- let neighbors_rev = match graph. node_weight ( current. node_idx ) . unwrap ( ) {
936- SingleModuleGraphNode :: Module ( _) => {
937- iter_graphs_neighbors_rev ( graph, current)
938- }
939- SingleModuleGraphNode :: VisitedModule { idx, .. } => {
940- // We switch graphs
941- iter_graphs_neighbors_rev ( & graphs[ idx. graph_idx ( ) ] . graph , * idx)
942- }
943- } ;
944- stack. extend ( neighbors_rev. map ( |( _, child) | ( Pass :: ExpandAndVisit , child) ) ) ;
934+ let current = current_node. target_idx ( ) . unwrap_or ( current) ;
935+ stack. extend (
936+ iter_graphs_neighbors_rev ( graphs, current)
937+ . map ( |( _, child) | ( Pass :: ExpandAndVisit , child) ) ,
938+ ) ;
945939 }
946940 }
947941 }
@@ -969,7 +963,7 @@ impl ModuleGraphRef {
969963 ) -> Result < GraphTraversalAction > ,
970964 ) -> Result < ( ) > {
971965 let graphs = & self . graphs ;
972- let ignore_visited_module = self . ignore_visited_module ;
966+ let skip_visited_module_children = self . skip_visited_module_children ;
973967
974968 let mut queue = VecDeque :: from (
975969 entries
@@ -985,18 +979,18 @@ impl ModuleGraphRef {
985979 if visited. insert ( node) {
986980 let node_weight = self . get_node ( node) ?;
987981 let graph = & graphs[ node. graph_idx ( ) ] . graph ;
988- for ( edge, succ) in iter_graphs_neighbors_rev ( graph , node) {
982+ for ( edge, succ) in iter_graphs_neighbors_rev ( graphs , node) {
989983 let succ_weight = self . get_node ( succ) ?;
990- if ignore_visited_module
991- && matches ! ( succ_weight, SingleModuleGraphNode :: VisitedModule { .. } )
992- {
993- continue ;
994- }
995984 let edge_weight = graph. edge_weight ( edge) . unwrap ( ) ;
996985 let action = visitor (
997986 Some ( ( node_weight. module ( ) , edge_weight) ) ,
998987 succ_weight. module ( ) ,
999988 ) ?;
989+ if skip_visited_module_children
990+ && matches ! ( succ_weight, SingleModuleGraphNode :: VisitedModule { .. } )
991+ {
992+ continue ;
993+ }
1000994 let succ = succ_weight. target_idx ( ) . unwrap_or ( succ) ;
1001995 if !visited. contains ( & succ) && action == GraphTraversalAction :: Continue {
1002996 queue. push_back ( succ) ;
@@ -1027,7 +1021,7 @@ impl ModuleGraphRef {
10271021 ) -> GraphTraversalAction ,
10281022 ) -> Result < ( ) > {
10291023 let graphs = & self . graphs ;
1030- let ignore_visited_module = self . ignore_visited_module ;
1024+ let skip_visited_module_children = self . skip_visited_module_children ;
10311025
10321026 let mut stack = entries
10331027 . into_iter ( )
@@ -1041,18 +1035,18 @@ impl ModuleGraphRef {
10411035 if visited. insert ( node) {
10421036 let node_weight = self . get_node ( node) ?;
10431037 let graph = & graphs[ node. graph_idx ( ) ] . graph ;
1044- for ( edge, succ) in iter_graphs_neighbors_rev ( graph , node) {
1038+ for ( edge, succ) in iter_graphs_neighbors_rev ( graphs , node) {
10451039 let succ_weight = self . get_node ( succ) ?;
1046- if ignore_visited_module
1047- && matches ! ( succ_weight, SingleModuleGraphNode :: VisitedModule { .. } )
1048- {
1049- continue ;
1050- }
10511040 let edge_weight = graph. edge_weight ( edge) . unwrap ( ) ;
10521041 let action = visitor (
10531042 Some ( ( node_weight. module ( ) , edge_weight) ) ,
10541043 succ_weight. module ( ) ,
10551044 ) ;
1045+ if skip_visited_module_children
1046+ && matches ! ( succ_weight, SingleModuleGraphNode :: VisitedModule { .. } )
1047+ {
1048+ continue ;
1049+ }
10561050 let succ = succ_weight. target_idx ( ) . unwrap_or ( succ) ;
10571051 if !visited. contains ( & succ) && action == GraphTraversalAction :: Continue {
10581052 stack. push ( succ) ;
@@ -1125,7 +1119,7 @@ impl ModuleGraphRef {
11251119 ) -> Result < ( ) > ,
11261120 ) -> Result < ( ) > {
11271121 let graphs = & self . graphs ;
1128- let ignore_visited_module = self . ignore_visited_module ;
1122+ let skip_visited_module_children = self . skip_visited_module_children ;
11291123
11301124 let entries = entries. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
11311125
@@ -1164,12 +1158,11 @@ impl ModuleGraphRef {
11641158 stack. push ( ( Pass :: Visit , parent, current) ) ;
11651159 if action == GraphTraversalAction :: Continue
11661160 && expanded. insert ( current)
1167- && !( ignore_visited_module
1161+ && !( skip_visited_module_children
11681162 && matches ! ( current_node, SingleModuleGraphNode :: VisitedModule { .. } ) )
11691163 {
1170- let graph = & graphs[ current. graph_idx ( ) ] . graph ;
11711164 let current = current_node. target_idx ( ) . unwrap_or ( current) ;
1172- stack. extend ( iter_graphs_neighbors_rev ( graph , current) . map (
1165+ stack. extend ( iter_graphs_neighbors_rev ( graphs , current) . map (
11731166 |( edge, child) | ( Pass :: ExpandAndVisit , Some ( ( current, edge) ) , child) ,
11741167 ) ) ;
11751168 }
@@ -1226,7 +1219,9 @@ impl ModuleGraphRef {
12261219 priority : impl Fn ( ResolvedVc < Box < dyn Module > > , & mut S ) -> Result < P > ,
12271220 ) -> Result < usize > {
12281221 let graphs = & self . graphs ;
1229- let ignore_visited_module = self . ignore_visited_module ;
1222+ if self . skip_visited_module_children {
1223+ bail ! ( "traverse_edges_fixed_point_with_priority musn't be called on individual graphs" ) ;
1224+ }
12301225
12311226 #[ derive( PartialEq , Eq ) ]
12321227 struct NodeWithPriority < T : Ord > {
@@ -1275,7 +1270,7 @@ impl ModuleGraphRef {
12751270 visit_count += 1 ;
12761271
12771272 let graph = & graphs[ node. graph_idx ( ) ] . graph ;
1278- for ( edge, succ) in iter_graphs_neighbors_rev ( graph , node) {
1273+ for ( edge, succ) in iter_graphs_neighbors_rev ( graphs , node) {
12791274 let succ_weight = self . get_node ( succ) ?;
12801275
12811276 let edge_weight = graph. edge_weight ( edge) . unwrap ( ) ;
@@ -1286,11 +1281,7 @@ impl ModuleGraphRef {
12861281 ) ?;
12871282
12881283 let succ = succ_weight. target_idx ( ) . unwrap_or ( succ) ;
1289- if action == GraphTraversalAction :: Continue
1290- && queue_set. insert ( succ)
1291- && !( ignore_visited_module
1292- && matches ! ( succ_weight, SingleModuleGraphNode :: VisitedModule { .. } ) )
1293- {
1284+ if action == GraphTraversalAction :: Continue && queue_set. insert ( succ) {
12941285 queue. push ( NodeWithPriority {
12951286 node : succ,
12961287 priority : priority ( succ_weight. module ( ) , state) ?,
@@ -1303,6 +1294,34 @@ impl ModuleGraphRef {
13031294 }
13041295}
13051296
1297+ /// Iterate the edges of a node REVERSED!
1298+ fn iter_graphs_neighbors_rev (
1299+ graphs : & [ ReadRef < SingleModuleGraph > ] ,
1300+ node : GraphNodeIndex ,
1301+ ) -> impl Iterator < Item = ( EdgeIndex , GraphNodeIndex ) > + ' _ {
1302+ let graph = & * graphs[ node. graph_idx ( ) ] . graph ;
1303+
1304+ if cfg ! ( debug_assertions) {
1305+ let node_weight = graph. node_weight ( node. node_idx ) . unwrap ( ) ;
1306+ if let SingleModuleGraphNode :: VisitedModule { .. } = node_weight {
1307+ panic ! ( "iter_graphs_neighbors_rev called on VisitedModule node" ) ;
1308+ }
1309+ }
1310+
1311+ let mut walker = graph. neighbors ( node. node_idx ) . detach ( ) ;
1312+ std:: iter:: from_fn ( move || {
1313+ walker. next ( graph) . map ( |( edge_idx, succ_idx) | {
1314+ (
1315+ edge_idx,
1316+ GraphNodeIndex {
1317+ graph_idx : node. graph_idx ,
1318+ node_idx : succ_idx,
1319+ } ,
1320+ )
1321+ } )
1322+ } )
1323+ }
1324+
13061325#[ turbo_tasks:: value_impl]
13071326impl SingleModuleGraph {
13081327 #[ turbo_tasks:: function]
0 commit comments