From be398efed51f8e26270979529d20eec60de32365 Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 19 Dec 2024 16:17:23 +0800 Subject: [PATCH] u --- crates/oxc_mangler/src/lib.rs | 2 +- crates/oxc_semantic/src/builder.rs | 14 ++++------- crates/oxc_semantic/src/symbol.rs | 28 ++++++++++++---------- crates/oxc_traverse/src/context/scoping.rs | 22 ++++++++++------- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/crates/oxc_mangler/src/lib.rs b/crates/oxc_mangler/src/lib.rs index 1eac2ef4736969..7e74e571f9ef5d 100644 --- a/crates/oxc_mangler/src/lib.rs +++ b/crates/oxc_mangler/src/lib.rs @@ -192,7 +192,7 @@ impl Mangler { // rename the variables for (symbol_to_rename, new_name) in symbols_to_rename_with_new_names { for &symbol_id in &symbol_to_rename.symbol_ids { - symbol_table.set_name(symbol_id, new_name.clone()); + symbol_table.set_name(symbol_id, new_name.as_str()); } } } diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index db622d3dbcb9e9..58fe32eac96446 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -399,14 +399,10 @@ impl<'a> SemanticBuilder<'a> { return symbol_id; } + let symbol_id = + self.symbols.create_symbol(span, name, includes, scope_id, self.current_node_id); + let name = CompactStr::new(name); - let symbol_id = self.symbols.create_symbol( - span, - name.clone(), - includes, - scope_id, - self.current_node_id, - ); self.scope.add_binding(scope_id, name, symbol_id); symbol_id } @@ -466,14 +462,14 @@ impl<'a> SemanticBuilder<'a> { scope_id: ScopeId, includes: SymbolFlags, ) -> SymbolId { - let name = CompactStr::new(name); let symbol_id = self.symbols.create_symbol( span, - name.clone(), + name, includes, self.current_scope_id, self.current_node_id, ); + let name = CompactStr::new(name); self.scope.get_bindings_mut(scope_id).insert(name, symbol_id); symbol_id } diff --git a/crates/oxc_semantic/src/symbol.rs b/crates/oxc_semantic/src/symbol.rs index ef45beed94bf90..567e7ba59e13aa 100644 --- a/crates/oxc_semantic/src/symbol.rs +++ b/crates/oxc_semantic/src/symbol.rs @@ -1,9 +1,9 @@ use std::mem; -use oxc_allocator::{Allocator, Vec as ArenaVec}; +use oxc_allocator::{Allocator, FromIn, Vec as ArenaVec}; use oxc_ast::ast::{Expression, IdentifierReference}; use oxc_index::{Idx, IndexVec}; -use oxc_span::{CompactStr, Span}; +use oxc_span::{Atom, Span}; use oxc_syntax::{ node::NodeId, reference::ReferenceId, @@ -22,7 +22,6 @@ use crate::reference::Reference; /// That ID indexes into `redeclarations` where the actual `Vec` is stored. pub struct SymbolTable { pub(crate) spans: IndexVec, - pub(crate) names: IndexVec, pub(crate) flags: IndexVec, pub(crate) scope_ids: IndexVec, /// Pointer to the AST Node where this symbol is declared @@ -39,13 +38,13 @@ impl Default for SymbolTable { let allocator = Allocator::default(); Self { spans: IndexVec::new(), - names: IndexVec::new(), flags: IndexVec::new(), scope_ids: IndexVec::new(), declarations: IndexVec::new(), redeclarations: IndexVec::new(), references: IndexVec::new(), inner: SymbolTableCell::new(allocator, |allocator| SymbolTableInner { + names: ArenaVec::new_in(allocator), resolved_references: ArenaVec::new_in(allocator), redeclaration_spans: ArenaVec::new_in(allocator), }), @@ -62,6 +61,7 @@ self_cell::self_cell!( ); struct SymbolTableInner<'a> { + names: ArenaVec<'a, Atom<'a>>, resolved_references: ArenaVec<'a, ArenaVec<'a, ReferenceId>>, redeclaration_spans: ArenaVec<'a, ArenaVec<'a, Span>>, } @@ -79,8 +79,8 @@ impl SymbolTable { self.spans.is_empty() } - pub fn names(&self) -> impl Iterator + '_ { - self.names.iter() + pub fn names(&self) -> impl Iterator + '_ { + self.inner.borrow_dependent().names.iter().map(|name| name.as_str()) } pub fn resolved_references(&self) -> impl Iterator> + '_ { @@ -124,15 +124,19 @@ impl SymbolTable { /// Get the identifier name a symbol is bound to. #[inline] pub fn get_name(&self, symbol_id: SymbolId) -> &str { - &self.names[symbol_id] + &self.inner.borrow_dependent().names[symbol_id.index()] } /// Rename a symbol. /// /// Returns the old name. #[inline] - pub fn set_name(&mut self, symbol_id: SymbolId, name: CompactStr) -> CompactStr { - mem::replace(&mut self.names[symbol_id], name) + pub fn set_name(&mut self, symbol_id: SymbolId, name: &str) -> &str { + self.inner + .with_dependent_mut(|allocator, inner| { + mem::replace(&mut inner.names[symbol_id.index()], Atom::from_in(name, allocator)) + }) + .as_str() } /// Get the [`SymbolFlags`] for a symbol, which describe how the symbol is declared. @@ -192,17 +196,17 @@ impl SymbolTable { pub fn create_symbol( &mut self, span: Span, - name: CompactStr, + name: &str, flags: SymbolFlags, scope_id: ScopeId, node_id: NodeId, ) -> SymbolId { self.spans.push(span); - self.names.push(name); self.flags.push(flags); self.scope_ids.push(scope_id); self.declarations.push(node_id); self.inner.with_dependent_mut(|allocator, inner| { + inner.names.push(Atom::from_in(name, allocator)); inner.resolved_references.push(ArenaVec::new_in(allocator)); }); self.redeclarations.push(None) @@ -303,11 +307,11 @@ impl SymbolTable { pub fn reserve(&mut self, additional_symbols: usize, additional_references: usize) { self.spans.reserve(additional_symbols); - self.names.reserve(additional_symbols); self.flags.reserve(additional_symbols); self.scope_ids.reserve(additional_symbols); self.declarations.reserve(additional_symbols); self.inner.with_dependent_mut(|_allocator, inner| { + inner.names.reserve(additional_symbols); inner.resolved_references.reserve(additional_symbols); }); self.references.reserve(additional_references); diff --git a/crates/oxc_traverse/src/context/scoping.rs b/crates/oxc_traverse/src/context/scoping.rs index 5982c9abcd841e..9d1ae635dde9c8 100644 --- a/crates/oxc_traverse/src/context/scoping.rs +++ b/crates/oxc_traverse/src/context/scoping.rs @@ -176,7 +176,7 @@ impl TraverseScoping { flags: SymbolFlags, ) -> SymbolId { let symbol_id = - self.symbols.create_symbol(SPAN, name.clone(), flags, scope_id, NodeId::DUMMY); + self.symbols.create_symbol(SPAN, name.as_str(), flags, scope_id, NodeId::DUMMY); self.scopes.add_binding(scope_id, name, symbol_id); symbol_id @@ -374,7 +374,7 @@ impl TraverseScoping { /// Panics in debug mode if either of the above are not satisfied. pub fn rename_symbol(&mut self, symbol_id: SymbolId, scope_id: ScopeId, new_name: CompactStr) { // Rename symbol - let old_name = self.symbols.set_name(symbol_id, new_name.clone()); + let old_name = self.symbols.set_name(symbol_id, new_name.as_str()); // Rename binding self.scopes.rename_binding(scope_id, symbol_id, &old_name, new_name); } @@ -422,14 +422,18 @@ impl TraverseScoping { self.scopes .root_unresolved_references() .keys() + .map(CompactStr::as_str) .chain(self.symbols.names()) - .filter_map(|name| { - if name.as_bytes().first() == Some(&b'_') { - Some(name.clone()) - } else { - None - } - }) + .filter_map( + |name| { + if name.as_bytes().first() == Some(&b'_') { + Some(name) + } else { + None + } + }, + ) + .map(CompactStr::from) .collect() } }