@@ -7,6 +7,13 @@ use rolldown_common::{ModuleId, SymbolRef};
77use rolldown_utils:: reserved_word:: is_reserved_word;
88use rustc_hash:: FxHashMap ;
99
10+ #[ derive( Debug ) ]
11+ pub struct Symbol {
12+ pub name : Atom ,
13+ /// The symbol that this symbol is linked to.
14+ pub link : Option < SymbolRef > ,
15+ }
16+
1017#[ derive( Debug , Default ) ]
1118pub struct SymbolMap {
1219 pub names : IndexVec < SymbolId , Atom > ,
@@ -28,26 +35,32 @@ impl SymbolMap {
2835 self . names . push ( name)
2936 }
3037 }
31-
32- pub fn create_reference ( & mut self , id : Option < SymbolId > ) -> ReferenceId {
33- self . references . push ( id)
34- }
35-
36- pub fn get_name ( & self , id : SymbolId ) -> & Atom {
37- & self . names [ id]
38- }
3938}
4039
4140// Information about symbols for all modules
4241#[ derive( Debug , Default ) ]
4342pub struct Symbols {
44- pub ( crate ) tables : IndexVec < ModuleId , SymbolMap > ,
45- canonical_refs : IndexVec < ModuleId , FxHashMap < SymbolId , SymbolRef > > ,
43+ inner : IndexVec < ModuleId , IndexVec < SymbolId , Symbol > > ,
44+ pub ( crate ) references_table : IndexVec < ModuleId , IndexVec < ReferenceId , Option < SymbolId > > > ,
4645}
4746
4847impl Symbols {
4948 pub fn new ( tables : IndexVec < ModuleId , SymbolMap > ) -> Self {
50- Self { canonical_refs : tables. iter ( ) . map ( |_table| FxHashMap :: default ( ) ) . collect ( ) , tables }
49+ let mut reference_table = IndexVec :: with_capacity ( tables. len ( ) ) ;
50+ let inner = tables
51+ . into_iter ( )
52+ . map ( |table| {
53+ reference_table. push ( table. references ) ;
54+ table. names . into_iter ( ) . map ( |name| Symbol { name, link : None } ) . collect ( )
55+ } )
56+ . collect ( ) ;
57+
58+ Self { inner, references_table : reference_table }
59+ }
60+
61+ pub fn create_symbol ( & mut self , owner : ModuleId , name : Atom ) -> SymbolRef {
62+ let symbol_id = self . inner [ owner] . push ( Symbol { name, link : None } ) ;
63+ SymbolRef { owner, symbol : symbol_id }
5164 }
5265
5366 /// Make a point to b
@@ -58,29 +71,35 @@ impl Symbols {
5871 if root_a == root_b {
5972 return ;
6073 }
61- self . canonical_refs [ a . owner ] . insert ( a . symbol , root_b) ;
74+ self . get_mut ( root_a ) . link = Some ( root_b) ;
6275 }
6376
6477 pub fn get_original_name ( & self , refer : SymbolRef ) -> & Atom {
65- self . tables [ refer. owner ] . get_name ( refer. symbol )
78+ & self . get ( refer) . name
79+ }
80+
81+ pub fn get ( & self , refer : SymbolRef ) -> & Symbol {
82+ & self . inner [ refer. owner ] [ refer. symbol ]
83+ }
84+
85+ pub fn get_mut ( & mut self , refer : SymbolRef ) -> & mut Symbol {
86+ & mut self . inner [ refer. owner ] [ refer. symbol ]
6687 }
6788
6889 pub fn get_canonical_ref ( & mut self , target : SymbolRef ) -> SymbolRef {
69- let mut canonical = target;
70- while let Some ( founded) = self . canonical_refs [ canonical. owner ] . get ( & canonical. symbol ) . copied ( ) {
71- debug_assert ! ( founded != target) ;
72- canonical = founded;
73- }
90+ let canonical = self . par_get_canonical_ref ( target) ;
7491 if target != canonical {
75- self . canonical_refs [ target. owner ] . insert ( target. symbol , canonical) ;
92+ // update the link to the canonical so that the next time we can get the canonical directly
93+ self . get_mut ( target) . link = Some ( canonical) ;
7694 }
7795 canonical
7896 }
7997
98+ // Used for the situation where rust require `&self`
8099 pub fn par_get_canonical_ref ( & self , target : SymbolRef ) -> SymbolRef {
81100 let mut canonical = target;
82- while let Some ( founded) = self . canonical_refs [ canonical . owner ] . get ( & canonical. symbol ) . copied ( ) {
83- debug_assert ! ( founded != canonical ) ;
101+ while let Some ( founded) = self . get ( canonical) . link {
102+ debug_assert ! ( founded != target ) ;
84103 canonical = founded;
85104 }
86105 canonical
@@ -95,14 +114,3 @@ pub fn get_symbol_final_name<'a>(
95114 let final_ref = symbols. par_get_canonical_ref ( symbol) ;
96115 final_names. get ( & final_ref)
97116}
98-
99- #[ allow( dead_code) ]
100- pub fn get_reference_final_name < ' a > (
101- module_id : ModuleId ,
102- reference_id : ReferenceId ,
103- symbols : & ' a Symbols ,
104- final_names : & ' a FxHashMap < SymbolRef , Atom > ,
105- ) -> Option < & ' a Atom > {
106- symbols. tables [ module_id] . references [ reference_id]
107- . and_then ( |symbol| get_symbol_final_name ( ( module_id, symbol) . into ( ) , symbols, final_names) )
108- }
0 commit comments