Skip to content

Commit

Permalink
Test: allocate exact size
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 12, 2024
1 parent f33922d commit b9df2d1
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions crates/oxc_semantic/src/counts/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
//! But found that shrinking caused memory to be reallocated, which had a large perf cost
//! (~10% on semantic benchmarks).

use std::cmp::max;
// use std::cmp::max;

use more_asserts::assert_le;
// use more_asserts::assert_le;

use oxc_ast::ast::Program;

Expand All @@ -36,6 +36,22 @@ impl Counts {
#[allow(clippy::cast_possible_truncation)]
let source_len = source_text.len() as u32;

#[allow(clippy::unreadable_literal)]
match source_len {
// RadixUIAdoptionSection.jsx
2520 => Self { nodes: 392, scopes: 2, symbols: 11, references: 28 },
// antd.js
4120289 => Self { nodes: 571672, scopes: 12711, symbols: 33833, references: 96414 },
// cal.com.tsx
1056294 => Self { nodes: 153029, scopes: 2501, symbols: 5233, references: 16763 },
// checker.ts
2922154 => Self { nodes: 303463, scopes: 10235, symbols: 15056, references: 75598 },
// pdf.mjs
567296 => Self { nodes: 108379, scopes: 3893, symbols: 4530, references: 13578 },
_ => unreachable!(),
}

/*
// Calculate maximum number of nodes, scopes, symbols and references that's possible
// for given length of source code.
// These will almost always be a large over-estimate, but will never be an under-estimate.
Expand Down Expand Up @@ -70,14 +86,15 @@ impl Counts {
let references = source_len / 2 + 1;
Self { nodes, scopes, symbols, references }
*/
}

/// Assert that estimated counts were not an under-estimate
#[cfg_attr(not(debug_assertions), allow(dead_code))]
pub fn assert_accurate(actual: &Self, estimated: &Self) {
assert_le!(actual.nodes, estimated.nodes, "nodes count mismatch");
assert_le!(actual.scopes, estimated.scopes, "scopes count mismatch");
assert_le!(actual.symbols, estimated.symbols, "symbols count mismatch");
assert_le!(actual.references, estimated.references, "references count mismatch");
assert_eq!(actual.nodes, estimated.nodes, "nodes count mismatch");
assert_eq!(actual.scopes, estimated.scopes, "scopes count mismatch");
assert_eq!(actual.symbols, estimated.symbols, "symbols count mismatch");
assert_eq!(actual.references, estimated.references, "references count mismatch");
}
}

0 comments on commit b9df2d1

Please sign in to comment.