Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b980d5

Browse files
authored
feat: refactor Symbols and remove a lot of ReferenceId (#93)
1 parent 6576d2c commit 5b980d5

6 files changed

Lines changed: 66 additions & 75 deletions

File tree

crates/rolldown/src/bundler/graph/linker.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'graph> Linker<'graph> {
123123
symbols,
124124
);
125125
importer.generate_symbol_import_and_use(
126-
importee.namespace_symbol.0,
126+
importee.namespace_symbol,
127127
importer_linker_module,
128128
symbols,
129129
);
@@ -366,7 +366,7 @@ impl<'graph> Linker<'graph> {
366366
match importee {
367367
Module::Normal(importee) => {
368368
let resolved_ref = if info.is_imported_star {
369-
importee.namespace_symbol.0
369+
importee.namespace_symbol
370370
} else {
371371
match importee.resolve_export_for_esm_and_cjs(
372372
&info.imported,
@@ -382,12 +382,12 @@ impl<'graph> Linker<'graph> {
382382
linker_module.unresolved_symbols.insert(
383383
info.imported_as,
384384
UnresolvedSymbol {
385-
importee_namespace: importee.namespace_symbol.0,
385+
importee_namespace: importee.namespace_symbol,
386386
reference_name,
387387
},
388388
);
389389
importer.generate_symbol_import_and_use(
390-
importee.namespace_symbol.0,
390+
importee.namespace_symbol,
391391
linker_module,
392392
symbols,
393393
);

crates/rolldown/src/bundler/graph/symbols.rs

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ use rolldown_common::{ModuleId, SymbolRef};
77
use rolldown_utils::reserved_word::is_reserved_word;
88
use 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)]
1118
pub 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)]
4342
pub 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

4847
impl 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-
}

crates/rolldown/src/bundler/module/external_module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl ExternalModule {
3434
self
3535
.symbols_imported_by_others
3636
.entry(symbol.clone())
37-
.or_insert_with(|| (self.id, symbols.tables[self.id].create_symbol(symbol.clone())).into());
37+
.or_insert_with(|| symbols.create_symbol(self.id, symbol.clone()));
3838
}
3939

4040
pub fn resolve_export(&self, exported: &Atom, is_star: bool) -> SymbolRef {

crates/rolldown/src/bundler/module/module_builder.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use index_vec::IndexVec;
22
use oxc::{
3-
semantic::{ReferenceId, ScopeTree, SymbolId},
3+
semantic::{ScopeTree, SymbolId},
44
span::{Atom, Span},
55
};
66
use rolldown_common::{
@@ -27,7 +27,7 @@ pub struct NormalModuleBuilder {
2727
pub star_exports: Option<Vec<ImportRecordId>>,
2828
pub scope: Option<ScopeTree>,
2929
pub default_export_symbol: Option<SymbolId>,
30-
pub namespace_symbol: Option<(SymbolRef, ReferenceId)>,
30+
pub namespace_symbol: Option<SymbolRef>,
3131
pub exports_kind: Option<ExportsKind>,
3232
pub module_type: ModuleType,
3333
pub is_entry: bool,
@@ -37,8 +37,7 @@ impl NormalModuleBuilder {
3737
pub fn initialize_namespace_binding(&mut self, symbol_table: &mut SymbolMap) {
3838
let name = format!("{}_ns", self.path.as_ref().unwrap().generate_unique_name());
3939
let symbol_ref: SymbolRef = (self.id.unwrap(), symbol_table.create_symbol(name.into())).into();
40-
let refer = symbol_table.create_reference(Some(symbol_ref.symbol));
41-
self.namespace_symbol = Some((symbol_ref, refer));
40+
self.namespace_symbol = Some(symbol_ref);
4241
}
4342

4443
pub fn build(self) -> NormalModule {

crates/rolldown/src/bundler/module/normal_module.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt::Debug;
22

33
use index_vec::IndexVec;
44
use oxc::{
5-
semantic::{ReferenceId, ScopeTree, SymbolId},
5+
semantic::{ScopeTree, SymbolId},
66
span::{Atom, Span},
77
};
88
use rolldown_common::{
@@ -42,7 +42,7 @@ pub struct NormalModule {
4242
pub star_exports: Vec<ImportRecordId>,
4343
pub exports_kind: ExportsKind,
4444
pub scope: ScopeTree,
45-
pub namespace_symbol: (SymbolRef, ReferenceId),
45+
pub namespace_symbol: SymbolRef,
4646
pub default_export_symbol: Option<SymbolId>,
4747
}
4848

@@ -105,7 +105,7 @@ impl NormalModule {
105105
if !self_linker_module.is_symbol_for_namespace_referenced {
106106
self_linker_module.is_symbol_for_namespace_referenced = true;
107107
self_linker_module.virtual_stmt_infos.push(VirtualStmtInfo {
108-
declared_symbols: vec![self.namespace_symbol.0.symbol],
108+
declared_symbols: vec![self.namespace_symbol.symbol],
109109
..Default::default()
110110
});
111111
}
@@ -174,7 +174,7 @@ impl NormalModule {
174174
match importee {
175175
Module::Normal(importee) => {
176176
let resolved = if named_import.is_imported_star {
177-
Resolution::Found(importee.namespace_symbol.0)
177+
Resolution::Found(importee.namespace_symbol)
178178
} else {
179179
importee.resolve_export(&named_import.imported, resolve_set, modules, symbols)
180180
};
@@ -199,7 +199,7 @@ impl NormalModule {
199199
match importee {
200200
Module::Normal(importee) => {
201201
if re.is_imported_star {
202-
return Resolution::Found(importee.namespace_symbol.0);
202+
return Resolution::Found(importee.namespace_symbol);
203203
}
204204
importee.resolve_export(&re.imported, resolve_set, modules, symbols)
205205
}
@@ -279,7 +279,7 @@ impl NormalModule {
279279
match importee {
280280
Module::Normal(importee) => {
281281
if importee.exports_kind == ExportsKind::CommonJs {
282-
return Resolution::Runtime(importee.namespace_symbol.0);
282+
return Resolution::Runtime(importee.namespace_symbol);
283283
}
284284
}
285285
Module::External(_) => {}
@@ -292,15 +292,15 @@ impl NormalModule {
292292
match importee {
293293
Module::Normal(importee) => {
294294
if importee.exports_kind == ExportsKind::CommonJs {
295-
return Resolution::Runtime(importee.namespace_symbol.0);
295+
return Resolution::Runtime(importee.namespace_symbol);
296296
}
297297
}
298298
Module::External(_) => {}
299299
}
300300
}
301301
}
302302
} else if has_cjs_star_resolution || self.exports_kind == ExportsKind::CommonJs {
303-
return Resolution::Runtime(self.namespace_symbol.0);
303+
return Resolution::Runtime(self.namespace_symbol);
304304
}
305305
return Resolution::None;
306306
}
@@ -335,7 +335,7 @@ impl NormalModule {
335335
self.resource_id.generate_unique_name()
336336
)
337337
.into();
338-
let symbol = symbols.tables[self.id].create_symbol(name);
338+
let symbol = symbols.create_symbol(self.id, name).symbol;
339339
self_linker_module.wrap_symbol = Some((self.id, symbol).into());
340340
self_linker_module
341341
.virtual_stmt_infos
@@ -362,13 +362,12 @@ impl NormalModule {
362362
self_linker_module: &mut LinkerModule,
363363
symbols: &mut Symbols,
364364
) -> SymbolRef {
365-
let local_symbol = symbols.tables[self.id].create_symbol(name);
366-
let local_symbol_ref = (self.id, local_symbol).into();
365+
let local_symbol_ref = symbols.create_symbol(self.id, name);
367366
self_linker_module.virtual_stmt_infos.push(VirtualStmtInfo {
368367
// FIXME: should store the symbol in `used_symbols` instead of `declared_symbols`.
369368
// The deconflict for runtime symbols would be handled in the deconflict on cross-chunk-imported
370369
// symbols
371-
declared_symbols: vec![local_symbol],
370+
declared_symbols: vec![local_symbol_ref.symbol],
372371
..Default::default()
373372
});
374373
local_symbol_ref

crates/rolldown/src/bundler/visitors/mod.rs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@ pub mod commonjs_source_render;
22
pub mod esm_source_render;
33
pub mod esm_wrap_source_render;
44
pub mod scanner;
5-
use oxc::{
6-
semantic::ReferenceId,
7-
span::{Atom, GetSpan, Span},
8-
};
9-
use rolldown_common::{ExportsKind, ModuleId, ResolvedExport, SymbolRef};
5+
use oxc::span::{Atom, GetSpan, Span};
6+
use rolldown_common::{ExportsKind, ResolvedExport, SymbolRef};
107
use rustc_hash::FxHashMap;
118
use string_wizard::{MagicString, UpdateOptions};
129

1310
use super::{
1411
chunk::chunk_graph::ChunkGraph,
15-
graph::{
16-
graph::Graph,
17-
linker::LinkerModule,
18-
symbols::{get_reference_final_name, get_symbol_final_name},
19-
},
12+
graph::{graph::Graph, linker::LinkerModule, symbols::get_symbol_final_name},
2013
module::{module::Module, NormalModule},
2114
};
2215

@@ -47,7 +40,7 @@ impl<'ast> RendererContext<'ast> {
4740
let wrap_symbol_name =
4841
linker_module.wrap_symbol.and_then(|s| get_symbol_final_name(s, &graph.symbols, final_names));
4942
let namespace_symbol_name = get_symbol_final_name(
50-
(module.id, module.namespace_symbol.0.symbol).into(),
43+
(module.id, module.namespace_symbol.symbol).into(),
5144
&graph.symbols,
5245
final_names,
5346
);
@@ -90,14 +83,6 @@ impl<'ast> RendererContext<'ast> {
9083
get_symbol_final_name(symbol, &self.graph.symbols, self.final_names)
9184
}
9285

93-
pub fn _get_reference_final_name(
94-
&self,
95-
module_id: ModuleId,
96-
reference_id: ReferenceId,
97-
) -> Option<&Atom> {
98-
get_reference_final_name(module_id, reference_id, &self.graph.symbols, self.final_names)
99-
}
100-
10186
pub fn get_runtime_symbol_final_name(&self, name: &Atom) -> &Atom {
10287
let symbol = self.graph.runtime.resolve_symbol(name);
10388
self.get_symbol_final_name(symbol).unwrap()
@@ -143,7 +128,7 @@ impl<'ast> RendererContext<'ast> {
143128
if self.module.module_type.is_esm() { ", 1" } else { "" }
144129
);
145130
if with_namespace_init {
146-
let namespace_name = self.get_symbol_final_name(importee.namespace_symbol.0).unwrap();
131+
let namespace_name = self.get_symbol_final_name(importee.namespace_symbol).unwrap();
147132
format!("var {namespace_name} = {code};\n")
148133
} else {
149134
code
@@ -166,7 +151,7 @@ impl<'ast> RendererContext<'ast> {
166151

167152
pub fn visit_identifier_reference(&mut self, ident: &'ast oxc::ast::ast::IdentifierReference) {
168153
if let Some(symbol_id) =
169-
self.graph.symbols.tables[self.module.id].references[ident.reference_id.get().unwrap()]
154+
self.graph.symbols.references_table[self.module.id][ident.reference_id.get().unwrap()]
170155
{
171156
let symbol_ref = (self.module.id, symbol_id).into();
172157
if let Some(unresolved_symbol) = self.linker_module.unresolved_symbols.get(&symbol_ref) {
@@ -276,7 +261,7 @@ impl<'ast> RendererContext<'ast> {
276261
self.source.update(expr.span.start, expr.span.end, format!("{wrap_symbol_name}()"));
277262
} else {
278263
let namespace_name = self
279-
.get_symbol_final_name((importee.id, importee.namespace_symbol.0.symbol).into())
264+
.get_symbol_final_name((importee.id, importee.namespace_symbol.symbol).into())
280265
.unwrap();
281266
let to_commonjs_runtime_symbol_name =
282267
self.get_runtime_symbol_final_name(&"__toCommonJS".into());

0 commit comments

Comments
 (0)