From 339fcfc8bec25745c89dd9f8fa232b4e190ed76a Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Wed, 11 Sep 2024 20:41:48 +0000 Subject: [PATCH] refactor(semantic): rename `Counts` in transform checker (#5709) Pure refactor. Just renaming. --- crates/oxc_semantic/src/builder.rs | 24 ++++++++++++------------ crates/oxc_semantic/src/counter.rs | 29 ++++++++++++++--------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 49966b11ee8a7..24e3444ca5fda 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -21,7 +21,7 @@ use crate::{ binder::Binder, checker, class::ClassTableBuilder, - counter::Counter, + counter::Counts, diagnostics::redeclaration, jsdoc::JSDocBuilder, label::UnusedLabels, @@ -231,26 +231,26 @@ impl<'a> SemanticBuilder<'a> { // Avoiding this growth produces up to 30% perf boost on our benchmarks. // TODO: It would be even more efficient to calculate counts in parser to avoid // this extra AST traversal. - let mut counter = Counter::default(); - counter.visit_program(program); - self.nodes.reserve(counter.nodes_count); - self.scope.reserve(counter.scopes_count); - self.symbols.reserve(counter.symbols_count, counter.references_count); + let mut counts = Counts::default(); + counts.visit_program(program); + self.nodes.reserve(counts.nodes); + self.scope.reserve(counts.scopes); + self.symbols.reserve(counts.symbols, counts.references); // Visit AST to generate scopes tree etc self.visit_program(program); - // Check that `Counter` got accurate counts - debug_assert_eq!(self.nodes.len(), counter.nodes_count); - debug_assert_eq!(self.scope.len(), counter.scopes_count); - debug_assert_eq!(self.symbols.references.len(), counter.references_count); - // `Counter` may overestimate number of symbols, because multiple `BindingIdentifier`s + // Check that estimated counts accurately + debug_assert_eq!(self.nodes.len(), counts.nodes); + debug_assert_eq!(self.scope.len(), counts.scopes); + debug_assert_eq!(self.symbols.references.len(), counts.references); + // `Counts` may overestimate number of symbols, because multiple `BindingIdentifier`s // can result in only a single symbol. // e.g. `var x; var x;` = 2 x `BindingIdentifier` but 1 x symbol. // This is not a big problem - allocating a `Vec` with excess capacity is cheap. // It's allocating with *not enough* capacity which is costly, as then the `Vec` // will grow and reallocate. - debug_assert!(self.symbols.len() <= counter.symbols_count); + debug_assert!(self.symbols.len() <= counts.symbols); // Checking syntax error on module record requires scope information from the previous AST pass if self.check_syntax_error { diff --git a/crates/oxc_semantic/src/counter.rs b/crates/oxc_semantic/src/counter.rs index 2a06e2bee6808..345633464ebe6 100644 --- a/crates/oxc_semantic/src/counter.rs +++ b/crates/oxc_semantic/src/counter.rs @@ -11,49 +11,48 @@ use oxc_ast::{ }; use oxc_syntax::scope::{ScopeFlags, ScopeId}; -#[allow(clippy::struct_field_names)] #[derive(Default, Debug)] -pub(crate) struct Counter { - pub nodes_count: usize, - pub scopes_count: usize, - pub symbols_count: usize, - pub references_count: usize, +pub(crate) struct Counts { + pub nodes: usize, + pub scopes: usize, + pub symbols: usize, + pub references: usize, } -impl<'a> Visit<'a> for Counter { +impl<'a> Visit<'a> for Counts { #[inline] fn enter_node(&mut self, _: AstKind<'a>) { - self.nodes_count += 1; + self.nodes += 1; } #[inline] fn enter_scope(&mut self, _: ScopeFlags, _: &Cell>) { - self.scopes_count += 1; + self.scopes += 1; } #[inline] fn visit_binding_identifier(&mut self, _: &BindingIdentifier<'a>) { - self.nodes_count += 1; - self.symbols_count += 1; + self.nodes += 1; + self.symbols += 1; } #[inline] fn visit_identifier_reference(&mut self, _: &IdentifierReference<'a>) { - self.nodes_count += 1; - self.references_count += 1; + self.nodes += 1; + self.references += 1; } #[inline] fn visit_ts_enum_member_name(&mut self, it: &TSEnumMemberName<'a>) { if !it.is_expression() { - self.symbols_count += 1; + self.symbols += 1; } walk_ts_enum_member_name(self, it); } #[inline] fn visit_ts_module_declaration_name(&mut self, it: &TSModuleDeclarationName<'a>) { - self.symbols_count += 1; + self.symbols += 1; walk_ts_module_declaration_name(self, it); } }