Skip to content

Commit d24f785

Browse files
authored
perf(es/utils): Prevent too many recursion (#9931)
**Description:** ![image](https://github.com/user-attachments/assets/40841d3a-9fbf-41b5-98dd-45320d2e5261) Some functions may recurse too much and have a bad effect on the performance.
1 parent e4c1e03 commit d24f785

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2720
-2593
lines changed

.changeset/tall-tigers-explain.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_ast: patch
3+
swc_ecma_utils: major
4+
---
5+
6+
perf(es/utils): Prevent too many recursion
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//// [destructuringArrayBindingPatternAndAssignment3.ts]
2-
var _param, _param1, _param2;
32
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
3+
var _param, _param1, tmp1, e, _param2, tmp2 = void 0, e1 = void 0 === tmp2 ? e1 : tmp2;
44
(_param = _sliced_to_array([
55
1
66
], 2))[0], _param[1], (_param1 = _sliced_to_array([
77
1
8-
], 3))[0], _param1[1], _param1[2], (_param2 = _sliced_to_array([
8+
], 3))[0], _param1[1], e = void 0 === (tmp1 = _param1[2]) ? e : tmp1, (_param2 = _sliced_to_array([
99
1
1010
], 4))[0], _param2[1], _param2[2], _param2[3];
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
//// [destructuringObjectBindingPatternAndAssignment4.ts]
2-
var _ref = {};
3-
_ref.a, _ref.b, _ref.c, _ref.d, _ref.e, _ref.f;
2+
var _ref = {}, _ref_f = (_ref.a, _ref.b, _ref.c, _ref.d, _ref.e, _ref.f), f = void 0 === _ref_f ? f : _ref_f;

crates/swc/tests/tsc-references/destructuringVariableDeclaration1ES5.2.minified.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ var _ref2 = [
1111
void 0,
1212
void 0,
1313
void 0
14-
];
15-
_ref2[0], _ref2[1], _ref2[2];
16-
var _concat = _sliced_to_array([
14+
], _concat = (_ref2[0], _ref2[1], _ref2[2], _sliced_to_array([
1715
1,
1816
"string"
1917
].concat(_to_consumable_array([
2018
!0,
2119
!1,
2220
!0
23-
])), 2);
21+
])), 2));
2422
_concat[0], _concat[1];
2523
var _ref_e = _sliced_to_array([
2624
1,
@@ -29,14 +27,12 @@ var _ref_e = _sliced_to_array([
2927
b1: 4,
3028
b4: 0
3129
}
32-
], 3);
33-
_ref_e[0], _ref_e[1], _ref_e[2];
34-
var _ref_f = _sliced_to_array([
30+
], 3), _ref_f = (_ref_e[0], _ref_e[1], _ref_e[2], _sliced_to_array([
3531
1,
3632
2,
3733
{
3834
f3: 4,
3935
f5: 0
4036
}
41-
], 3), _ref_f_ = (_ref_f[0], _ref_f[1], _ref_f[2]);
37+
], 3)), _ref_f_ = (_ref_f[0], _ref_f[1], _ref_f[2]);
4238
_ref_f_.f3, _ref_f_.f5;

crates/swc/tests/tsc-references/destructuringVariableDeclaration1ES5iterable.2.minified.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ var _ref2 = [
1111
void 0,
1212
void 0,
1313
void 0
14-
];
15-
_ref2[0], _ref2[1], _ref2[2];
16-
var _concat = _sliced_to_array([
14+
], _concat = (_ref2[0], _ref2[1], _ref2[2], _sliced_to_array([
1715
1,
1816
"string"
1917
].concat(_to_consumable_array([
2018
!0,
2119
!1,
2220
!0
23-
])), 2);
21+
])), 2));
2422
_concat[0], _concat[1];
2523
var _ref_e = _sliced_to_array([
2624
1,
@@ -29,14 +27,12 @@ var _ref_e = _sliced_to_array([
2927
b1: 4,
3028
b4: 0
3129
}
32-
], 3);
33-
_ref_e[0], _ref_e[1], _ref_e[2];
34-
var _ref_f = _sliced_to_array([
30+
], 3), _ref_f = (_ref_e[0], _ref_e[1], _ref_e[2], _sliced_to_array([
3531
1,
3632
2,
3733
{
3834
f3: 4,
3935
f5: 0
4036
}
41-
], 3), _ref_f_ = (_ref_f[0], _ref_f[1], _ref_f[2]);
37+
], 3)), _ref_f_ = (_ref_f[0], _ref_f[1], _ref_f[2]);
4238
_ref_f_.f3, _ref_f_.f5;

