From ae895d8c0e3fe31fe809ccc27bc4d4831b9997d5 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Wed, 22 Jan 2025 16:04:35 +0000 Subject: [PATCH] refactor(minifier): use `NonEmptyStack` for function stack (#8661) --- Cargo.lock | 1 + crates/oxc_minifier/Cargo.toml | 1 + crates/oxc_minifier/src/peephole/mod.rs | 40 ++++++++++--------------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4b40a3703bcfc..eac4924798e85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1850,6 +1850,7 @@ dependencies = [ "oxc_allocator", "oxc_ast", "oxc_codegen", + "oxc_data_structures", "oxc_ecmascript", "oxc_mangler", "oxc_parser", diff --git a/crates/oxc_minifier/Cargo.toml b/crates/oxc_minifier/Cargo.toml index 5212e6e03a5d8..68d12eab1198b 100644 --- a/crates/oxc_minifier/Cargo.toml +++ b/crates/oxc_minifier/Cargo.toml @@ -24,6 +24,7 @@ doctest = false oxc_allocator = { workspace = true } oxc_ast = { workspace = true } oxc_codegen = { workspace = true } +oxc_data_structures = { workspace = true } oxc_ecmascript = { workspace = true, features = ["constant_evaluation"] } oxc_mangler = { workspace = true } oxc_parser = { workspace = true } diff --git a/crates/oxc_minifier/src/peephole/mod.rs b/crates/oxc_minifier/src/peephole/mod.rs index 72223943c40ca..7d80a35984ed7 100644 --- a/crates/oxc_minifier/src/peephole/mod.rs +++ b/crates/oxc_minifier/src/peephole/mod.rs @@ -11,6 +11,7 @@ mod substitute_alternate_syntax; use oxc_allocator::Vec; use oxc_ast::ast::*; +use oxc_data_structures::stack::NonEmptyStack; use oxc_syntax::{es_target::ESTarget, scope::ScopeId}; use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx}; @@ -28,8 +29,8 @@ pub struct PeepholeOptimizations { prev_functions_changed: FxHashSet, functions_changed: FxHashSet, /// Track the current function as a stack. - current_function_stack: - std::vec::Vec<(ScopeId, /* prev changed */ bool, /* current changed */ bool)>, + current_function: + NonEmptyStack<(ScopeId, /* prev changed */ bool, /* current changed */ bool)>, } impl<'a> PeepholeOptimizations { @@ -39,7 +40,7 @@ impl<'a> PeepholeOptimizations { iteration: 0, prev_functions_changed: FxHashSet::default(), functions_changed: FxHashSet::default(), - current_function_stack: std::vec::Vec::new(), + current_function: NonEmptyStack::new((ScopeId::new(0), true, false)), } } @@ -64,43 +65,32 @@ impl<'a> PeepholeOptimizations { } fn mark_current_function_as_changed(&mut self) { - if let Some((_scope_id, _prev_changed, current_changed)) = - self.current_function_stack.last_mut() - { - *current_changed = true; - } + let (_scope_id, _prev_changed, current_changed) = self.current_function.last_mut(); + *current_changed = true; } pub fn is_current_function_changed(&self) -> bool { - if let Some((_, _, current_changed)) = self.current_function_stack.last() { - return *current_changed; - } - false + let (_, _, current_changed) = self.current_function.last(); + *current_changed } fn is_prev_function_changed(&self) -> bool { - if self.iteration == 0 { - return true; - } - if let Some((_, prev_changed, _)) = self.current_function_stack.last() { - return *prev_changed; - } - false + let (_, prev_changed, _) = self.current_function.last(); + *prev_changed } fn enter_program_or_function(&mut self, scope_id: ScopeId) { - self.current_function_stack.push(( + self.current_function.push(( scope_id, - self.prev_functions_changed.contains(&scope_id), + self.iteration == 0 || self.prev_functions_changed.contains(&scope_id), false, )); } fn exit_program_or_function(&mut self) { - if let Some((scope_id, _, changed)) = self.current_function_stack.pop() { - if changed { - self.functions_changed.insert(scope_id); - } + let (scope_id, _, changed) = self.current_function.pop(); + if changed { + self.functions_changed.insert(scope_id); } } }