1
1
use std:: collections:: { hash_map:: Entry , VecDeque } ;
2
2
3
- use rayon:: prelude:: * ;
4
- use rspack_collections:: { Identifier , IdentifierMap , UkeyMap } ;
3
+ use rspack_collections:: { IdentifierMap , UkeyMap } ;
5
4
use rspack_core:: {
6
5
get_entry_runtime, incremental:: IncrementalPasses , is_exports_object_referenced,
7
6
is_no_exports_referenced, AsyncDependenciesBlockIdentifier , BuildMetaExportsType , Compilation ,
@@ -11,20 +10,14 @@ use rspack_core::{
11
10
} ;
12
11
use rspack_error:: Result ;
13
12
use rspack_hook:: { plugin, plugin_hook} ;
14
- use rspack_util:: swc:: join_atom;
13
+ use rspack_util:: { queue :: Queue , swc:: join_atom} ;
15
14
use rustc_hash:: FxHashMap as HashMap ;
16
15
17
16
#[ derive( Debug , PartialEq , Eq , Hash , Clone , Copy ) ]
18
17
enum ModuleOrAsyncDependenciesBlock {
19
18
Module ( ModuleIdentifier ) ,
20
19
AsyncDependenciesBlock ( AsyncDependenciesBlockIdentifier ) ,
21
20
}
22
-
23
- #[ derive( Debug , Clone ) ]
24
- enum ProcessModuleReferencedExports {
25
- Map ( HashMap < String , ExtendedReferencedExport > ) ,
26
- ExtendRef ( Vec < ExtendedReferencedExport > ) ,
27
- }
28
21
#[ allow( unused) ]
29
22
pub struct FlagDependencyUsagePluginProxy < ' a > {
30
23
global : bool ,
@@ -49,8 +42,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
49
42
. exports_info_module_map
50
43
. insert ( mgm. exports , mgm. module_identifier ) ;
51
44
}
52
- let mut batch = Vec :: new ( ) ;
53
-
45
+ let mut q = Queue :: new ( ) ;
54
46
let mg = & mut module_graph;
55
47
for exports_info in self . exports_info_module_map . keys ( ) {
56
48
exports_info. set_has_use_info ( mg) ;
@@ -69,65 +61,43 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
69
61
global_runtime. get_or_insert_default ( ) . extend ( runtime) ;
70
62
}
71
63
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 ) ;
73
65
}
74
66
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 ) ;
76
68
}
77
69
}
78
70
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 ) ;
80
72
}
81
73
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 ) ;
83
75
}
84
76
self . compilation . entries = entries;
85
77
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
+ ) ;
116
85
}
117
86
}
118
87
119
- fn collect_module_referenced_exports (
120
- & self ,
88
+ fn process_module (
89
+ & mut self ,
121
90
block_id : ModuleOrAsyncDependenciesBlock ,
122
91
runtime : Option < RuntimeSpec > ,
123
92
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
+
131
101
let mut map: IdentifierMap < ProcessModuleReferencedExports > = IdentifierMap :: default ( ) ;
132
102
let mut queue = VecDeque :: new ( ) ;
133
103
queue. push_back ( block_id) ;
@@ -157,11 +127,12 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
157
127
&& let Some ( GroupOptions :: Entrypoint ( options) ) = block. get_group_options ( )
158
128
{
159
129
let runtime = RuntimeSpec :: from_entry_options ( options) ;
160
- res . extend ( self . collect_module_referenced_exports (
130
+ self . process_module (
161
131
ModuleOrAsyncDependenciesBlock :: AsyncDependenciesBlock ( block_id) ,
162
132
runtime,
163
133
true ,
164
- ) ) ;
134
+ q,
135
+ )
165
136
} else {
166
137
queue. push_back ( ModuleOrAsyncDependenciesBlock :: AsyncDependenciesBlock (
167
138
block_id,
@@ -186,11 +157,12 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
186
157
continue ;
187
158
}
188
159
ConnectionState :: TransitiveOnly => {
189
- res . extend ( self . collect_module_referenced_exports (
160
+ self . process_module (
190
161
ModuleOrAsyncDependenciesBlock :: Module ( * connection. module_identifier ( ) ) ,
191
162
runtime. clone ( ) ,
192
163
false ,
193
- ) ) ;
164
+ q,
165
+ ) ;
194
166
continue ;
195
167
}
196
168
_ => { }
@@ -282,30 +254,33 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
282
254
}
283
255
}
284
256
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 (
287
263
module_id,
288
- referenced_exports ,
264
+ normalized_refs ,
289
265
runtime. clone ( ) ,
290
266
force_side_effects,
291
- )
292
- } ) ) ;
293
-
294
- res
267
+ q,
268
+ ) ;
269
+ }
295
270
}
296
271
297
272
fn process_entry_dependency (
298
273
& mut self ,
299
274
dep : DependencyId ,
300
275
runtime : Option < RuntimeSpec > ,
301
- batch : & mut Vec < ( Identifier , Option < RuntimeSpec > ) > ,
276
+ queue : & mut Queue < ( ModuleIdentifier , Option < RuntimeSpec > ) > ,
302
277
) {
303
278
if let Some ( module) = self
304
279
. compilation
305
280
. get_module_graph ( )
306
281
. module_graph_module_by_dependency_id ( & dep)
307
282
{
308
- self . process_referenced_module ( module. module_identifier , vec ! [ ] , runtime, true , batch ) ;
283
+ self . process_referenced_module ( module. module_identifier , vec ! [ ] , runtime, true , queue ) ;
309
284
}
310
285
}
311
286
@@ -315,7 +290,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
315
290
used_exports : Vec < ExtendedReferencedExport > ,
316
291
runtime : Option < RuntimeSpec > ,
317
292
force_side_effects : bool ,
318
- batch : & mut Vec < ( Identifier , Option < RuntimeSpec > ) > ,
293
+ queue : & mut Queue < ( ModuleIdentifier , Option < RuntimeSpec > ) > ,
319
294
) {
320
295
let mut module_graph = self . compilation . get_module_graph_mut ( ) ;
321
296
let mgm = module_graph
@@ -333,7 +308,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
333
308
if need_insert {
334
309
let flag = mgm_exports_info. set_used_without_info ( & mut module_graph, runtime. as_ref ( ) ) ;
335
310
if flag {
336
- batch . push ( ( module_id, None ) ) ;
311
+ queue . enqueue ( ( module_id, None ) ) ;
337
312
}
338
313
return ;
339
314
}
@@ -349,30 +324,34 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
349
324
let flag = mgm_exports_info. set_used_in_unknown_way ( & mut module_graph, runtime. as_ref ( ) ) ;
350
325
351
326
if flag {
352
- batch . push ( ( module_id, runtime. clone ( ) ) ) ;
327
+ queue . enqueue ( ( module_id, runtime. clone ( ) ) ) ;
353
328
}
354
329
} else {
355
330
let mut current_exports_info = mgm_exports_info;
356
331
let len = used_exports. len ( ) ;
357
332
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) ;
361
334
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 ) ) ;
363
338
}
364
339
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 ) ;
366
343
}
367
344
let last_one = i == len - 1 ;
368
345
if !last_one {
369
- let nested_info = export_info. exports_info ( ) ;
346
+ let nested_info = export_info. as_data ( & module_graph ) . exports_info ( ) ;
370
347
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
+ ) ;
376
355
if changed_flag {
377
356
let current_module = if current_exports_info == mgm_exports_info {
378
357
Some ( module_id)
@@ -383,19 +362,21 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
383
362
. cloned ( )
384
363
} ;
385
364
if let Some ( current_module) = current_module {
386
- batch . push ( ( current_module, runtime. clone ( ) ) ) ;
365
+ queue . enqueue ( ( current_module, runtime. clone ( ) ) ) ;
387
366
}
388
367
}
389
368
current_exports_info = nested_info;
390
369
continue ;
391
370
}
392
371
}
393
372
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
+ ) ;
399
380
if changed_flag {
400
381
let current_module = if current_exports_info == mgm_exports_info {
401
382
Some ( module_id)
@@ -406,7 +387,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
406
387
. cloned ( )
407
388
} ;
408
389
if let Some ( current_module) = current_module {
409
- batch . push ( ( current_module, runtime. clone ( ) ) ) ;
390
+ queue . enqueue ( ( current_module, runtime. clone ( ) ) ) ;
410
391
}
411
392
}
412
393
break ;
@@ -426,7 +407,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> {
426
407
. as_data_mut ( & mut module_graph)
427
408
. set_used_for_side_effects_only ( runtime. as_ref ( ) ) ;
428
409
if changed_flag {
429
- batch . push ( ( module_id, runtime) ) ;
410
+ queue . enqueue ( ( module_id, runtime) ) ;
430
411
}
431
412
}
432
413
}
0 commit comments