Skip to content

Commit 16c2ec4

Browse files
committed
perf(semantic)!: remove SymbolTable::spans field
1 parent b60bdf1 commit 16c2ec4

File tree

12 files changed

+40
-47
lines changed

12 files changed

+40
-47
lines changed

crates/oxc_linter/src/rules/eslint/no_class_assign.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ declare_oxc_lint!(
3535

3636
impl Rule for NoClassAssign {
3737
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
38-
let symbol_table = ctx.semantic().symbols();
38+
let semantic = &**ctx.semantic();
39+
let symbol_table = semantic.symbols();
3940
if symbol_table.get_flag(symbol_id).is_class() {
4041
for reference in symbol_table.get_resolved_references(symbol_id) {
4142
if reference.is_write() {
4243
ctx.diagnostic(no_class_assign_diagnostic(
4344
symbol_table.get_name(symbol_id),
44-
symbol_table.get_span(symbol_id),
45+
semantic.symbol_span(symbol_id),
4546
ctx.semantic().reference_span(reference),
4647
));
4748
}

crates/oxc_linter/src/rules/eslint/no_const_assign.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ declare_oxc_lint!(
3434

3535
impl Rule for NoConstAssign {
3636
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext<'_>) {
37-
let symbol_table = ctx.semantic().symbols();
37+
let semantic = &**ctx.semantic();
38+
let symbol_table = semantic.symbols();
3839
if symbol_table.get_flag(symbol_id).is_const_variable() {
3940
for reference in symbol_table.get_resolved_references(symbol_id) {
4041
if reference.is_write() {
4142
ctx.diagnostic(no_const_assign_diagnostic(
4243
symbol_table.get_name(symbol_id),
43-
symbol_table.get_span(symbol_id),
44+
semantic.symbol_span(symbol_id),
4445
ctx.semantic().reference_span(reference),
4546
));
4647
}

crates/oxc_linter/src/rules/eslint/no_label_var.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Rule for NoLabelVar {
4747
if let Some(symbol_id) =
4848
ctx.scopes().find_binding(node.scope_id(), &labeled_stmt.label.name)
4949
{
50-
let decl_span = ctx.symbols().get_span(symbol_id);
50+
let decl_span = ctx.semantic().symbol_span(symbol_id);
5151
let label_decl = labeled_stmt.span.start;
5252
ctx.diagnostic(no_label_var_diagnostic(
5353
&labeled_stmt.label.name,

crates/oxc_linter/src/rules/eslint/no_shadow_restricted_names.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Rule for NoShadowRestrictedNames {
6060
}
6161
}
6262

63-
let span = ctx.symbols().get_span(symbol_id);
63+
let span = ctx.semantic().symbol_span(symbol_id);
6464
ctx.diagnostic(no_shadow_restricted_names_diagnostic(name, span));
6565

6666
for &span in ctx.symbols().get_redeclarations(symbol_id) {

crates/oxc_linter/src/rules/import/namespace.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ impl Rule for Namespace {
9898
return;
9999
}
100100

101-
let Some(symbol_id) =
102-
ctx.semantic().symbols().get_symbol_id_from_span(entry.local_name.span())
101+
let Some(symbol_id) = ctx.semantic().get_symbol_id_from_span(entry.local_name.span())
103102
else {
104103
return;
105104
};

crates/oxc_linter/src/rules/import/no_named_as_default_member.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl Rule for NoNamedAsDefaultMember {
7171

7272
if !remote_module_record_ref.exported_bindings.is_empty() {
7373
has_members_map.insert(
74-
ctx.symbols().get_symbol_id_from_span(import_entry.local_name.span()).unwrap(),
74+
ctx.semantic().get_symbol_id_from_span(import_entry.local_name.span()).unwrap(),
7575
(remote_module_record_ref, import_entry.module_request.name().clone()),
7676
);
7777
}

crates/oxc_semantic/src/binder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ impl<'a> Binder<'a> for CatchParameter<'a> {
254254
// unless CatchParameter is CatchParameter : BindingIdentifier
255255
if let BindingPatternKind::BindingIdentifier(ident) = &self.pattern.kind {
256256
let includes = SymbolFlags::FunctionScopedVariable | SymbolFlags::CatchVariable;
257-
let symbol_id =
258-
builder.declare_shadow_symbol(&ident.name, ident.span, current_scope_id, includes);
257+
let symbol_id = builder.declare_shadow_symbol(&ident.name, current_scope_id, includes);
259258
ident.symbol_id.set(Some(symbol_id));
260259
} else {
261260
self.pattern.bound_names(&mut |ident| {

crates/oxc_semantic/src/builder.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use oxc_cfg::{
1313
IterationInstructionKind, ReturnInstructionKind,
1414
};
1515
use oxc_diagnostics::OxcDiagnostic;
16-
use oxc_span::{Atom, CompactStr, SourceType, Span};
16+
use oxc_span::{Atom, CompactStr, GetSpan, SourceType, Span};
1717
use oxc_syntax::{module_record::ModuleRecord, operator::AssignmentOperator};
1818
use rustc_hash::FxHashMap;
1919

@@ -343,13 +343,8 @@ impl<'a> SemanticBuilder<'a> {
343343

344344
let includes = includes | self.current_symbol_flags;
345345
let name = CompactStr::new(name);
346-
let symbol_id = self.symbols.create_symbol(
347-
span,
348-
name.clone(),
349-
includes,
350-
scope_id,
351-
self.current_node_id,
352-
);
346+
let symbol_id =
347+
self.symbols.create_symbol(name.clone(), includes, scope_id, self.current_node_id);
353348
self.scope.add_binding(scope_id, name, symbol_id);
354349
symbol_id
355350
}
@@ -376,7 +371,8 @@ impl<'a> SemanticBuilder<'a> {
376371
self.hoisting_variables.get(&scope_id).and_then(|symbols| symbols.get(name).copied())
377372
})?;
378373
if report_error && self.symbols.get_flag(symbol_id).intersects(excludes) {
379-
let symbol_span = self.symbols.get_span(symbol_id);
374+
let declaration_node_id = self.symbols.get_declaration(symbol_id);
375+
let symbol_span = self.nodes.kind(declaration_node_id).span();
380376
self.error(redeclaration(name, symbol_span, span));
381377
}
382378
Some(symbol_id)
@@ -401,14 +397,12 @@ impl<'a> SemanticBuilder<'a> {
401397
pub fn declare_shadow_symbol(
402398
&mut self,
403399
name: &str,
404-
span: Span,
405400
scope_id: ScopeId,
406401
includes: SymbolFlags,
407402
) -> SymbolId {
408403
let includes = includes | self.current_symbol_flags;
409404
let name = CompactStr::new(name);
410405
let symbol_id = self.symbols.create_symbol(
411-
span,
412406
name.clone(),
413407
includes,
414408
self.current_scope_id,

crates/oxc_semantic/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ impl<'a> Semantic<'a> {
136136
self.nodes.get_node(self.symbols.get_declaration(symbol_id))
137137
}
138138

139+
pub fn symbol_span(&self, symbol_id: SymbolId) -> Span {
140+
self.symbol_declaration(symbol_id).kind().span()
141+
}
142+
139143
pub fn is_reference_to_global_variable(&self, ident: &IdentifierReference) -> bool {
140144
self.scopes().root_unresolved_references().contains_key(ident.name.as_str())
141145
}
@@ -153,6 +157,21 @@ impl<'a> Semantic<'a> {
153157
let node = self.nodes.get_node(reference.node_id());
154158
node.kind().span()
155159
}
160+
161+
pub fn get_symbol_id_from_span(&self, span: Span) -> Option<SymbolId> {
162+
self.symbols.declarations.iter_enumerated().find_map(|(symbol_id, &node_id)| {
163+
let inner_span = self.nodes.kind(node_id).span();
164+
if inner_span == span {
165+
Some(symbol_id)
166+
} else {
167+
None
168+
}
169+
})
170+
}
171+
172+
pub fn get_scope_id_from_span(&self, span: Span) -> Option<ScopeId> {
173+
self.get_symbol_id_from_span(span).map(|symbol_id| self.symbols.get_scope_id(symbol_id))
174+
}
156175
}
157176

158177
#[cfg(test)]

crates/oxc_semantic/src/symbol.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export type IndexVec<I, T> = Array<T>;
3333
#[derive(Debug, Default)]
3434
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify), serde(rename_all = "camelCase"))]
3535
pub struct SymbolTable {
36-
pub spans: IndexVec<SymbolId, Span>,
3736
pub names: IndexVec<SymbolId, CompactStr>,
3837
pub flags: IndexVec<SymbolId, SymbolFlags>,
3938
pub scope_ids: IndexVec<SymbolId, ScopeId>,
@@ -49,25 +48,19 @@ pub struct SymbolTable {
4948

5049
impl SymbolTable {
5150
pub fn len(&self) -> usize {
52-
self.spans.len()
51+
self.names.len()
5352
}
5453

5554
pub fn is_empty(&self) -> bool {
5655
self.len() == 0
5756
}
5857

5958
pub fn iter(&self) -> impl Iterator<Item = SymbolId> + '_ {
60-
self.spans.iter_enumerated().map(|(symbol_id, _)| symbol_id)
59+
self.flags.iter_enumerated().map(|(symbol_id, _)| symbol_id)
6160
}
6261

6362
pub fn iter_rev(&self) -> impl Iterator<Item = SymbolId> + '_ {
64-
self.spans.iter_enumerated().rev().map(|(symbol_id, _)| symbol_id)
65-
}
66-
67-
pub fn get_symbol_id_from_span(&self, span: Span) -> Option<SymbolId> {
68-
self.spans
69-
.iter_enumerated()
70-
.find_map(|(symbol, &inner_span)| if inner_span == span { Some(symbol) } else { None })
63+
self.flags.iter_enumerated().rev().map(|(symbol_id, _)| symbol_id)
7164
}
7265

7366
pub fn get_symbol_id_from_name(&self, name: &str) -> Option<SymbolId> {
@@ -80,10 +73,6 @@ impl SymbolTable {
8073
})
8174
}
8275

83-
pub fn get_span(&self, symbol_id: SymbolId) -> Span {
84-
self.spans[symbol_id]
85-
}
86-
8776
pub fn get_name(&self, symbol_id: SymbolId) -> &str {
8877
&self.names[symbol_id]
8978
}
@@ -113,10 +102,6 @@ impl SymbolTable {
113102
self.scope_ids[symbol_id]
114103
}
115104

116-
pub fn get_scope_id_from_span(&self, span: Span) -> Option<ScopeId> {
117-
self.get_symbol_id_from_span(span).map(|symbol_id| self.get_scope_id(symbol_id))
118-
}
119-
120105
pub fn get_scope_id_from_name(&self, name: &str) -> Option<ScopeId> {
121106
self.get_symbol_id_from_name(name).map(|symbol_id| self.get_scope_id(symbol_id))
122107
}
@@ -127,13 +112,11 @@ impl SymbolTable {
127112

128113
pub fn create_symbol(
129114
&mut self,
130-
span: Span,
131115
name: CompactStr,
132116
flag: SymbolFlags,
133117
scope_id: ScopeId,
134118
node_id: AstNodeId,
135119
) -> SymbolId {
136-
self.spans.push(span);
137120
self.names.push(name);
138121
self.flags.push(flag);
139122
self.scope_ids.push(scope_id);
@@ -210,7 +193,6 @@ impl SymbolTable {
210193
}
211194

212195
pub fn reserve(&mut self, additional_symbols: usize, additional_references: usize) {
213-
self.spans.reserve(additional_symbols);
214196
self.names.reserve(additional_symbols);
215197
self.flags.reserve(additional_symbols);
216198
self.scope_ids.reserve(additional_symbols);

0 commit comments

Comments
 (0)