From c886d8cfe69f26e952d6cd6f31001c0e25602078 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Fri, 13 Sep 2024 09:18:24 +0100 Subject: [PATCH] refactor(semantic): rename `Counts` to `Stats` --- crates/oxc_semantic/src/builder.rs | 14 +++++++------- crates/oxc_semantic/src/lib.rs | 2 +- crates/oxc_semantic/src/{counter.rs => stats.rs} | 14 +++++++------- 3 files changed, 15 insertions(+), 15 deletions(-) rename crates/oxc_semantic/src/{counter.rs => stats.rs} (90%) diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index bb67c4844ced5..b4f7d68150fa2 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -22,7 +22,6 @@ use crate::{ binder::Binder, checker, class::ClassTableBuilder, - counter::Counts, diagnostics::redeclaration, jsdoc::JSDocBuilder, label::UnusedLabels, @@ -30,6 +29,7 @@ use crate::{ node::{AstNodes, NodeFlags, NodeId}, reference::{Reference, ReferenceFlags, ReferenceId}, scope::{Bindings, ScopeFlags, ScopeId, ScopeTree}, + stats::Stats, symbol::{SymbolFlags, SymbolId, SymbolTable}, unresolved_stack::UnresolvedReferencesStack, JSDocFinder, Semantic, @@ -232,10 +232,10 @@ 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 counts = Counts::count(program); - self.nodes.reserve(counts.nodes); - self.scope.reserve(counts.scopes); - self.symbols.reserve(counts.symbols, counts.references); + let stats = Stats::count(program); + self.nodes.reserve(stats.nodes); + self.scope.reserve(stats.scopes); + self.symbols.reserve(stats.symbols, stats.references); // Visit AST to generate scopes tree etc self.visit_program(program); @@ -243,13 +243,13 @@ impl<'a> SemanticBuilder<'a> { // Check that estimated counts accurately #[cfg(debug_assertions)] { - let actual_counts = Counts { + let actual_stats = Stats { nodes: self.nodes.len(), scopes: self.scope.len(), symbols: self.symbols.len(), references: self.symbols.references.len(), }; - Counts::assert_accurate(&actual_counts, &counts); + Stats::assert_accurate(&actual_stats, &stats); } // Checking syntax error on module record requires scope information from the previous AST pass diff --git a/crates/oxc_semantic/src/lib.rs b/crates/oxc_semantic/src/lib.rs index c1a1f28415a02..72ed0f9b4d844 100644 --- a/crates/oxc_semantic/src/lib.rs +++ b/crates/oxc_semantic/src/lib.rs @@ -23,7 +23,6 @@ mod binder; mod builder; mod checker; mod class; -mod counter; mod diagnostics; mod jsdoc; mod label; @@ -31,6 +30,7 @@ mod module_record; mod node; mod reference; mod scope; +mod stats; mod symbol; mod unresolved_stack; diff --git a/crates/oxc_semantic/src/counter.rs b/crates/oxc_semantic/src/stats.rs similarity index 90% rename from crates/oxc_semantic/src/counter.rs rename to crates/oxc_semantic/src/stats.rs index 947292b8ffdda..86226488d6807 100644 --- a/crates/oxc_semantic/src/counter.rs +++ b/crates/oxc_semantic/src/stats.rs @@ -14,18 +14,18 @@ use oxc_ast::{ use oxc_syntax::scope::{ScopeFlags, ScopeId}; #[derive(Default, Debug)] -pub(crate) struct Counts { +pub(crate) struct Stats { pub nodes: usize, pub scopes: usize, pub symbols: usize, pub references: usize, } -impl Counts { +impl Stats { pub fn count(program: &Program) -> Self { - let mut counts = Counts::default(); - counts.visit_program(program); - counts + let mut stats = Stats::default(); + stats.visit_program(program); + stats } #[cfg_attr(not(debug_assertions), expect(dead_code))] @@ -33,7 +33,7 @@ impl Counts { assert_eq!(actual.nodes, estimated.nodes, "nodes count mismatch"); assert_eq!(actual.scopes, estimated.scopes, "scopes count mismatch"); assert_eq!(actual.references, estimated.references, "references count mismatch"); - // `Counts` may overestimate number of symbols, because multiple `BindingIdentifier`s + // `Stats` 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. @@ -48,7 +48,7 @@ impl Counts { } } -impl<'a> Visit<'a> for Counts { +impl<'a> Visit<'a> for Stats { #[inline] fn enter_node(&mut self, _: AstKind<'a>) { self.nodes += 1;