Skip to content

Commit e507c56

Browse files
committed
refactor(semantic): use FxHashMap to replace IndexVec for symbol_redeclarations
1 parent 435bfba commit e507c56

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

crates/oxc_semantic/src/scoping.rs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::{fmt, mem};
1+
use std::{collections::hash_map::Entry, fmt, mem};
22

3-
use rustc_hash::FxHashSet;
3+
use rustc_hash::{FxHashMap, FxHashSet};
44

55
use oxc_allocator::{Allocator, CloneIn, FromIn, HashMap as ArenaHashMap, Vec as ArenaVec};
66
use oxc_index::{Idx, IndexVec};
@@ -9,7 +9,7 @@ use oxc_syntax::{
99
node::NodeId,
1010
reference::{Reference, ReferenceId},
1111
scope::{ScopeFlags, ScopeId},
12-
symbol::{RedeclarationId, SymbolFlags, SymbolId},
12+
symbol::{SymbolFlags, SymbolId},
1313
};
1414

1515
pub type Bindings<'a> = ArenaHashMap<'a, &'a str, SymbolId>;
@@ -40,7 +40,6 @@ pub struct Scoping {
4040
pub(crate) symbol_scope_ids: IndexVec<SymbolId, ScopeId>,
4141
/// Pointer to the AST Node where this symbol is declared
4242
pub(crate) symbol_declarations: IndexVec<SymbolId, NodeId>,
43-
symbol_redeclarations: IndexVec<SymbolId, Option<RedeclarationId>>,
4443

4544
pub(crate) references: IndexVec<ReferenceId, Reference>,
4645

@@ -75,7 +74,6 @@ impl Default for Scoping {
7574
symbol_flags: IndexVec::new(),
7675
symbol_scope_ids: IndexVec::new(),
7776
symbol_declarations: IndexVec::new(),
78-
symbol_redeclarations: IndexVec::new(),
7977
references: IndexVec::new(),
8078
no_side_effects: FxHashSet::default(),
8179
scope_parent_ids: IndexVec::new(),
@@ -85,7 +83,7 @@ impl Default for Scoping {
8583
cell: ScopingCell::new(Allocator::default(), |allocator| ScopingInner {
8684
symbol_names: ArenaVec::new_in(allocator),
8785
resolved_references: ArenaVec::new_in(allocator),
88-
redeclaration_spans: ArenaVec::new_in(allocator),
86+
symbol_redeclarations: FxHashMap::default(),
8987
bindings: IndexVec::new(),
9088
scope_child_ids: ArenaVec::new_in(allocator),
9189
root_unresolved_references: UnresolvedReferences::new_in(allocator),
@@ -106,7 +104,7 @@ pub struct ScopingInner<'cell> {
106104
/* Symbol Table Fields */
107105
symbol_names: ArenaVec<'cell, Atom<'cell>>,
108106
resolved_references: ArenaVec<'cell, ArenaVec<'cell, ReferenceId>>,
109-
redeclaration_spans: ArenaVec<'cell, ArenaVec<'cell, Span>>,
107+
symbol_redeclarations: FxHashMap<SymbolId, ArenaVec<'cell, Span>>,
110108
/* Scope Tree Fields */
111109
/// Symbol bindings in a scope.
112110
///
@@ -196,12 +194,13 @@ impl Scoping {
196194

197195
#[inline]
198196
pub fn symbol_redeclarations(&self, symbol_id: SymbolId) -> &[Span] {
199-
if let Some(redeclaration_id) = self.symbol_redeclarations[symbol_id] {
200-
&self.cell.borrow_dependent().redeclaration_spans[redeclaration_id.index()]
201-
} else {
202-
static EMPTY: &[Span] = &[];
203-
EMPTY
204-
}
197+
self.cell.borrow_dependent().symbol_redeclarations.get(&symbol_id).map_or_else(
198+
|| {
199+
static EMPTY: &[Span] = &[];
200+
EMPTY
201+
},
202+
|spans| spans.as_slice(),
203+
)
205204
}
206205

207206
#[inline]
@@ -242,31 +241,27 @@ impl Scoping {
242241
scope_id: ScopeId,
243242
node_id: NodeId,
244243
) -> SymbolId {
245-
self.symbol_spans.push(span);
246-
self.symbol_flags.push(flags);
247-
self.symbol_scope_ids.push(scope_id);
248-
self.symbol_declarations.push(node_id);
249244
self.cell.with_dependent_mut(|allocator, cell| {
250245
cell.symbol_names.push(Atom::from_in(name, allocator));
251246
cell.resolved_references.push(ArenaVec::new_in(allocator));
252247
});
253-
self.symbol_redeclarations.push(None)
248+
self.symbol_spans.push(span);
249+
self.symbol_flags.push(flags);
250+
self.symbol_scope_ids.push(scope_id);
251+
self.symbol_declarations.push(node_id)
254252
}
255253

256254
pub fn add_symbol_redeclaration(&mut self, symbol_id: SymbolId, span: Span) {
257-
if let Some(redeclaration_id) = self.symbol_redeclarations[symbol_id] {
258-
self.cell.with_dependent_mut(|_, cell| {
259-
cell.redeclaration_spans[redeclaration_id.index()].push(span);
260-
});
261-
} else {
262-
self.cell.with_dependent_mut(|allocator, cell| {
263-
let v = ArenaVec::from_array_in([span], allocator);
264-
let redeclaration_id = cell.redeclaration_spans.len();
265-
cell.redeclaration_spans.push(v);
266-
self.symbol_redeclarations[symbol_id] =
267-
Some(RedeclarationId::from_usize(redeclaration_id));
268-
});
269-
};
255+
self.cell.with_dependent_mut(|allocator, cell| {
256+
match cell.symbol_redeclarations.entry(symbol_id) {
257+
Entry::Occupied(mut entry) => {
258+
entry.get_mut().push(span);
259+
}
260+
Entry::Vacant(entry) => {
261+
entry.insert(ArenaVec::from_array_in([span], allocator));
262+
}
263+
}
264+
});
270265
}
271266

272267
pub fn create_reference(&mut self, reference: Reference) -> ReferenceId {
@@ -779,7 +774,6 @@ impl Scoping {
779774
symbol_flags: self.symbol_flags.clone(),
780775
symbol_scope_ids: self.symbol_scope_ids.clone(),
781776
symbol_declarations: self.symbol_declarations.clone(),
782-
symbol_redeclarations: self.symbol_redeclarations.clone(),
783777
references: self.references.clone(),
784778
no_side_effects: self.no_side_effects.clone(),
785779
scope_parent_ids: self.scope_parent_ids.clone(),
@@ -793,9 +787,11 @@ impl Scoping {
793787
resolved_references: cell
794788
.resolved_references
795789
.clone_in_with_semantic_ids(allocator),
796-
redeclaration_spans: cell
797-
.redeclaration_spans
798-
.clone_in_with_semantic_ids(allocator),
790+
symbol_redeclarations: cell
791+
.symbol_redeclarations
792+
.iter()
793+
.map(|(k, v)| (*k, v.clone_in_with_semantic_ids(allocator)))
794+
.collect(),
799795
bindings: cell
800796
.bindings
801797
.iter()

0 commit comments

Comments
 (0)