11use std:: collections:: { hash_map:: Entry , VecDeque } ;
22
3- use rayon:: prelude:: * ;
4- use rspack_collections:: { Identifier , IdentifierMap , UkeyMap } ;
3+ use rspack_collections:: { IdentifierMap , UkeyMap } ;
54use rspack_core:: {
65 get_entry_runtime, incremental:: IncrementalPasses , is_exports_object_referenced,
76 is_no_exports_referenced, AsyncDependenciesBlockIdentifier , BuildMetaExportsType , Compilation ,
@@ -11,20 +10,14 @@ use rspack_core::{
1110} ;
1211use rspack_error:: Result ;
1312use rspack_hook:: { plugin, plugin_hook} ;
14- use rspack_util:: swc:: join_atom;
13+ use rspack_util:: { queue :: Queue , swc:: join_atom} ;
1514use rustc_hash:: FxHashMap as HashMap ;
1615
1716#[ derive( Debug , PartialEq , Eq , Hash , Clone , Copy ) ]
1817enum ModuleOrAsyncDependenciesBlock {
1918 Module ( ModuleIdentifier ) ,
2019 AsyncDependenciesBlock ( AsyncDependenciesBlockIdentifier ) ,
2120}
22-
23- #[ derive( Debug , Clone ) ]
24- enum ProcessModuleReferencedExports {
25- Map ( HashMap < String , ExtendedReferencedExport > ) ,
26- ExtendRef ( Vec < ExtendedReferencedExport > ) ,
27- }
2821#[ allow( unused) ]
2922pub struct FlagDependencyUsagePluginProxy < ' a > {
3023 global : bool ,
@@ -49,8 +42,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
4942 . exports_info_module_map
5043 . insert ( mgm. exports , mgm. module_identifier ) ;
5144 }
52- let mut batch = Vec :: new ( ) ;
53-
45+ let mut q = Queue :: new ( ) ;
5446 let mg = & mut module_graph;
5547 for exports_info in self . exports_info_module_map . keys ( ) {
5648 exports_info. set_has_use_info ( mg) ;
@@ -69,65 +61,43 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
6961 global_runtime. get_or_insert_default ( ) . extend ( runtime) ;
7062 }
7163 for & dep in entry. dependencies . iter ( ) {
72- self . process_entry_dependency ( dep, runtime. clone ( ) , & mut batch ) ;
64+ self . process_entry_dependency ( dep, runtime. clone ( ) , & mut q ) ;
7365 }
7466 for & dep in entry. include_dependencies . iter ( ) {
75- self . process_entry_dependency ( dep, runtime. clone ( ) , & mut batch ) ;
67+ self . process_entry_dependency ( dep, runtime. clone ( ) , & mut q ) ;
7668 }
7769 }
7870 for dep in self . compilation . global_entry . dependencies . clone ( ) {
79- self . process_entry_dependency ( dep, global_runtime. clone ( ) , & mut batch ) ;
71+ self . process_entry_dependency ( dep, global_runtime. clone ( ) , & mut q ) ;
8072 }
8173 for dep in self . compilation . global_entry . include_dependencies . clone ( ) {
82- self . process_entry_dependency ( dep, global_runtime. clone ( ) , & mut batch ) ;
74+ self . process_entry_dependency ( dep, global_runtime. clone ( ) , & mut q ) ;
8375 }
8476 self . compilation . entries = entries;
8577
86- while !batch. is_empty ( ) {
87- let modules = std:: mem:: take ( & mut batch) ;
88- let module_graph = self . compilation . get_module_graph ( ) ;
89- let module_graph_cache = & self . compilation . module_graph_cache_artifact ;
90- let mut module_referenced_exports = modules
91- . into_par_iter ( )
92- . map ( |( module_id, runtime) | {
93- self . collect_module_referenced_exports (
94- ModuleOrAsyncDependenciesBlock :: Module ( module_id) ,
95- runtime,
96- false ,
97- )
98- } )
99- . collect :: < Vec < _ > > ( ) ;
100-
101- for referenced_exports in module_referenced_exports {
102- for ( module_id, referenced_exports, runtime, force_side_effects) in referenced_exports {
103- let normalized_refs = match referenced_exports {
104- ProcessModuleReferencedExports :: Map ( map) => map. into_values ( ) . collect :: < Vec < _ > > ( ) ,
105- ProcessModuleReferencedExports :: ExtendRef ( extend_ref) => extend_ref,
106- } ;
107- self . process_referenced_module (
108- module_id,
109- normalized_refs,
110- runtime. clone ( ) ,
111- force_side_effects,
112- & mut batch,
113- ) ;
114- }
115- }
78+ while let Some ( ( module_id, runtime) ) = q. dequeue ( ) {
79+ self . process_module (
80+ ModuleOrAsyncDependenciesBlock :: Module ( module_id) ,
81+ runtime,
82+ false ,
83+ & mut q,
84+ ) ;
11685 }
11786 }
11887
119- fn collect_module_referenced_exports (
120- & self ,
88+ fn process_module (
89+ & mut self ,
12190 block_id : ModuleOrAsyncDependenciesBlock ,
12291 runtime : Option < RuntimeSpec > ,
12392 force_side_effects : bool ,
124- ) -> Vec < (
125- Identifier ,
126- ProcessModuleReferencedExports ,
127- Option < RuntimeSpec > ,
128- bool ,
129- ) > {
130- let mut res = Vec :: new ( ) ;
93+ q : & mut Queue < ( ModuleIdentifier , Option < RuntimeSpec > ) > ,
94+ ) {
95+ #[ derive( Debug , Clone ) ]
96+ enum ProcessModuleReferencedExports {
97+ Map ( HashMap < String , ExtendedReferencedExport > ) ,
98+ ExtendRef ( Vec < ExtendedReferencedExport > ) ,
99+ }
100+
131101 let mut map: IdentifierMap < ProcessModuleReferencedExports > = IdentifierMap :: default ( ) ;
132102 let mut queue = VecDeque :: new ( ) ;
133103 queue. push_back ( block_id) ;
@@ -157,11 +127,12 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
157127 && let Some ( GroupOptions :: Entrypoint ( options) ) = block. get_group_options ( )
158128 {
159129 let runtime = RuntimeSpec :: from_entry_options ( options) ;
160- res . extend ( self . collect_module_referenced_exports (
130+ self . process_module (
161131 ModuleOrAsyncDependenciesBlock :: AsyncDependenciesBlock ( block_id) ,
162132 runtime,
163133 true ,
164- ) ) ;
134+ q,
135+ )
165136 } else {
166137 queue. push_back ( ModuleOrAsyncDependenciesBlock :: AsyncDependenciesBlock (
167138 block_id,
@@ -186,11 +157,12 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
186157 continue ;
187158 }
188159 ConnectionState :: TransitiveOnly => {
189- res . extend ( self . collect_module_referenced_exports (
160+ self . process_module (
190161 ModuleOrAsyncDependenciesBlock :: Module ( * connection. module_identifier ( ) ) ,
191162 runtime. clone ( ) ,
192163 false ,
193- ) ) ;
164+ q,
165+ ) ;
194166 continue ;
195167 }
196168 _ => { }
@@ -282,30 +254,33 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
282254 }
283255 }
284256
285- res. extend ( map. into_iter ( ) . map ( |( module_id, referenced_exports) | {
286- (
257+ for ( module_id, referenced_exports) in map {
258+ let normalized_refs = match referenced_exports {
259+ ProcessModuleReferencedExports :: Map ( map) => map. into_values ( ) . collect :: < Vec < _ > > ( ) ,
260+ ProcessModuleReferencedExports :: ExtendRef ( extend_ref) => extend_ref,
261+ } ;
262+ self . process_referenced_module (
287263 module_id,
288- referenced_exports ,
264+ normalized_refs ,
289265 runtime. clone ( ) ,
290266 force_side_effects,
291- )
292- } ) ) ;
293-
294- res
267+ q,
268+ ) ;
269+ }
295270 }
296271
297272 fn process_entry_dependency (
298273 & mut self ,
299274 dep : DependencyId ,
300275 runtime : Option < RuntimeSpec > ,
301- batch : & mut Vec < ( Identifier , Option < RuntimeSpec > ) > ,
276+ queue : & mut Queue < ( ModuleIdentifier , Option < RuntimeSpec > ) > ,
302277 ) {
303278 if let Some ( module) = self
304279 . compilation
305280 . get_module_graph ( )
306281 . module_graph_module_by_dependency_id ( & dep)
307282 {
308- self . process_referenced_module ( module. module_identifier , vec ! [ ] , runtime, true , batch ) ;
283+ self . process_referenced_module ( module. module_identifier , vec ! [ ] , runtime, true , queue ) ;
309284 }
310285 }
311286
@@ -315,7 +290,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
315290 used_exports : Vec < ExtendedReferencedExport > ,
316291 runtime : Option < RuntimeSpec > ,
317292 force_side_effects : bool ,
318- batch : & mut Vec < ( Identifier , Option < RuntimeSpec > ) > ,
293+ queue : & mut Queue < ( ModuleIdentifier , Option < RuntimeSpec > ) > ,
319294 ) {
320295 let mut module_graph = self . compilation . get_module_graph_mut ( ) ;
321296 let mgm = module_graph
@@ -333,7 +308,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
333308 if need_insert {
334309 let flag = mgm_exports_info. set_used_without_info ( & mut module_graph, runtime. as_ref ( ) ) ;
335310 if flag {
336- batch . push ( ( module_id, None ) ) ;
311+ queue . enqueue ( ( module_id, None ) ) ;
337312 }
338313 return ;
339314 }
@@ -349,30 +324,34 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
349324 let flag = mgm_exports_info. set_used_in_unknown_way ( & mut module_graph, runtime. as_ref ( ) ) ;
350325
351326 if flag {
352- batch . push ( ( module_id, runtime. clone ( ) ) ) ;
327+ queue . enqueue ( ( module_id, runtime. clone ( ) ) ) ;
353328 }
354329 } else {
355330 let mut current_exports_info = mgm_exports_info;
356331 let len = used_exports. len ( ) ;
357332 for ( i, used_export) in used_exports. into_iter ( ) . enumerate ( ) {
358- let export_info = current_exports_info
359- . get_export_info ( & mut module_graph, & used_export)
360- . as_data_mut ( & mut module_graph) ;
333+ let export_info = current_exports_info. get_export_info ( & mut module_graph, & used_export) ;
361334 if !can_mangle {
362- export_info. set_can_mangle_use ( Some ( false ) ) ;
335+ export_info
336+ . as_data_mut ( & mut module_graph)
337+ . set_can_mangle_use ( Some ( false ) ) ;
363338 }
364339 if !can_inline {
365- export_info. set_inlinable ( Inlinable :: NoByUse ) ;
340+ export_info
341+ . as_data_mut ( & mut module_graph)
342+ . set_inlinable ( Inlinable :: NoByUse ) ;
366343 }
367344 let last_one = i == len - 1 ;
368345 if !last_one {
369- let nested_info = export_info. exports_info ( ) ;
346+ let nested_info = export_info. as_data ( & module_graph ) . exports_info ( ) ;
370347 if let Some ( nested_info) = nested_info {
371- let changed_flag = export_info. set_used_conditionally (
372- Box :: new ( |used| used == & UsageState :: Unused ) ,
373- UsageState :: OnlyPropertiesUsed ,
374- runtime. as_ref ( ) ,
375- ) ;
348+ let changed_flag = export_info
349+ . as_data_mut ( & mut module_graph)
350+ . set_used_conditionally (
351+ Box :: new ( |used| used == & UsageState :: Unused ) ,
352+ UsageState :: OnlyPropertiesUsed ,
353+ runtime. as_ref ( ) ,
354+ ) ;
376355 if changed_flag {
377356 let current_module = if current_exports_info == mgm_exports_info {
378357 Some ( module_id)
@@ -383,19 +362,21 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
383362 . cloned ( )
384363 } ;
385364 if let Some ( current_module) = current_module {
386- batch . push ( ( current_module, runtime. clone ( ) ) ) ;
365+ queue . enqueue ( ( current_module, runtime. clone ( ) ) ) ;
387366 }
388367 }
389368 current_exports_info = nested_info;
390369 continue ;
391370 }
392371 }
393372
394- let changed_flag = export_info. set_used_conditionally (
395- Box :: new ( |v| v != & UsageState :: Used ) ,
396- UsageState :: Used ,
397- runtime. as_ref ( ) ,
398- ) ;
373+ let changed_flag = export_info
374+ . as_data_mut ( & mut module_graph)
375+ . set_used_conditionally (
376+ Box :: new ( |v| v != & UsageState :: Used ) ,
377+ UsageState :: Used ,
378+ runtime. as_ref ( ) ,
379+ ) ;
399380 if changed_flag {
400381 let current_module = if current_exports_info == mgm_exports_info {
401382 Some ( module_id)
@@ -406,7 +387,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
406387 . cloned ( )
407388 } ;
408389 if let Some ( current_module) = current_module {
409- batch . push ( ( current_module, runtime. clone ( ) ) ) ;
390+ queue . enqueue ( ( current_module, runtime. clone ( ) ) ) ;
410391 }
411392 }
412393 break ;
@@ -426,7 +407,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
426407 . as_data_mut ( & mut module_graph)
427408 . set_used_for_side_effects_only ( runtime. as_ref ( ) ) ;
428409 if changed_flag {
429- batch . push ( ( module_id, runtime) ) ;
410+ queue . enqueue ( ( module_id, runtime) ) ;
430411 }
431412 }
432413 }
0 commit comments