Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(transformer): pre-allocate more stack space #6095

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions crates/oxc_transformer/src/helpers/stack/capacity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ pub trait StackCapacity<T> {

/// Default capacity of stack.
///
/// Same defaults as [`std::vec::Vec`] uses.
/// Same defaults as [`std::vec::Vec`] uses, except 16 bytes for types with size 1 (instead of 8).
/// Allocators will usually allocate minimum 16 bytes anyway.
const DEFAULT_CAPACITY: usize = {
// It's impossible for this to exceed `MAX_CAPACITY` because `size_of::<T>() >= align_of::<T>()`
match size_of::<T>() {
1 => 8,
1 => 16,
size if size <= 1024 => 4,
_ => 1,
}
Expand All @@ -65,8 +66,8 @@ mod tests {
impl StackCapacity<bool> for TestStack {}
assert_eq!(TestStack::MAX_CAPACITY, ISIZE_MAX);
assert_eq!(TestStack::MAX_CAPACITY_BYTES, ISIZE_MAX);
assert_eq!(TestStack::DEFAULT_CAPACITY, 8);
assert_eq!(TestStack::DEFAULT_CAPACITY_BYTES, 8);
assert_eq!(TestStack::DEFAULT_CAPACITY, 16);
assert_eq!(TestStack::DEFAULT_CAPACITY_BYTES, 16);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/helpers/stack/non_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ mod tests {
#[test]
fn new() {
let stack = NonEmptyStack::new(true);
assert_len_cap_last!(stack, 1, 8, &true);
assert_eq!(stack.capacity_bytes(), 8);
assert_len_cap_last!(stack, 1, 16, &true);
assert_eq!(stack.capacity_bytes(), 16);

let stack = NonEmptyStack::new(10u64);
assert_len_cap_last!(stack, 1, 4, &10);
Expand Down