perf(es/minifier): Remove needless type parameter (#3897)

This commit is contained in:
Donny/강동윤 2022-03-08 19:04:14 +09:00 committed by GitHub
parent 782df70560
commit 8b3aa6d3aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 54 additions and 114 deletions

View File

@ -320,7 +320,7 @@ where
let mut visitor = pure_optimizer(
self.options,
self.marks,
self.mode,
M::force_str_for_tpl(),
self.pass > 1,
self.pass >= 20,
);

View File

@ -3,13 +3,9 @@ use swc_ecma_ast::*;
use swc_ecma_utils::contains_this_expr;
use super::Pure;
use crate::mode::Mode;
/// Methods related to the option `arrows`.
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
pub(super) fn unsafe_optimize_fn_as_arrow(&mut self, e: &mut Expr) {
if self.options.ecma < EsVersion::Es2015 {
return;

View File

@ -8,15 +8,11 @@ use swc_ecma_utils::{ExprExt, Type, Value};
use super::Pure;
use crate::{
compress::util::{is_pure_undefined, negate, negate_cost},
mode::Mode,
option::CompressOptions,
util::make_bool,
};
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
pub(super) fn negate_twice(&mut self, e: &mut Expr, is_ret_val_ignored: bool) {
negate(e, false, is_ret_val_ignored);
negate(e, false, is_ret_val_ignored);

View File

@ -5,12 +5,9 @@ use swc_ecma_ast::*;
use swc_ecma_utils::{ExprExt, Type, Value};
use super::Pure;
use crate::{compress::util::negate_cost, debug::dump, mode::Mode, util::make_bool};
use crate::{compress::util::negate_cost, debug::dump, util::make_bool};
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
///
/// - `foo ? bar : false` => `!!foo && bar`
/// - `!foo ? true : bar` => `!foo || bar`

View File

@ -4,6 +4,9 @@ use super::Pure;
#[derive(Default, Clone, Copy)]
pub(super) struct Ctx {
/// This is actually config, but stored here.
pub force_str_for_tpl: bool,
pub par_depth: u8,
pub in_delete: bool,
@ -24,10 +27,10 @@ pub(super) struct Ctx {
pub in_first_expr: bool,
}
impl<'b, M> Pure<'b, M> {
impl<'b> Pure<'b> {
/// RAII guard to change context temporarically
#[inline]
pub(super) fn with_ctx(&mut self, ctx: Ctx) -> WithCtx<'_, 'b, M> {
pub(super) fn with_ctx(&mut self, ctx: Ctx) -> WithCtx<'_, 'b> {
let orig_ctx = self.ctx;
self.ctx = ctx;
WithCtx {
@ -37,26 +40,26 @@ impl<'b, M> Pure<'b, M> {
}
}
pub(super) struct WithCtx<'a, 'b, M> {
pass: &'a mut Pure<'b, M>,
pub(super) struct WithCtx<'a, 'b> {
pass: &'a mut Pure<'b>,
orig_ctx: Ctx,
}
impl<'b, M> Deref for WithCtx<'_, 'b, M> {
type Target = Pure<'b, M>;
impl<'b> Deref for WithCtx<'_, 'b> {
type Target = Pure<'b>;
fn deref(&self) -> &Self::Target {
self.pass
}
}
impl<M> DerefMut for WithCtx<'_, '_, M> {
impl DerefMut for WithCtx<'_, '_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.pass
}
}
impl<M> Drop for WithCtx<'_, '_, M> {
impl Drop for WithCtx<'_, '_> {
fn drop(&mut self) {
self.pass.ctx = self.orig_ctx;
}

View File

@ -5,15 +5,11 @@ use swc_ecma_utils::{ExprExt, StmtLike, Value};
use super::Pure;
use crate::{
compress::util::{always_terminates, is_fine_for_if_cons},
mode::Mode,
util::ModuleItemExt,
};
/// Methods related to option `dead_code`.
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
pub(super) fn drop_unreachable_stmts<T>(&mut self, stmts: &mut Vec<T>)
where
T: StmtLike + ModuleItemExt + Take,

View File

@ -4,7 +4,7 @@ use swc_ecma_utils::undefined;
use super::Pure;
impl<M> Pure<'_, M> {
impl Pure<'_> {
pub(super) fn drop_console(&mut self, e: &mut Expr) {
if !self.options.drop_console {
return;

View File

@ -4,15 +4,9 @@ use swc_ecma_ast::*;
use swc_ecma_utils::{undefined, ExprExt, Value};
use super::Pure;
use crate::{
compress::util::{eval_as_number, is_pure_undefined_or_null},
mode::Mode,
};
use crate::compress::util::{eval_as_number, is_pure_undefined_or_null};
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
pub(super) fn eval_array_method_call(&mut self, e: &mut Expr) {
if !self.options.evaluate {
return;
@ -182,7 +176,7 @@ where
self.changed = true;
tracing::debug!(
"evaludate: Reduced `funtion.valueOf()` into a function expression"
"evaluate: Reduced `function.valueOf()` into a function expression"
);
*e = *obj.take();
@ -190,7 +184,7 @@ where
}
}
/// unsafely evaulate call to `Number`.
/// unsafely evaluate call to `Number`.
pub(super) fn eval_number_call(&mut self, e: &mut Expr) {
if self.options.unsafe_passes && self.options.unsafe_math {
if let Expr::Call(CallExpr {
@ -311,10 +305,7 @@ where
}
/// Evaluation of strings.
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
/// Handle calls on string literals, like `'foo'.toUpperCase()`.
pub(super) fn eval_str_method_call(&mut self, e: &mut Expr) {
if !self.options.evaluate {

View File

@ -4,7 +4,7 @@ use swc_ecma_ast::*;
use super::Pure;
use crate::compress::util::{is_fine_for_if_cons, negate};
impl<M> Pure<'_, M> {
impl Pure<'_> {
/// # Input
///
/// ```js

View File

@ -3,12 +3,8 @@ use swc_ecma_ast::*;
use swc_ecma_utils::{ExprExt, Value};
use super::Pure;
use crate::mode::Mode;
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
///
/// - `while(test);` => `for(;;test);
/// - `do; while(true)` => `for(;;);

View File

@ -5,12 +5,9 @@ use swc_ecma_ast::*;
use swc_ecma_utils::ident::IdentLike;
use super::Pure;
use crate::{compress::util::is_pure_undefined, mode::Mode};
use crate::compress::util::is_pure_undefined;
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
pub(super) fn remove_invalid(&mut self, e: &mut Expr) {
if let Expr::Bin(BinExpr { left, right, .. }) = e {
self.remove_invalid(left);

View File

@ -13,7 +13,6 @@ use self::{ctx::Ctx, misc::DropOpts};
use crate::{
debug::{dump, AssertValid},
marks::Marks,
mode::Mode,
option::CompressOptions,
util::ModuleItemExt,
MAX_PAR_DEPTH,
@ -36,43 +35,41 @@ mod strings;
mod unsafes;
mod vars;
pub(crate) fn pure_optimizer<'a, M>(
#[allow(clippy::needless_lifetimes)]
pub(crate) fn pure_optimizer<'a>(
options: &'a CompressOptions,
marks: Marks,
mode: &'a M,
force_str_for_tpl: bool,
enable_everything: bool,
debug_infinite_loop: bool,
) -> impl 'a + VisitMut + Repeated
where
M: Mode,
{
) -> impl 'a + VisitMut + Repeated {
Pure {
options,
marks,
ctx: Default::default(),
ctx: Ctx {
force_str_for_tpl,
..Default::default()
},
changed: Default::default(),
enable_everything,
mode,
debug_infinite_loop,
bindings: Default::default(),
}
}
struct Pure<'a, M> {
struct Pure<'a> {
options: &'a CompressOptions,
marks: Marks,
ctx: Ctx,
changed: bool,
enable_everything: bool,
mode: &'a M,
debug_infinite_loop: bool,
bindings: Option<Arc<AHashSet<Id>>>,
}
impl<M> Repeated for Pure<'_, M> {
impl Repeated for Pure<'_> {
fn changed(&self) -> bool {
self.changed
}
@ -84,10 +81,7 @@ impl<M> Repeated for Pure<'_, M> {
}
}
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
fn handle_stmt_likes<T>(&mut self, stmts: &mut Vec<T>)
where
T: ModuleItemExt + Take,
@ -154,7 +148,7 @@ where
/// Visit `nodes`, maybe in parallel.
fn visit_par<N>(&mut self, nodes: &mut Vec<N>)
where
N: for<'aa> VisitMutWith<Pure<'aa, M>> + Send + Sync,
N: for<'aa> VisitMutWith<Pure<'aa>> + Send + Sync,
{
if self.ctx.par_depth >= MAX_PAR_DEPTH * 2 || cfg!(target_arch = "wasm32") {
for node in nodes {
@ -164,7 +158,6 @@ where
ctx: self.ctx,
changed: false,
enable_everything: self.enable_everything,
mode: self.mode,
debug_infinite_loop: self.debug_infinite_loop,
bindings: self.bindings.clone(),
};
@ -187,7 +180,6 @@ where
},
changed: false,
enable_everything: self.enable_everything,
mode: self.mode,
debug_infinite_loop: self.debug_infinite_loop,
bindings: self.bindings.clone(),
};
@ -204,10 +196,7 @@ where
}
}
impl<M> VisitMut for Pure<'_, M>
where
M: Mode,
{
impl VisitMut for Pure<'_> {
noop_visit_mut_type!();
fn visit_mut_assign_expr(&mut self, e: &mut AssignExpr) {

View File

@ -2,12 +2,8 @@ use swc_common::util::take::Take;
use swc_ecma_ast::*;
use super::Pure;
use crate::mode::Mode;
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
pub(super) fn optimize_expr_in_num_ctx(&mut self, e: &mut Expr) {
if let Expr::Lit(Lit::Str(Str { span, value, .. })) = e {
let value = if value.is_empty() { 0f64 } else { 1f64 };

View File

@ -4,12 +4,9 @@ use swc_ecma_ast::*;
use swc_ecma_utils::{prop_name_eq, ExprExt};
use super::Pure;
use crate::{compress::util::is_valid_identifier, mode::Mode, util::deeply_contains_this_expr};
use crate::{compress::util::is_valid_identifier, util::deeply_contains_this_expr};
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
pub(super) fn optimize_property_of_member_expr(
&mut self,
obj: Option<&Expr>,

View File

@ -4,12 +4,8 @@ use swc_ecma_ast::*;
use swc_ecma_utils::{ExprExt, ExprFactory};
use super::Pure;
use crate::mode::Mode;
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
pub(super) fn drop_useless_ident_ref_in_seq(&mut self, seq: &mut SeqExpr) {
if !self.options.collapse_vars {
return;

View File

@ -6,12 +6,8 @@ use swc_ecma_ast::*;
use swc_ecma_utils::{ExprExt, Type, Value};
use super::Pure;
use crate::mode::Mode;
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
/// Converts template literals to string if `exprs` of [Tpl] is empty.
pub(super) fn convert_tpl_to_str(&mut self, e: &mut Expr) {
match e {
@ -20,9 +16,9 @@ where
if c.value.chars().all(|c| match c {
'\u{0020}'..='\u{007e}' => true,
'\n' | '\r' => M::force_str_for_tpl(),
'\n' | '\r' => self.ctx.force_str_for_tpl,
_ => false,
}) && (M::force_str_for_tpl()
}) && (self.ctx.force_str_for_tpl
|| (!c.value.contains("\\n") && !c.value.contains("\\r")))
&& !c.value.contains("\\0")
&& !c.value.contains("\\x")

View File

@ -3,12 +3,8 @@ use swc_ecma_ast::*;
use swc_ecma_utils::ExprExt;
use super::Pure;
use crate::mode::Mode;
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
/// Drop arguments of `Symbol()` call.
pub(super) fn drop_arguments_of_symbol_call(&mut self, e: &mut CallExpr) {
if !self.options.unsafe_symbols {

View File

@ -8,14 +8,10 @@ use swc_ecma_visit::{
use super::Pure;
use crate::{
compress::util::{drop_invalid_stmts, is_directive},
mode::Mode,
util::ModuleItemExt,
};
impl<M> Pure<'_, M>
where
M: Mode,
{
impl Pure<'_> {
/// Join variables.
///
/// This method may move variables to head of for statements like

View File

@ -220,11 +220,10 @@ impl Evaluator {
});
{
let data = self.data.clone();
e.visit_mut_with(&mut pure_optimizer(
&serde_json::from_str("{}").unwrap(),
self.marks,
&data,
Eval::force_str_for_tpl(),
true,
false,
));

View File

@ -12,6 +12,7 @@
//! them something other. Don't call methods like `visit_mut_script` nor
//! `visit_mut_module_items`.
#![deny(clippy::all)]
#![deny(unused)]
#![allow(clippy::blocks_in_if_conditions)]
#![allow(clippy::collapsible_else_if)]
#![allow(clippy::collapsible_if)]
@ -20,6 +21,7 @@
#![allow(unstable_name_collisions)]
use compress::pure_optimizer;
use mode::Mode;
use swc_common::{comments::Comments, pass::Repeat, sync::Lrc, SourceMap, SyntaxContext, GLOBALS};
use swc_ecma_ast::Module;
use swc_ecma_visit::{FoldWith, VisitMutWith};
@ -140,7 +142,7 @@ pub fn optimize(
m.visit_mut_with(&mut Repeat::new(pure_optimizer(
options,
marks,
&Minification,
Minification::force_str_for_tpl(),
true,
false,
)));

View File

@ -116,6 +116,7 @@
"punct",
"putc",
"qself",
"RAII",
"regs",
"repr",
"rfind",