Skip to content

Commit

Permalink
refactor(transformer): add SparseStack::with_capacity method
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 26, 2024
1 parent c98b3bb commit e1a6472
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
2 changes: 0 additions & 2 deletions crates/oxc_transformer/src/helpers/stack/non_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ impl<T> NonEmptyStack<T> {
/// * `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`");
Expand Down Expand Up @@ -352,7 +351,6 @@ impl<T> NonEmptyStack<T> {

/// 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`).
Expand Down
56 changes: 56 additions & 0 deletions crates/oxc_transformer/src/helpers/stack/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ pub struct SparseStack<T> {
}

impl<T> SparseStack<T> {
/// Maximum capacity for entries (either `Some` or `None`).
///
/// Effectively unlimited on 64-bit systems.
#[expect(dead_code)]
pub const MAX_TOTAL_CAPACITY: usize = NonEmptyStack::<bool>::MAX_CAPACITY;

/// Maximum capacity for filled entries (`Some`).
///
/// Unless `size_of::<T>() == 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::<T>::MAX_CAPACITY;

/// Create new `SparseStack`.
///
/// # Panics
Expand All @@ -42,6 +58,26 @@ impl<T> SparseStack<T> {
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<T>) {
Expand Down Expand Up @@ -150,4 +186,24 @@ impl<T> SparseStack<T> {
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()
}
}
2 changes: 0 additions & 2 deletions crates/oxc_transformer/src/helpers/stack/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ impl<T> Stack<T> {
/// * `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()
Expand Down Expand Up @@ -374,7 +373,6 @@ impl<T> Stack<T> {

/// 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`).
Expand Down

0 comments on commit e1a6472

Please sign in to comment.