Skip to content

Commit

Permalink
refactor(semantic): rename Counts to Stats (#5753)
Browse files Browse the repository at this point in the history
Pure refactor. Just rename the type.
  • Loading branch information
overlookmotel committed Sep 13, 2024
1 parent 042afa9 commit 667170c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ use crate::{
binder::Binder,
checker,
class::ClassTableBuilder,
counter::Counts,
diagnostics::redeclaration,
jsdoc::JSDocBuilder,
label::UnusedLabels,
module_record::ModuleRecordBuilder,
node::{AstNodes, NodeFlags, NodeId},
reference::{Reference, ReferenceFlags, ReferenceId},
scope::{Bindings, ScopeFlags, ScopeId, ScopeTree},
stats::Stats,
symbol::{SymbolFlags, SymbolId, SymbolTable},
unresolved_stack::UnresolvedReferencesStack,
JSDocFinder, Semantic,
Expand Down Expand Up @@ -232,24 +232,24 @@ 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);

// 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
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ mod binder;
mod builder;
mod checker;
mod class;
mod counter;
mod diagnostics;
mod jsdoc;
mod label;
mod module_record;
mod node;
mod reference;
mod scope;
mod stats;
mod symbol;
mod unresolved_stack;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ 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))]
pub fn assert_accurate(actual: &Self, estimated: &Self) {
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.
Expand All @@ -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;
Expand Down

0 comments on commit 667170c

Please sign in to comment.