@@ -9,14 +9,14 @@ use rolldown_common::{
99 NamedImport , OutputFormat , SymbolRef ,
1010} ;
1111use rolldown_rstr:: { Rstr , ToRstr } ;
12- use rolldown_utils:: rayon:: { IntoParallelIterator , ParallelBridge , ParallelIterator } ;
12+ use rolldown_utils:: rayon:: { ParallelBridge , ParallelIterator } ;
1313use rustc_hash:: { FxHashMap , FxHashSet } ;
1414
1515type ChunkMetaImports = IndexVec < ChunkId , FxHashSet < SymbolRef > > ;
1616type ChunkMetaImportsForExternalModules =
1717 IndexVec < ChunkId , FxHashMap < ExternalModuleId , Vec < NamedImport > > > ;
1818type ChunkMetaExports = IndexVec < ChunkId , FxHashSet < SymbolRef > > ;
19- type IndexCrossChunkImports = IndexVec < ChunkId , FxHashSet < ChunkId > > ;
19+ type IndexCrossChunkImports = IndexVec < ChunkId , Vec < ChunkId > > ;
2020
2121impl < ' a > GenerateStage < ' a > {
2222 /// - Assign each symbol to the chunk it belongs to
@@ -26,7 +26,7 @@ impl<'a> GenerateStage<'a> {
2626 chunk_graph : & mut ChunkGraph ,
2727 chunk_meta_imports : & mut ChunkMetaImports ,
2828 chunk_meta_imports_from_external_modules : & mut ChunkMetaImportsForExternalModules ,
29- index_cross_chunk_imports : & mut IndexCrossChunkImports ,
29+ cross_chunk_dynamic_imports : & mut IndexCrossChunkImports ,
3030 ) {
3131 let symbols = & Mutex :: new ( & mut self . link_output . symbols ) ;
3232
@@ -38,15 +38,15 @@ impl<'a> GenerateStage<'a> {
3838 chunk_meta_imports. iter_mut ( ) . zip (
3939 chunk_meta_imports_from_external_modules
4040 . iter_mut ( )
41- . zip ( index_cross_chunk_imports . iter_mut ( ) ) ,
41+ . zip ( cross_chunk_dynamic_imports . iter_mut ( ) ) ,
4242 ) ,
4343 )
4444 . par_bridge ( )
4545 } ;
4646 chunks_iter. for_each (
4747 |(
4848 ( chunk_id, chunk) ,
49- ( chunk_meta_imports, ( imports_from_external_modules, cross_chunk_imports ) ) ,
49+ ( chunk_meta_imports, ( imports_from_external_modules, cross_chunk_dynamic_imports ) ) ,
5050 ) | {
5151 chunk. modules . iter ( ) . copied ( ) . for_each ( |module_id| {
5252 let module = & self . link_output . module_table . normal_modules [ module_id] ;
@@ -58,7 +58,7 @@ impl<'a> GenerateStage<'a> {
5858 if let ModuleId :: Normal ( importee_id) = rec. resolved_module {
5959 let importee_chunk =
6060 chunk_graph. module_to_chunk [ importee_id] . expect ( "importee chunk should exist" ) ;
61- cross_chunk_imports . insert ( importee_chunk) ;
61+ cross_chunk_dynamic_imports . push ( importee_chunk) ;
6262 }
6363 }
6464 } )
@@ -145,14 +145,16 @@ impl<'a> GenerateStage<'a> {
145145 ChunkId ,
146146 FxHashMap < ChunkId , Vec < CrossChunkImportItem > > ,
147147 > = index_vec ! [ FxHashMap :: <ChunkId , Vec <CrossChunkImportItem >>:: default ( ) ; chunk_graph. chunks. len( ) ] ;
148- let mut index_cross_chunk_imports: IndexCrossChunkImports =
149- index_vec ! [ FxHashSet :: default ( ) ; chunk_graph. chunks. len( ) ] ;
148+ let mut cross_chunk_imports: IndexCrossChunkImports =
149+ index_vec ! [ vec![ ] ; chunk_graph. chunks. len( ) ] ;
150+ let mut cross_chunk_dynamic_imports: IndexCrossChunkImports =
151+ index_vec ! [ vec![ ] ; chunk_graph. chunks. len( ) ] ;
150152
151153 self . collect_potential_chunk_imports (
152154 chunk_graph,
153155 & mut chunk_meta_imports_vec,
154156 & mut chunk_meta_imports_from_external_modules_vec,
155- & mut index_cross_chunk_imports ,
157+ & mut cross_chunk_dynamic_imports ,
156158 ) ;
157159
158160 // - Find out what imports are actually come from other chunks
@@ -171,7 +173,7 @@ impl<'a> GenerateStage<'a> {
171173 } ) ;
172174 // Check if the import is from another chunk
173175 if chunk_id != importee_chunk_id {
174- index_cross_chunk_imports [ chunk_id] . insert ( importee_chunk_id) ;
176+ cross_chunk_imports [ chunk_id] . push ( importee_chunk_id) ;
175177 let imports_from_other_chunks = & mut imports_from_other_chunks_vec[ chunk_id] ;
176178 imports_from_other_chunks
177179 . entry ( importee_chunk_id)
@@ -234,47 +236,29 @@ impl<'a> GenerateStage<'a> {
234236 }
235237 }
236238
237- let index_sorted_cross_chunk_imports = index_cross_chunk_imports
238- . into_iter ( )
239- // FIXME: Extra traversing. This is a workaround due to `par_bridge` doesn't ensure order https://github.com/rayon-rs/rayon/issues/551#issuecomment-882069261
240- . collect :: < Vec < _ > > ( )
241- . into_par_iter ( )
242- . map ( |cross_chunk_imports| {
243- let mut cross_chunk_imports = cross_chunk_imports. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
244- cross_chunk_imports. sort_by_cached_key ( |chunk_id| {
245- let mut resource_ids = chunk_graph. chunks [ * chunk_id]
246- . modules
247- . iter ( )
248- . map ( |id| {
249- self . link_output . module_table . normal_modules [ * id] . resource_id . expect_file ( ) . as_str ( )
250- } )
251- . collect :: < Vec < _ > > ( ) ;
252- resource_ids. sort_unstable ( ) ;
253- resource_ids
254- } ) ;
255- cross_chunk_imports
256- } )
257- . collect :: < Vec < _ > > ( ) ;
258-
259239 chunk_graph
260240 . chunks
261241 . iter_mut ( )
262242 . zip (
263243 imports_from_other_chunks_vec. into_iter ( ) . zip (
264244 chunk_meta_imports_from_external_modules_vec
265245 . into_iter ( )
266- . zip ( index_sorted_cross_chunk_imports ) ,
246+ . zip ( cross_chunk_imports . into_iter ( ) . zip ( cross_chunk_dynamic_imports ) ) ,
267247 ) ,
268248 )
269249 . par_bridge ( )
270250 . for_each (
271251 |(
272252 chunk,
273- ( imports_from_other_chunks, ( imports_from_external_modules, cross_chunk_imports) ) ,
253+ (
254+ imports_from_other_chunks,
255+ ( imports_from_external_modules, ( cross_chunk_imports, cross_chunk_dynamic_imports) ) ,
256+ ) ,
274257 ) | {
275258 chunk. imports_from_other_chunks = imports_from_other_chunks;
276259 chunk. imports_from_external_modules = imports_from_external_modules;
277260 chunk. cross_chunk_imports = cross_chunk_imports;
261+ chunk. cross_chunk_dynamic_imports = cross_chunk_dynamic_imports;
278262 } ,
279263 ) ;
280264 }
0 commit comments