From 30a869ead27d5d31b999492130e2f17da8a6b07f Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:29:57 +0000 Subject: [PATCH] perf(semantic): use `oxc_allocator::HashMap` in `ScopeTree` (#8554) Use `oxc_allocator::HashMap` in `ScopeTree`, replacing `hashbrown::HashMap`. `oxc_allocator::HashMap` is non-drop, so it may reduce drop time of `ScopeTree` a little. --- Cargo.lock | 1 - crates/oxc_semantic/Cargo.toml | 1 - crates/oxc_semantic/src/builder.rs | 3 +-- crates/oxc_semantic/src/scope.rs | 16 +++++----------- 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ce7ed0c069e14..2fa54cd0f76bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2034,7 +2034,6 @@ name = "oxc_semantic" version = "0.46.0" dependencies = [ "assert-unchecked", - "hashbrown 0.15.2", "insta", "itertools", "oxc_allocator", diff --git a/crates/oxc_semantic/Cargo.toml b/crates/oxc_semantic/Cargo.toml index c2381f31888de..101c96d6d1cde 100644 --- a/crates/oxc_semantic/Cargo.toml +++ b/crates/oxc_semantic/Cargo.toml @@ -30,7 +30,6 @@ oxc_span = { workspace = true } oxc_syntax = { workspace = true } assert-unchecked = { workspace = true } -hashbrown = { workspace = true, features = ["allocator-api2"] } itertools = { workspace = true } phf = { workspace = true, features = ["macros"] } rustc-hash = { workspace = true } diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index dfe5a4b892d8b..81b9e211b7a21 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -712,8 +712,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> { if self.scope.get_flags(parent_scope_id).is_catch_clause() { self.scope.cell.with_dependent_mut(|allocator, inner| { if !inner.bindings[parent_scope_id].is_empty() { - let mut parent_bindings = - Bindings::with_hasher_in(rustc_hash::FxBuildHasher, allocator); + let mut parent_bindings = Bindings::new_in(allocator); mem::swap(&mut inner.bindings[parent_scope_id], &mut parent_bindings); for &symbol_id in parent_bindings.values() { self.symbols.set_scope_id(symbol_id, self.current_scope_id); diff --git a/crates/oxc_semantic/src/scope.rs b/crates/oxc_semantic/src/scope.rs index 3edc5ff6b405f..a2bdffb519e74 100644 --- a/crates/oxc_semantic/src/scope.rs +++ b/crates/oxc_semantic/src/scope.rs @@ -1,8 +1,6 @@ use std::{fmt, mem}; -use rustc_hash::FxBuildHasher; - -use oxc_allocator::{Allocator, Vec as ArenaVec}; +use oxc_allocator::{Allocator, HashMap as ArenaHashMap, Vec as ArenaVec}; use oxc_index::{Idx, IndexVec}; use oxc_syntax::{ node::NodeId, @@ -13,9 +11,8 @@ use oxc_syntax::{ use crate::SymbolTable; -pub(crate) type Bindings<'a> = hashbrown::HashMap<&'a str, SymbolId, FxBuildHasher, &'a Allocator>; -pub type UnresolvedReferences<'a> = - hashbrown::HashMap<&'a str, ArenaVec<'a, ReferenceId>, FxBuildHasher, &'a Allocator>; +pub(crate) type Bindings<'a> = ArenaHashMap<'a, &'a str, SymbolId>; +pub type UnresolvedReferences<'a> = ArenaHashMap<'a, &'a str, ArenaVec<'a, ReferenceId>>; /// Scope Tree /// @@ -58,10 +55,7 @@ impl Default for ScopeTree { cell: ScopeTreeCell::new(Allocator::default(), |allocator| ScopeTreeInner { bindings: IndexVec::new(), child_ids: ArenaVec::new_in(allocator), - root_unresolved_references: UnresolvedReferences::with_hasher_in( - FxBuildHasher, - allocator, - ), + root_unresolved_references: UnresolvedReferences::new_in(allocator), }), } } @@ -384,7 +378,7 @@ impl ScopeTree { let scope_id = self.parent_ids.push(parent_id); self.flags.push(flags); self.cell.with_dependent_mut(|allocator, inner| { - inner.bindings.push(Bindings::with_hasher_in(FxBuildHasher, allocator)); + inner.bindings.push(Bindings::new_in(allocator)); }); self.node_ids.push(node_id); if self.build_child_ids {