@@ -54,7 +54,6 @@ enum Manglable {
54
54
#[ derive( Debug ) ]
55
55
struct ExportInfoCache {
56
56
id : ExportInfo ,
57
- deterministic : bool ,
58
57
exports_info : Option < ExportsInfo > ,
59
58
can_mangle : Manglable ,
60
59
}
@@ -75,19 +74,14 @@ async fn optimize_code_generation(&self, compilation: &mut Compilation) -> Resul
75
74
// TODO: should bailout if compilation.moduleMemCache is enable, https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/MangleExportsPlugin.js#L160-L164
76
75
// We don't do that cause we don't have this option
77
76
let mut mg = compilation. get_module_graph_mut ( ) ;
78
- let module_id_list = mg. modules ( ) . keys ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) ;
77
+ let modules = mg. modules ( ) ;
79
78
80
79
let mut exports_info_cache = FxHashMap :: default ( ) ;
81
80
82
- let mut q = module_id_list
81
+ let mut q = modules
83
82
. iter ( )
84
- . filter_map ( |id| {
85
- let ( Some ( mgm) , Some ( module) ) = (
86
- mg. module_graph_module_by_identifier ( id) ,
87
- mg. module_by_identifier ( id) ,
88
- ) else {
89
- return None ;
90
- } ;
83
+ . filter_map ( |( mid, module) | {
84
+ let mgm = mg. module_graph_module_by_identifier ( mid) ?;
91
85
let is_namespace = matches ! (
92
86
module. build_meta( ) . exports_type,
93
87
BuildMetaExportsType :: Namespace
@@ -163,7 +157,6 @@ async fn optimize_code_generation(&self, compilation: &mut Compilation) -> Resul
163
157
164
158
ExportInfoCache {
165
159
id : export_info_data. id ( ) ,
166
- deterministic,
167
160
exports_info : nested_exports_info,
168
161
can_mangle,
169
162
}
@@ -184,21 +177,32 @@ async fn optimize_code_generation(&self, compilation: &mut Compilation) -> Resul
184
177
}
185
178
}
186
179
187
- for identifier in module_id_list {
188
- let ( Some ( mgm) , Some ( _) ) = (
189
- mg. module_graph_module_by_identifier ( & identifier) ,
190
- mg. module_by_identifier ( & identifier) ,
191
- ) else {
192
- continue ;
193
- } ;
194
- let exports_info = mgm. exports ;
195
- mangle_exports_info (
196
- & mut mg,
197
- self . deterministic ,
198
- exports_info,
199
- & exports_info_cache,
200
- ) ;
180
+ let mut queue = modules
181
+ . into_iter ( )
182
+ . filter_map ( |( mid, _) | {
183
+ let mgm = mg. module_graph_module_by_identifier ( & mid) ?;
184
+ Some ( mgm. exports )
185
+ } )
186
+ . collect_vec ( ) ;
187
+
188
+ while !queue. is_empty ( ) {
189
+ let tasks = std:: mem:: take ( & mut queue) ;
190
+ let batch = tasks
191
+ . into_par_iter ( )
192
+ . map ( |exports_info| {
193
+ mangle_exports_info ( & mg, self . deterministic , exports_info, & exports_info_cache)
194
+ } )
195
+ . collect :: < Vec < _ > > ( ) ;
196
+
197
+ let mut used_name_tasks = vec ! [ ] ;
198
+ for ( changes, nested_exports) in batch {
199
+ used_name_tasks. extend ( changes) ;
200
+ queue. extend ( nested_exports) ;
201
+ }
202
+
203
+ mg. batch_set_export_info_used_name ( used_name_tasks) ;
201
204
}
205
+
202
206
Ok ( ( ) )
203
207
}
204
208
@@ -224,23 +228,25 @@ static MANGLE_NAME_DETERMINISTIC_REG: LazyLock<Regex> = LazyLock::new(|| {
224
228
225
229
/// Function to mangle exports information.
226
230
fn mangle_exports_info (
227
- mg : & mut ModuleGraph ,
231
+ mg : & ModuleGraph ,
228
232
deterministic : bool ,
229
233
exports_info : ExportsInfo ,
230
234
exports_info_cache : & FxHashMap < ExportsInfo , Vec < ExportInfoCache > > ,
231
- ) {
235
+ ) -> ( Vec < ( ExportInfo , Atom ) > , Vec < ExportsInfo > ) {
236
+ let mut changes = vec ! [ ] ;
237
+ let mut nested_exports = vec ! [ ] ;
232
238
let mut used_names = FxHashSet :: default ( ) ;
233
239
let mut mangleable_exports = Vec :: new ( ) ;
234
240
let Some ( export_list) = exports_info_cache. get ( & exports_info) else {
235
- return ;
241
+ return ( changes , nested_exports ) ;
236
242
} ;
237
243
238
244
let mut mangleable_export_names = FxHashMap :: default ( ) ;
239
245
240
246
for export_info in export_list {
241
247
match & export_info. can_mangle {
242
248
Manglable :: CanNotMangle ( name) => {
243
- export_info. id . as_data_mut ( mg ) . set_used_name ( name. clone ( ) ) ;
249
+ changes . push ( ( export_info. id . clone ( ) , name. clone ( ) ) ) ;
244
250
used_names. insert ( name. to_string ( ) ) ;
245
251
}
246
252
Manglable :: CanMangle ( name) => {
@@ -251,12 +257,7 @@ fn mangle_exports_info(
251
257
}
252
258
253
259
if let Some ( nested_exports_info) = export_info. exports_info {
254
- mangle_exports_info (
255
- mg,
256
- export_info. deterministic ,
257
- nested_exports_info,
258
- exports_info_cache,
259
- ) ;
260
+ nested_exports. push ( nested_exports_info) ;
260
261
}
261
262
}
262
263
@@ -297,7 +298,7 @@ fn mangle_exports_info(
297
298
0 ,
298
299
) ;
299
300
for ( export_info, name) in export_info_used_name {
300
- export_info . as_data_mut ( mg ) . set_used_name ( name. into ( ) ) ;
301
+ changes . push ( ( export_info , name. into ( ) ) ) ;
301
302
}
302
303
} else {
303
304
let mut used_exports = Vec :: new ( ) ;
@@ -336,8 +337,9 @@ fn mangle_exports_info(
336
337
}
337
338
i += 1 ;
338
339
}
339
- export_info . as_data_mut ( mg ) . set_used_name ( name. into ( ) ) ;
340
+ changes . push ( ( export_info , name. into ( ) ) ) ;
340
341
}
341
342
}
342
343
}
344
+ ( changes, nested_exports)
343
345
}
0 commit comments