Skip to content

Commit 43a77ca

Browse files
committed
perf(es/minifier): less clone for ExprCtx
1 parent 73d01e8 commit 43a77ca

File tree

15 files changed

+269
-187
lines changed

15 files changed

+269
-187
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,14 @@ impl Optimizer<'_> {
183183
_ => return None,
184184
};
185185

186-
let lt = left.get_type(self.ctx.expr_ctx);
187-
let rt = right.get_type(self.ctx.expr_ctx);
186+
let lt = left.get_type(
187+
self.ctx.expr_ctx.unresolved_ctxt,
188+
self.ctx.expr_ctx.remaining_depth,
189+
);
190+
let rt = right.get_type(
191+
self.ctx.expr_ctx.unresolved_ctxt,
192+
self.ctx.expr_ctx.remaining_depth,
193+
);
188194
if let (Value::Known(Type::Undefined), Value::Known(Type::Null))
189195
| (Value::Known(Type::Null), Value::Known(Type::Undefined)) = (lt, rt)
190196
{

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,11 +1562,17 @@ impl VisitMut for Optimizer<'_> {
15621562
self.optimize_bin_and_or(n);
15631563

15641564
if n.op == op!(bin, "+") {
1565-
if let Known(Type::Str) = n.left.get_type(self.ctx.expr_ctx) {
1565+
if let Known(Type::Str) = n.left.get_type(
1566+
self.ctx.expr_ctx.unresolved_ctxt,
1567+
self.ctx.expr_ctx.remaining_depth,
1568+
) {
15661569
self.optimize_expr_in_str_ctx(&mut n.right);
15671570
}
15681571

1569-
if let Known(Type::Str) = n.right.get_type(self.ctx.expr_ctx) {
1572+
if let Known(Type::Str) = n.right.get_type(
1573+
self.ctx.expr_ctx.unresolved_ctxt,
1574+
self.ctx.expr_ctx.remaining_depth,
1575+
) {
15701576
self.optimize_expr_in_str_ctx(&mut n.left);
15711577
}
15721578
}

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ impl Optimizer<'_> {
5757
}
5858

5959
if e.op == op!("===") {
60-
if let Known(lt) = e.left.get_type(self.ctx.expr_ctx) {
61-
if let Known(rt) = e.right.get_type(self.ctx.expr_ctx) {
60+
if let Known(lt) = e.left.get_type(
61+
self.ctx.expr_ctx.unresolved_ctxt,
62+
self.ctx.expr_ctx.remaining_depth,
63+
) {
64+
if let Known(rt) = e.right.get_type(
65+
self.ctx.expr_ctx.unresolved_ctxt,
66+
self.ctx.expr_ctx.remaining_depth,
67+
) {
6268
if lt == rt {
6369
e.op = op!("==");
6470
self.changed = true;
@@ -131,7 +137,10 @@ impl Optimizer<'_> {
131137
| Expr::Bin(BinExpr { op: op!("<"), .. })
132138
| Expr::Bin(BinExpr { op: op!(">="), .. })
133139
| Expr::Bin(BinExpr { op: op!(">"), .. }) => {
134-
if let Known(Type::Bool) = arg.get_type(self.ctx.expr_ctx) {
140+
if let Known(Type::Bool) = arg.get_type(
141+
self.ctx.expr_ctx.unresolved_ctxt,
142+
self.ctx.expr_ctx.remaining_depth,
143+
) {
135144
self.changed = true;
136145
report_change!("Optimizing: `!!expr` => `expr`");
137146
*e = *arg.take();
@@ -187,14 +196,20 @@ impl Optimizer<'_> {
187196
_ => {}
188197
}
189198

190-
let lt = bin.left.get_type(self.ctx.expr_ctx);
199+
let lt = bin.left.get_type(
200+
self.ctx.expr_ctx.unresolved_ctxt,
201+
self.ctx.expr_ctx.remaining_depth,
202+
);
191203
match lt {
192204
// Don't change type
193205
Known(Type::Bool) => {}
194206
_ => return,
195207
}
196208

197-
let rt = bin.right.get_type(self.ctx.expr_ctx);
209+
let rt = bin.right.get_type(
210+
self.ctx.expr_ctx.unresolved_ctxt,
211+
self.ctx.expr_ctx.remaining_depth,
212+
);
198213
match rt {
199214
Known(Type::Bool) => {}
200215
_ => return,

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ impl Optimizer<'_> {
13401340
}
13411341

13421342
if let Callee::Expr(callee) = &e.callee {
1343-
if callee.is_pure_callee(self.ctx.expr_ctx) {
1343+
if callee.is_pure_callee(self.ctx.expr_ctx.unresolved_ctxt) {
13441344
if !self.is_skippable_for_seq(a, callee) {
13451345
return false;
13461346
}
@@ -1403,7 +1403,7 @@ impl Optimizer<'_> {
14031403
false
14041404
}
14051405
OptChainBase::Call(e) => {
1406-
if e.callee.is_pure_callee(self.ctx.expr_ctx) {
1406+
if e.callee.is_pure_callee(self.ctx.expr_ctx.unresolved_ctxt) {
14071407
if !self.is_skippable_for_seq(a, &e.callee) {
14081408
return false;
14091409
}
@@ -2371,7 +2371,12 @@ impl Optimizer<'_> {
23712371
Mergable::Drop => return Ok(false),
23722372
};
23732373

2374-
let a_type = a_right.as_deref().map(|a| a.get_type(self.ctx.expr_ctx));
2374+
let a_type = a_right.as_deref().map(|a| {
2375+
a.get_type(
2376+
self.ctx.expr_ctx.unresolved_ctxt,
2377+
self.ctx.expr_ctx.remaining_depth,
2378+
)
2379+
});
23752380

23762381
if let Some(a_right) = a_right {
23772382
if a_right.is_this() || a_right.is_ident_ref_to("arguments") {
@@ -2487,7 +2492,10 @@ impl Optimizer<'_> {
24872492
let Some(a_type) = a_type else {
24882493
return Ok(false);
24892494
};
2490-
let b_type = b.right.get_type(self.ctx.expr_ctx);
2495+
let b_type = b.right.get_type(
2496+
self.ctx.expr_ctx.unresolved_ctxt,
2497+
self.ctx.expr_ctx.remaining_depth,
2498+
);
24912499

24922500
if let Some(a_op) = a_op {
24932501
if can_drop_op_for(a_op, b.op, var_type, a_type, b_type) {

crates/swc_ecma_minifier/src/compress/pure/bools.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl Pure<'_> {
263263
|| is_typeof_unaray(&e.right, &e.left)
264264
|| (self.options.comparisons
265265
&& matches!(
266-
(e.left.get_type(self.expr_ctx), e.right.get_type(self.expr_ctx)),
266+
(e.left.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth), e.right.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth)),
267267
(Value::Known(l), Value::Known(r)) if l == r
268268
));
269269

@@ -306,8 +306,10 @@ impl Pure<'_> {
306306
right,
307307
..
308308
}) => {
309-
let lt = left.get_type(self.expr_ctx);
310-
let rt = right.get_type(self.expr_ctx);
309+
let lt =
310+
left.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
311+
let rt =
312+
right.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
311313

312314
if let (Value::Known(Type::Bool), Value::Known(Type::Bool)) = (lt, rt) {
313315
let rb = right.as_pure_bool(self.expr_ctx);

crates/swc_ecma_minifier/src/compress/pure/conds.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ impl Pure<'_> {
107107

108108
let Expr::Cond(cond) = e else { return };
109109

110-
let lt = cond.cons.get_type(self.expr_ctx);
110+
let lt = cond
111+
.cons
112+
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
111113
if let Value::Known(Type::Bool) = lt {
112114
let lb = cond.cons.as_pure_bool(self.expr_ctx);
113115
if let Value::Known(true) = lb {
@@ -145,7 +147,9 @@ impl Pure<'_> {
145147
}
146148
}
147149

148-
let rt = cond.alt.get_type(self.expr_ctx);
150+
let rt = cond
151+
.alt
152+
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
149153
if let Value::Known(Type::Bool) = rt {
150154
let rb = cond.alt.as_pure_bool(self.expr_ctx);
151155
if let Value::Known(false) = rb {
@@ -233,7 +237,10 @@ impl Pure<'_> {
233237
Expr::Lit(Lit::Num(Number { value: 0.0, .. })),
234238
) if *value > 0.0
235239
&& (!cond.test.is_bin()
236-
|| cond.test.get_type(self.expr_ctx) == Value::Known(Type::Bool)) =>
240+
|| cond.test.get_type(
241+
self.expr_ctx.unresolved_ctxt,
242+
self.expr_ctx.remaining_depth,
243+
) == Value::Known(Type::Bool)) =>
237244
{
238245
report_change!("conditionals: `foo ? num : 0` => `num * !!foo`");
239246
self.changed = true;
@@ -292,8 +299,12 @@ impl Pure<'_> {
292299
return;
293300
}
294301

295-
let lt = bin.left.get_type(self.expr_ctx);
296-
let rt = bin.right.get_type(self.expr_ctx);
302+
let lt = bin
303+
.left
304+
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
305+
let rt = bin
306+
.right
307+
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
297308

298309
let _lb = bin.left.as_pure_bool(self.expr_ctx);
299310
let rb = bin.right.as_pure_bool(self.expr_ctx);

crates/swc_ecma_minifier/src/compress/pure/evaluate.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ impl Pure<'_> {
3535
)
3636
};
3737
match (
38-
n.left.get_type(self.expr_ctx).opt()?,
39-
n.right.get_type(self.expr_ctx).opt()?,
38+
n.left
39+
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth)
40+
.opt()?,
41+
n.right
42+
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth)
43+
.opt()?,
4044
) {
4145
// Abort if types differ, or one of them is unknown.
4246
(lt, rt) if lt != rt => {}

crates/swc_ecma_minifier/src/compress/pure/misc.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,11 @@ impl Pure<'_> {
11341134
callee: Callee::Expr(callee),
11351135
args,
11361136
..
1137-
}) if callee.is_one_of_global_ref_to(self.expr_ctx, &["Array", "Object", "RegExp"]) => {
1137+
}) if callee.is_one_of_global_ref_to(
1138+
self.expr_ctx.unresolved_ctxt,
1139+
&["Array", "Object", "RegExp"],
1140+
) =>
1141+
{
11381142
let new_expr = match &**callee {
11391143
Expr::Ident(Ident { sym, .. }) if &**sym == "RegExp" => {
11401144
self.optimize_regex(args, span)
@@ -1163,7 +1167,7 @@ impl Pure<'_> {
11631167
args,
11641168
..
11651169
}) if callee.is_one_of_global_ref_to(
1166-
self.expr_ctx,
1170+
self.expr_ctx.unresolved_ctxt,
11671171
&["Boolean", "Number", "String", "Symbol"],
11681172
) =>
11691173
{
@@ -1273,7 +1277,7 @@ impl Pure<'_> {
12731277
args,
12741278
..
12751279
}) if callee.is_one_of_global_ref_to(
1276-
self.expr_ctx,
1280+
self.expr_ctx.unresolved_ctxt,
12771281
&[
12781282
"Object",
12791283
// https://262.ecma-international.org/12.0/#sec-array-constructor
@@ -1292,7 +1296,7 @@ impl Pure<'_> {
12921296
"TypeError",
12931297
"URIError",
12941298
],
1295-
) || (callee.is_global_ref_to(self.expr_ctx, "RegExp")
1299+
) || (callee.is_global_ref_to(self.expr_ctx.unresolved_ctxt, "RegExp")
12961300
&& can_compress_new_regexp(args.as_deref())) =>
12971301
{
12981302
self.changed = true;
@@ -1615,7 +1619,7 @@ impl Pure<'_> {
16151619
callee: Callee::Expr(callee),
16161620
args,
16171621
..
1618-
}) if callee.is_pure_callee(self.expr_ctx) => {
1622+
}) if callee.is_pure_callee(self.expr_ctx.unresolved_ctxt) => {
16191623
report_change!("ignore_return_value: Dropping a pure call (callee is pure)");
16201624
self.changed = true;
16211625

@@ -1644,7 +1648,9 @@ impl Pure<'_> {
16441648
ctxt,
16451649
args,
16461650
..
1647-
}) if callee.is_pure_callee(self.expr_ctx) || ctxt.has_mark(self.marks.pure) => {
1651+
}) if callee.is_pure_callee(self.expr_ctx.unresolved_ctxt)
1652+
|| ctxt.has_mark(self.marks.pure) =>
1653+
{
16481654
report_change!("ignore_return_value: Dropping a pure call");
16491655
self.changed = true;
16501656

@@ -1667,7 +1673,7 @@ impl Pure<'_> {
16671673
args,
16681674
..
16691675
}) => {
1670-
if callee.is_pure_callee(self.expr_ctx) {
1676+
if callee.is_pure_callee(self.expr_ctx.unresolved_ctxt) {
16711677
self.changed = true;
16721678
report_change!("Dropping pure call as callee is pure");
16731679
*e = self
@@ -1683,7 +1689,7 @@ impl Pure<'_> {
16831689
tpl,
16841690
..
16851691
}) => {
1686-
if callee.is_pure_callee(self.expr_ctx) {
1692+
if callee.is_pure_callee(self.expr_ctx.unresolved_ctxt) {
16871693
self.changed = true;
16881694
report_change!("Dropping pure tag tpl as callee is pure");
16891695
*e = self
@@ -2157,7 +2163,7 @@ impl Pure<'_> {
21572163
Expr::New(NewExpr {
21582164
span, callee, args, ..
21592165
}) if callee.is_one_of_global_ref_to(
2160-
self.expr_ctx,
2166+
self.expr_ctx.unresolved_ctxt,
21612167
&[
21622168
"Map", "Set", "Array", "Object", "Boolean", "Number", "String",
21632169
],
@@ -2181,7 +2187,7 @@ impl Pure<'_> {
21812187
args,
21822188
..
21832189
}) if callee.is_one_of_global_ref_to(
2184-
self.expr_ctx,
2190+
self.expr_ctx.unresolved_ctxt,
21852191
&["Array", "Object", "Boolean", "Number"],
21862192
) =>
21872193
{
@@ -2422,8 +2428,12 @@ impl Pure<'_> {
24222428
_ => return,
24232429
};
24242430

2425-
let lt = cond.cons.get_type(self.expr_ctx);
2426-
let rt = cond.alt.get_type(self.expr_ctx);
2431+
let lt = cond
2432+
.cons
2433+
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
2434+
let rt = cond
2435+
.alt
2436+
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
24272437
match (lt, rt) {
24282438
(Known(Type::Bool), Known(Type::Bool)) => {}
24292439
_ => return,

crates/swc_ecma_minifier/src/compress/pure/strings.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ impl Pure<'_> {
2828
_ => return,
2929
};
3030

31-
match l_l.get_type(self.expr_ctx) {
31+
match l_l.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth) {
3232
Known(Type::Str) => {}
3333
_ => return,
3434
}
35-
match r_l.get_type(self.expr_ctx) {
35+
match r_l.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth) {
3636
Known(Type::Str) => {}
3737
_ => return,
3838
}
@@ -487,8 +487,12 @@ impl Pure<'_> {
487487
},
488488
) = &mut *bin.left
489489
{
490-
let type_of_second = left.right.get_type(self.expr_ctx);
491-
let type_of_third = bin.right.get_type(self.expr_ctx);
490+
let type_of_second = left
491+
.right
492+
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
493+
let type_of_third = bin
494+
.right
495+
.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
492496

493497
if let Value::Known(Type::Str) = type_of_second {
494498
if let Value::Known(Type::Str) = type_of_third {
@@ -534,8 +538,8 @@ impl Pure<'_> {
534538
..
535539
}) = e
536540
{
537-
let lt = left.get_type(self.expr_ctx);
538-
let rt = right.get_type(self.expr_ctx);
541+
let lt = left.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
542+
let rt = right.get_type(self.expr_ctx.unresolved_ctxt, self.expr_ctx.remaining_depth);
539543
if let Value::Known(Type::Str) = lt {
540544
if let Value::Known(Type::Str) = rt {
541545
match &**left {

crates/swc_ecma_minifier/src/compress/util/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub(crate) fn is_pure_undefined(expr_ctx: ExprCtx, e: &Expr) -> bool {
370370
..
371371
}) if !arg.may_have_side_effects(expr_ctx) => true,
372372

373-
_ => e.is_undefined(expr_ctx),
373+
_ => e.is_undefined(expr_ctx.unresolved_ctxt),
374374
}
375375
}
376376

@@ -419,7 +419,7 @@ pub(crate) fn eval_to_undefined(expr_ctx: ExprCtx, e: &Expr) -> bool {
419419
eval_to_undefined(expr_ctx, &c.cons) && eval_to_undefined(expr_ctx, &c.alt)
420420
}
421421

422-
_ => e.is_undefined(expr_ctx),
422+
_ => e.is_undefined(expr_ctx.unresolved_ctxt),
423423
}
424424
}
425425

@@ -787,7 +787,10 @@ pub(crate) fn can_absorb_negate(e: &Expr, expr_ctx: ExprCtx) -> bool {
787787
Expr::Bin(BinExpr { op, .. }) if is_eq(*op) => true,
788788
Expr::Unary(UnaryExpr {
789789
op: op!("!"), arg, ..
790-
}) => arg.get_type(expr_ctx) == Value::Known(Type::Bool),
790+
}) => {
791+
arg.get_type(expr_ctx.unresolved_ctxt, expr_ctx.remaining_depth)
792+
== Value::Known(Type::Bool)
793+
}
791794
_ => false,
792795
}
793796
}

0 commit comments

Comments
 (0)