Skip to content

Commit

Permalink
refactor(semantic): Stats store counts as u32 (#5754)
Browse files Browse the repository at this point in the history
These counts can never exceed `u32`, so reduce size of these fields.
  • Loading branch information
overlookmotel committed Sep 13, 2024
1 parent 667170c commit b4b460f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
15 changes: 8 additions & 7 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,22 @@ impl<'a> SemanticBuilder<'a> {
// TODO: It would be even more efficient to calculate counts in parser to avoid
// this extra AST traversal.
let stats = Stats::count(program);
self.nodes.reserve(stats.nodes);
self.scope.reserve(stats.scopes);
self.symbols.reserve(stats.symbols, stats.references);
self.nodes.reserve(stats.nodes as usize);
self.scope.reserve(stats.scopes as usize);
self.symbols.reserve(stats.symbols as usize, stats.references as usize);

// Visit AST to generate scopes tree etc
self.visit_program(program);

// Check that estimated counts accurately
#[cfg(debug_assertions)]
{
#[allow(clippy::cast_possible_truncation)]
let actual_stats = Stats {
nodes: self.nodes.len(),
scopes: self.scope.len(),
symbols: self.symbols.len(),
references: self.symbols.references.len(),
nodes: self.nodes.len() as u32,
scopes: self.scope.len() as u32,
symbols: self.symbols.len() as u32,
references: self.symbols.references.len() as u32,
};
Stats::assert_accurate(&actual_stats, &stats);
}
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_semantic/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use oxc_syntax::scope::{ScopeFlags, ScopeId};

#[derive(Default, Debug)]
pub(crate) struct Stats {
pub nodes: usize,
pub scopes: usize,
pub symbols: usize,
pub references: usize,
pub nodes: u32,
pub scopes: u32,
pub symbols: u32,
pub references: u32,
}

impl Stats {
Expand Down

0 comments on commit b4b460f

Please sign in to comment.