-
-
Notifications
You must be signed in to change notification settings - Fork 726
feat(minifier): inline const variables that are only used once
#12488
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ mod keep_var; | |
| mod options; | ||
| mod peephole; | ||
| mod state; | ||
| mod symbol_value; | ||
|
|
||
| #[cfg(test)] | ||
| mod tester; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| use oxc_ast::ast::*; | ||
| use oxc_ecmascript::constant_evaluation::ConstantEvaluation; | ||
| use oxc_span::GetSpan; | ||
|
|
||
| use crate::ctx::Ctx; | ||
|
|
||
| use super::PeepholeOptimizations; | ||
|
|
||
| impl<'a> PeepholeOptimizations { | ||
| pub fn init_symbol_value(&self, decl: &VariableDeclarator<'a>, ctx: &mut Ctx<'a, '_>) { | ||
| let BindingPatternKind::BindingIdentifier(ident) = &decl.id.kind else { return }; | ||
| let Some(symbol_id) = ident.symbol_id.get() else { return }; | ||
| // Skip if not `const` variable. | ||
| if !ctx.scoping().symbol_flags(symbol_id).is_const_variable() { | ||
| return; | ||
| } | ||
| let Some(value) = decl.init.evaluate_value(ctx) else { return }; | ||
| ctx.init_value(symbol_id, value); | ||
| } | ||
|
|
||
| pub fn inline_identifier_reference(&self, expr: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) { | ||
| let Expression::Identifier(ident) = expr else { return }; | ||
| let Some(reference_id) = ident.reference_id.get() else { return }; | ||
| let Some(symbol_id) = ctx.scoping().get_reference(reference_id).symbol_id() else { return }; | ||
| let Some(symbol_value) = ctx.state.symbol_values.get_symbol_value(symbol_id) else { | ||
| return; | ||
| }; | ||
| // Only inline single reference (for now). | ||
| if symbol_value.read_references_count > 1 { | ||
| return; | ||
| } | ||
| // Skip if there are write references. | ||
| if symbol_value.write_references_count > 0 { | ||
| return; | ||
| } | ||
| *expr = ctx.value_to_expr(expr.span(), symbol_value.constant.clone()); | ||
| ctx.state.changed = true; | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use crate::{ | ||
| CompressOptions, | ||
| tester::{test_options, test_same}, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn r#const() { | ||
| let options = CompressOptions::smallest(); | ||
| test_options("const foo = 1; log(foo)", "log(1)", &options); | ||
| test_options("export const foo = 1; log(foo)", "export const foo = 1; log(1)", &options); | ||
| test_same("const foo = 1; log(foo), log(foo)"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| use rustc_hash::FxHashMap; | ||
|
|
||
| use oxc_ecmascript::constant_evaluation::ConstantValue; | ||
| use oxc_syntax::{scope::ScopeId, symbol::SymbolId}; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct SymbolValue<'a> { | ||
| /// Constant value evaluated from expressions. | ||
| pub constant: ConstantValue<'a>, | ||
|
|
||
| pub read_references_count: u32, | ||
| pub write_references_count: u32, | ||
|
|
||
| #[expect(unused)] | ||
| pub scope_id: ScopeId, | ||
| } | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct SymbolValues<'a> { | ||
| values: FxHashMap<SymbolId, SymbolValue<'a>>, | ||
| } | ||
|
|
||
| impl<'a> SymbolValues<'a> { | ||
| pub fn clear(&mut self) { | ||
| self.values.clear(); | ||
| } | ||
|
|
||
| pub fn init_value(&mut self, symbol_id: SymbolId, symbol_value: SymbolValue<'a>) { | ||
| self.values.insert(symbol_id, symbol_value); | ||
| } | ||
|
|
||
| pub fn get_constant_value(&self, symbol_id: SymbolId) -> Option<&ConstantValue<'a>> { | ||
| self.values.get(&symbol_id).map(|v| &v.constant) | ||
| } | ||
|
|
||
| pub fn get_symbol_value(&self, symbol_id: SymbolId) -> Option<&SymbolValue<'a>> { | ||
| self.values.get(&symbol_id) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.