Skip to content
Closed
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
3 changes: 1 addition & 2 deletions crates/oxc_minifier/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ impl<'a> oxc_ecmascript::GlobalContext<'a> for Ctx<'a, '_> {
.symbol_id()
.and_then(|symbol_id| self.state.symbol_values.get_symbol_value(symbol_id))
.filter(|sv| sv.write_references_count == 0)
.and_then(|sv| sv.initialized_constant.as_ref())
.cloned()
.and_then(|sv| sv.value.to_constant_value())
}
}

Expand Down
13 changes: 8 additions & 5 deletions crates/oxc_minifier/src/peephole/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ use oxc_span::GetSpan;
use oxc_traverse::Ancestor;
use rustc_hash::FxHashSet;

use crate::{ctx::Ctx, symbol_value::SymbolValue};
use crate::{
ctx::Ctx,
symbol_value::{SymbolInformation, SymbolValue},
};

use super::PeepholeOptimizations;

impl<'a> PeepholeOptimizations {
pub fn init_symbol_values(program: &Program<'a>, ctx: &mut Ctx<'a, '_>) {
pub fn init_symbol_information_map(program: &Program<'a>, ctx: &mut Ctx<'a, '_>) {
let exported_values = if ctx.source_type().is_script() {
FxHashSet::default()
} else {
Expand Down Expand Up @@ -81,8 +84,8 @@ impl<'a> PeepholeOptimizations {
}
}
let scope_id = ctx.scoping().symbol_scope_id(symbol_id);
let value = SymbolValue {
initialized_constant: None,
let value = SymbolInformation {
value: SymbolValue::default(),
exported: exported_values.contains(&symbol_id),
read_references_count,
write_references_count,
Expand Down Expand Up @@ -121,7 +124,7 @@ impl<'a> PeepholeOptimizations {
if symbol_value.write_references_count > 0 {
return;
}
let Some(cv) = &symbol_value.initialized_constant else { return };
let SymbolValue::Primitive(cv) = &symbol_value.value else { return };
if symbol_value.read_references_count == 1
|| match cv {
ConstantValue::Number(n) => n.fract() == 0.0 && *n >= -99.0 && *n <= 999.0,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_minifier/src/peephole/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<'a> Traverse<'a, MinifierState<'a>> for PeepholeOptimizations {
fn enter_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
let ctx = &mut Ctx::new(ctx);
ctx.state.symbol_values.clear();
Self::init_symbol_values(program, ctx);
Self::init_symbol_information_map(program, ctx);
ctx.state.changed = false;
}

Expand Down Expand Up @@ -423,7 +423,7 @@ impl<'a> Traverse<'a, MinifierState<'a>> for DeadCodeElimination {
fn enter_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
let ctx = &mut Ctx::new(ctx);
ctx.state.symbol_values.clear();
PeepholeOptimizations::init_symbol_values(program, ctx);
PeepholeOptimizations::init_symbol_information_map(program, ctx);
ctx.state.changed = false;
}

Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_minifier/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_hash::FxHashMap;
use oxc_span::SourceType;
use oxc_syntax::symbol::SymbolId;

use crate::{CompressOptions, symbol_value::SymbolValues};
use crate::{CompressOptions, symbol_value::SymbolInformationMap};

pub struct MinifierState<'a> {
pub source_type: SourceType,
Expand All @@ -14,7 +14,7 @@ pub struct MinifierState<'a> {
/// The return value of function declarations that are pure
pub pure_functions: FxHashMap<SymbolId, Option<ConstantValue<'a>>>,

pub symbol_values: SymbolValues<'a>,
pub symbol_values: SymbolInformationMap<'a>,

pub changed: bool,
}
Expand All @@ -25,7 +25,7 @@ impl MinifierState<'_> {
source_type,
options,
pure_functions: FxHashMap::default(),
symbol_values: SymbolValues::default(),
symbol_values: SymbolInformationMap::default(),
changed: false,
}
}
Expand Down
36 changes: 25 additions & 11 deletions crates/oxc_minifier/src/symbol_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ use rustc_hash::FxHashMap;
use oxc_ecmascript::constant_evaluation::ConstantValue;
use oxc_syntax::{scope::ScopeId, symbol::SymbolId};

#[derive(Debug, Default)]
pub enum SymbolValue<'a> {
/// Initialized primitive constant value evaluated from expressions.
Primitive(ConstantValue<'a>),
#[default]
Unknown,
}

impl<'a> SymbolValue<'a> {
pub fn to_constant_value(&self) -> Option<ConstantValue<'a>> {
if let SymbolValue::Primitive(cv) = self { Some(cv.clone()) } else { None }
}
}

#[derive(Debug)]
pub struct SymbolValue<'a> {
/// Initialized constant value evaluated from expressions.
/// `None` when the value is not a constant evaluated value.
pub initialized_constant: Option<ConstantValue<'a>>,
pub struct SymbolInformation<'a> {
pub value: SymbolValue<'a>,

/// Symbol is exported.
pub exported: bool,
Expand All @@ -20,16 +32,16 @@ pub struct SymbolValue<'a> {
}

#[derive(Debug, Default)]
pub struct SymbolValues<'a> {
values: FxHashMap<SymbolId, SymbolValue<'a>>,
pub struct SymbolInformationMap<'a> {
values: FxHashMap<SymbolId, SymbolInformation<'a>>,
}

impl<'a> SymbolValues<'a> {
impl<'a> SymbolInformationMap<'a> {
pub fn clear(&mut self) {
self.values.clear();
}

pub fn init_value(&mut self, symbol_id: SymbolId, symbol_value: SymbolValue<'a>) {
pub fn init_value(&mut self, symbol_id: SymbolId, symbol_value: SymbolInformation<'a>) {
self.values.insert(symbol_id, symbol_value);
}

Expand All @@ -38,11 +50,13 @@ impl<'a> SymbolValues<'a> {
symbol_id: SymbolId,
symbol_value: Option<ConstantValue<'a>>,
) {
let value = self.values.get_mut(&symbol_id).expect("symbol value must exist");
value.initialized_constant = symbol_value;
let info = self.values.get_mut(&symbol_id).expect("symbol value must exist");
if let Some(constant) = symbol_value {
info.value = SymbolValue::Primitive(constant);
}
}

pub fn get_symbol_value(&self, symbol_id: SymbolId) -> Option<&SymbolValue<'a>> {
pub fn get_symbol_value(&self, symbol_id: SymbolId) -> Option<&SymbolInformation<'a>> {
self.values.get(&symbol_id)
}
}
Loading