crates/swc/tests/tsc-references/destructuringWithLiteralInitializers.2.minified.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//// [destructuringWithLiteralInitializers.ts]
2-
var param, param1, param2, param3, param4, param5, param6, _param, _param1;
2+
var param, _param, _param1, param1, param2, param3, param4, param5, param6;
33
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
44
function f4() {
55
var _ref = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {

crates/swc/tests/tsc-references/privateNameStaticEmitHelpers.2.minified.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//// [privateNameStaticEmitHelpers.ts]
22
//// [main.ts]
3-
import "@swc/helpers/_/_class_static_private_field_spec_set";
4-
import "@swc/helpers/_/_class_static_private_method_get";
3+
import { _ as _class_static_private_field_spec_set } from "@swc/helpers/_/_class_static_private_field_spec_set";
4+
import { _ as _class_static_private_method_get } from "@swc/helpers/_/_class_static_private_method_get";
55
export class S {
66
}
77
//// [tslib.d.ts]

crates/swc_ecma_ast/src/expr.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,13 @@ impl Expr {
200200
}
201201
}
202202

203-
pub fn is_ident_ref_to(&self, ident: &str) -> bool {
203+
pub fn is_ident_ref_to<S>(&self, ident: &S) -> bool
204+
where
205+
S: ?Sized,
206+
Atom: PartialEq<S>,
207+
{
204208
match self {
205-
Expr::Ident(i) => i.sym == ident,
209+
Expr::Ident(i) => i.sym == *ident,
206210
_ => false,
207211
}
208212
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Optimizer<'_> {
2222
is_ret_val_ignored: bool,
2323
) -> bool {
2424
let cost = negate_cost(
25-
&self.ctx.expr_ctx,
25+
self.ctx.expr_ctx,
2626
expr,
2727
is_ret_val_ignored,
2828
is_ret_val_ignored,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Optimizer<'_> {
3030
_ => {}
3131
}
3232

33-
if negate_cost(&self.ctx.expr_ctx, &stmt.test, true, false) < 0 {
33+
if negate_cost(self.ctx.expr_ctx, &stmt.test, true, false) < 0 {
3434
report_change!("if_return: Negating `cond` of an if statement which has cons and alt");
3535
let ctx = Ctx {
3636
in_bool_ctx: true,
@@ -63,7 +63,7 @@ impl Optimizer<'_> {
6363
_ => return,
6464
};
6565

66-
if !cond.cons.may_have_side_effects(&self.ctx.expr_ctx) {
66+
if !cond.cons.may_have_side_effects(self.ctx.expr_ctx) {
6767
self.changed = true;
6868
report_change!("conditionals: `cond ? useless : alt` => `cond || alt`");
6969
*e = BinExpr {
@@ -76,7 +76,7 @@ impl Optimizer<'_> {
7676
return;
7777
}
7878

79-
if !cond.alt.may_have_side_effects(&self.ctx.expr_ctx) {
79+
if !cond.alt.may_have_side_effects(self.ctx.expr_ctx) {
8080
self.changed = true;
8181
report_change!("conditionals: `cond ? cons : useless` => `cond && cons`");
8282
*e = BinExpr {
@@ -880,7 +880,7 @@ impl Optimizer<'_> {
880880
) = (&*cons, &*alt)
881881
{
882882
// I don't know why, but terser behaves differently
883-
negate(&self.ctx.expr_ctx, &mut test, true, false);
883+
negate(self.ctx.expr_ctx, &mut test, true, false);
884884

885885
swap(&mut cons, &mut alt);
886886
}

0 commit comments

Comments
 (0)