From e60ce506ea293f23dfdfccc2e234f4f0fc1b2429 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 27 Sep 2024 04:28:55 +0000 Subject: [PATCH] refactor(transformer): add `SparseStack::with_capacity` method (#6094) Add `SparseStack::with_capacity` method. --- .../src/helpers/stack/non_empty.rs | 2 - .../src/helpers/stack/sparse.rs | 56 +++++++++++++++++++ .../src/helpers/stack/standard.rs | 2 - 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/crates/oxc_transformer/src/helpers/stack/non_empty.rs b/crates/oxc_transformer/src/helpers/stack/non_empty.rs index 9287b7f184f75..8246dbd305694 100644 --- a/crates/oxc_transformer/src/helpers/stack/non_empty.rs +++ b/crates/oxc_transformer/src/helpers/stack/non_empty.rs @@ -93,7 +93,6 @@ impl NonEmptyStack { /// * `capacity` must not be 0. /// * `capacity` must not exceed [`Self::MAX_CAPACITY`]. #[inline] - #[cfg_attr(not(test), expect(dead_code))] pub fn with_capacity(capacity: usize, initial_value: T) -> Self { assert!(capacity > 0, "`capacity` cannot be zero"); assert!(capacity <= Self::MAX_CAPACITY, "`capacity` must not exceed `Self::MAX_CAPACITY`"); @@ -352,7 +351,6 @@ impl NonEmptyStack { /// Get capacity. #[inline] - #[cfg_attr(not(test), expect(dead_code))] pub fn capacity(&self) -> usize { // SAFETY: `self.start` and `self.end` are both derived from same pointer // (in `new_with_capacity_bytes_unchecked` and `push_slow`). diff --git a/crates/oxc_transformer/src/helpers/stack/sparse.rs b/crates/oxc_transformer/src/helpers/stack/sparse.rs index 469dcfde73384..943b0e0a093bf 100644 --- a/crates/oxc_transformer/src/helpers/stack/sparse.rs +++ b/crates/oxc_transformer/src/helpers/stack/sparse.rs @@ -31,6 +31,22 @@ pub struct SparseStack { } impl SparseStack { + /// Maximum capacity for entries (either `Some` or `None`). + /// + /// Effectively unlimited on 64-bit systems. + #[expect(dead_code)] + pub const MAX_TOTAL_CAPACITY: usize = NonEmptyStack::::MAX_CAPACITY; + + /// Maximum capacity for filled entries (`Some`). + /// + /// Unless `size_of::() == 1`, `MAX_FILLED_CAPACITY` is lower than [`MAX_TOTAL_CAPACITY`]. + /// + /// Both are effectively unlimited on 64-bit systems. + /// + /// [`MAX_TOTAL_CAPACITY`]: Self::MAX_TOTAL_CAPACITY + #[expect(dead_code)] + pub const MAX_FILLED_CAPACITY: usize = Stack::::MAX_CAPACITY; + /// Create new `SparseStack`. /// /// # Panics @@ -42,6 +58,26 @@ impl SparseStack { Self { has_values: NonEmptyStack::new(false), values: Stack::new() } } + /// Create new `SparseStack` with pre-allocated capacity. + /// + /// * `total_capacity` is capacity for any entries (either `Some` or `None`). Cannot be 0. + /// * `filled_capacity` is capacity for full entries (`Some`). + /// + /// # Panics + /// Panics if any of these requirements are not satisfied: + /// * `T` must not be a zero-sized type. + /// * `total_capacity` must not be 0. + /// * `total_capacity` must not exceed `Self::MAX_TOTAL_CAPACITY`. + /// * `filled_capacity` must not exceed `Self::MAX_FILLED_CAPACITY`. + #[inline] + #[expect(dead_code)] + pub fn with_capacity(total_capacity: usize, filled_capacity: usize) -> Self { + Self { + has_values: NonEmptyStack::with_capacity(total_capacity, false), + values: Stack::with_capacity(filled_capacity), + } + } + /// Push an entry to the stack. #[inline] pub fn push(&mut self, value: Option) { @@ -150,4 +186,24 @@ impl SparseStack { pub fn len(&self) -> usize { self.has_values.len() } + + /// Get capacity of stack for any entries (either `Some` or `None`). + /// + /// Capacity is always at least 1. Stack is never empty. + #[inline] + #[expect(dead_code)] + pub fn total_capacity(&self) -> usize { + self.has_values.capacity() + } + + /// Get capacity of stack for filled entries (`Some`). + /// + /// The capacity can be zero (unlike [`total_capacity`]). + /// + /// [`total_capacity`]: Self::total_capacity + #[inline] + #[expect(dead_code)] + pub fn filled_capacity(&self) -> usize { + self.values.capacity() + } } diff --git a/crates/oxc_transformer/src/helpers/stack/standard.rs b/crates/oxc_transformer/src/helpers/stack/standard.rs index f22f28e811e07..50731d2e8afd5 100644 --- a/crates/oxc_transformer/src/helpers/stack/standard.rs +++ b/crates/oxc_transformer/src/helpers/stack/standard.rs @@ -73,7 +73,6 @@ impl Stack { /// * `T` must not be a zero-sized type. /// * `capacity` must not exceed [`Self::MAX_CAPACITY`]. #[inline] - #[cfg_attr(not(test), expect(dead_code))] pub fn with_capacity(capacity: usize) -> Self { if capacity == 0 { Self::new() @@ -374,7 +373,6 @@ impl Stack { /// Get capacity. #[inline] - #[cfg_attr(not(test), expect(dead_code))] pub fn capacity(&self) -> usize { // SAFETY: `self.start` and `self.end` are both derived from same pointer // (in `new`, `new_with_capacity_bytes_unchecked` and `push_slow`).