Skip to content

Commit 73d01e8

Browse files
committed
perf(es/minifier): less clone
1 parent 49ba5cc commit 73d01e8

File tree

2 files changed

+14
-20
lines changed
  • crates
    • swc_ecma_minifier/src/compress/optimize
    • swc_ecma_usage_analyzer/src/analyzer

2 files changed

+14
-20
lines changed

crates/swc_ecma_minifier/src/compress/optimize/unused.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,19 @@ impl Optimizer<'_> {
107107

108108
#[cfg_attr(feature = "debug", tracing::instrument(level = "debug", skip_all))]
109109
pub(super) fn drop_unused_vars(&mut self, name: &mut Pat, init: Option<&mut Expr>) {
110-
if self
111-
.ctx
112-
.bit_ctx
113-
.intersects(BitCtx::IsExported | BitCtx::InAsm)
114-
{
110+
if !self.options.unused && !self.options.side_effects {
115111
return;
116112
}
117113

118-
trace_op!("unused: drop_unused_vars({})", dump(&*name, false));
119-
120-
if !self.options.unused && !self.options.side_effects {
114+
if self.ctx.bit_ctx.intersects(
115+
BitCtx::IsExported
116+
.union(BitCtx::InAsm)
117+
.union(BitCtx::InVarDeclOfForInOrOfLoop),
118+
) {
121119
return;
122120
}
123121

124-
if self.ctx.bit_ctx.contains(BitCtx::InVarDeclOfForInOrOfLoop) {
122+
if !name.is_ident() && init.is_none() {
125123
return;
126124
}
127125

@@ -135,9 +133,7 @@ impl Optimizer<'_> {
135133
}
136134
}
137135

138-
if !name.is_ident() && init.is_none() {
139-
return;
140-
}
136+
trace_op!("unused: drop_unused_vars({})", dump(&*name, false));
141137

142138
self.take_pat_if_unused(name, init, true);
143139
}

crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use ctx::BitContext;
22
use rustc_hash::FxHashMap;
33
use swc_common::SyntaxContext;
44
use swc_ecma_ast::*;
5-
use swc_ecma_utils::{
6-
find_pat_ids, ident::IdentLike, ExprCtx, ExprExt, IsEmpty, StmtExt, Type, Value,
7-
};
5+
use swc_ecma_utils::{find_pat_ids, ExprCtx, ExprExt, IsEmpty, StmtExt, Type, Value};
86
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
97
use swc_timer::timer;
108

@@ -159,7 +157,7 @@ where
159157
self.data.report_usage(self.ctx, i.clone());
160158
self.data.var_or_default(i.clone()).mark_used_above_decl()
161159
}
162-
self.data.var_or_default(i.clone()).mark_used_recursively();
160+
self.data.var_or_default(i).mark_used_recursively();
163161
return;
164162
}
165163

@@ -301,7 +299,7 @@ where
301299

302300
if n.op == op!("=") {
303301
let left = match &n.left {
304-
AssignTarget::Simple(left) => left.leftmost().map(Ident::to_id),
302+
AssignTarget::Simple(left) => left.leftmost(),
305303
AssignTarget::Pat(..) => None,
306304
};
307305

@@ -319,7 +317,7 @@ where
319317
v = Some(self.data.var_or_default(left.to_id()));
320318
}
321319

322-
v.as_mut().unwrap().add_infects_to(id.clone());
320+
v.as_mut().unwrap().add_infects_to(id);
323321
}
324322
}
325323
}
@@ -800,7 +798,7 @@ where
800798
v = Some(self.data.var_or_default(n.ident.to_id()));
801799
}
802800

803-
v.as_mut().unwrap().add_infects_to(id.clone());
801+
v.as_mut().unwrap().add_infects_to(id);
804802
}
805803
}
806804
}
@@ -1390,7 +1388,7 @@ where
13901388
v = Some(self.data.var_or_default(var.to_id()));
13911389
}
13921390

1393-
v.as_mut().unwrap().add_infects_to(id.clone());
1391+
v.as_mut().unwrap().add_infects_to(id);
13941392
}
13951393
}
13961394
}

0 commit comments

Comments
 (0)