Skip to content

Commit ae895d8

Browse files
committed
refactor(minifier): use NonEmptyStack for function stack (#8661)
1 parent a730f99 commit ae895d8

3 files changed

Lines changed: 17 additions & 25 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_minifier/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ doctest = false
2424
oxc_allocator = { workspace = true }
2525
oxc_ast = { workspace = true }
2626
oxc_codegen = { workspace = true }
27+
oxc_data_structures = { workspace = true }
2728
oxc_ecmascript = { workspace = true, features = ["constant_evaluation"] }
2829
oxc_mangler = { workspace = true }
2930
oxc_parser = { workspace = true }

crates/oxc_minifier/src/peephole/mod.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod substitute_alternate_syntax;
1111

1212
use oxc_allocator::Vec;
1313
use oxc_ast::ast::*;
14+
use oxc_data_structures::stack::NonEmptyStack;
1415
use oxc_syntax::{es_target::ESTarget, scope::ScopeId};
1516
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};
1617

@@ -28,8 +29,8 @@ pub struct PeepholeOptimizations {
2829
prev_functions_changed: FxHashSet<ScopeId>,
2930
functions_changed: FxHashSet<ScopeId>,
3031
/// Track the current function as a stack.
31-
current_function_stack:
32-
std::vec::Vec<(ScopeId, /* prev changed */ bool, /* current changed */ bool)>,
32+
current_function:
33+
NonEmptyStack<(ScopeId, /* prev changed */ bool, /* current changed */ bool)>,
3334
}
3435

3536
impl<'a> PeepholeOptimizations {
@@ -39,7 +40,7 @@ impl<'a> PeepholeOptimizations {
3940
iteration: 0,
4041
prev_functions_changed: FxHashSet::default(),
4142
functions_changed: FxHashSet::default(),
42-
current_function_stack: std::vec::Vec::new(),
43+
current_function: NonEmptyStack::new((ScopeId::new(0), true, false)),
4344
}
4445
}
4546

@@ -64,43 +65,32 @@ impl<'a> PeepholeOptimizations {
6465
}
6566

6667
fn mark_current_function_as_changed(&mut self) {
67-
if let Some((_scope_id, _prev_changed, current_changed)) =
68-
self.current_function_stack.last_mut()
69-
{
70-
*current_changed = true;
71-
}
68+
let (_scope_id, _prev_changed, current_changed) = self.current_function.last_mut();
69+
*current_changed = true;
7270
}
7371

7472
pub fn is_current_function_changed(&self) -> bool {
75-
if let Some((_, _, current_changed)) = self.current_function_stack.last() {
76-
return *current_changed;
77-
}
78-
false
73+
let (_, _, current_changed) = self.current_function.last();
74+
*current_changed
7975
}
8076

8177
fn is_prev_function_changed(&self) -> bool {
82-
if self.iteration == 0 {
83-
return true;
84-
}
85-
if let Some((_, prev_changed, _)) = self.current_function_stack.last() {
86-
return *prev_changed;
87-
}
88-
false
78+
let (_, prev_changed, _) = self.current_function.last();
79+
*prev_changed
8980
}
9081

9182
fn enter_program_or_function(&mut self, scope_id: ScopeId) {
92-
self.current_function_stack.push((
83+
self.current_function.push((
9384
scope_id,
94-
self.prev_functions_changed.contains(&scope_id),
85+
self.iteration == 0 || self.prev_functions_changed.contains(&scope_id),
9586
false,
9687
));
9788
}
9889

9990
fn exit_program_or_function(&mut self) {
100-
if let Some((scope_id, _, changed)) = self.current_function_stack.pop() {
101-
if changed {
102-
self.functions_changed.insert(scope_id);
103-
}
91+
let (scope_id, _, changed) = self.current_function.pop();
92+
if changed {
93+
self.functions_changed.insert(scope_id);
10494
}
10595
}
10696
}

0 commit comments

Comments
 (0)