From c6767fa598c909bf21024cf3c0fcbe75e46abc45 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Fri, 16 Feb 2024 20:48:03 +0800 Subject: [PATCH] refactor(semantic): reduce allocation in resolve_references_for_current_scope (#2414) --- crates/oxc_semantic/src/builder.rs | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 69c41363f..96d8b6810 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -8,7 +8,6 @@ use oxc_ast::{ast::*, AstKind, Trivias, TriviasMap, Visit}; use oxc_diagnostics::Error; use oxc_span::{Atom, SourceType, Span}; use oxc_syntax::{module_record::ModuleRecord, operator::AssignmentOperator}; -use rustc_hash::FxHashMap; use crate::{ binder::Binder, @@ -319,28 +318,17 @@ impl<'a> SemanticBuilder<'a> { .drain() .collect::)>>(); - let mut unresolved_references: FxHashMap> = FxHashMap::default(); - let mut resolved_references: Vec<(SymbolId, Vec)> = vec![]; + let parent_scope_id = + self.scope.get_parent_id(self.current_scope_id).unwrap_or(self.current_scope_id); for (name, reference_ids) in all_references { if let Some(symbol_id) = self.scope.get_binding(self.current_scope_id, &name) { - resolved_references.push((symbol_id, reference_ids)); + for reference_id in &reference_ids { + self.symbols.references[*reference_id].set_symbol_id(symbol_id); + } + self.symbols.resolved_references[symbol_id].extend(reference_ids); } else { - unresolved_references.insert(name, reference_ids); - } - } - - let scope_id = - self.scope.get_parent_id(self.current_scope_id).unwrap_or(self.current_scope_id); - - for (name, reference_ids) in unresolved_references { - self.scope.extend_unresolved_reference(scope_id, name, reference_ids); - } - - for (symbol_id, reference_ids) in resolved_references { - for reference_id in reference_ids { - self.symbols.references[reference_id].set_symbol_id(symbol_id); - self.symbols.resolved_references[symbol_id].push(reference_id); + self.scope.extend_unresolved_reference(parent_scope_id, name, reference_ids); } } }