Skip to content

Commit d9256ba

Browse files
committed
perf(es/minifier): bitflag for var kind
1 parent 4808875 commit d9256ba

2 files changed

Lines changed: 61 additions & 41 deletions

File tree

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

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ pub(super) fn optimizer<'a>(
7373
in_strict: options.module,
7474
remaining_depth: 6,
7575
},
76-
var_kind: None,
7776
scope: SyntaxContext::default(),
7877
bit_ctx: BitCtx::default(),
7978
};
@@ -101,8 +100,6 @@ pub(super) fn optimizer<'a>(
101100
struct Ctx {
102101
expr_ctx: ExprCtx,
103102

104-
var_kind: Option<VarDeclKind>,
105-
106103
/// Current scope.
107104
scope: SyntaxContext,
108105

@@ -120,68 +117,75 @@ bitflags! {
120117
#[derive(Debug, Clone, Copy, Default)]
121118
pub(crate) struct BitCtx: u32 {
122119
/// `true` if the [VarDecl] has const annotation.
123-
const HasConstAnn = 1 << 0;
120+
const IsConst = 1 << 0;
121+
const IsVar = 1 << 1;
122+
const IsLet = 1 << 2;
124123

125-
const DontUsePrependNorAppend = 1 << 1;
124+
const DontUsePrependNorAppend = 1 << 3;
126125

127-
const InBoolCtx = 1 << 2;
126+
const InBoolCtx = 1 << 4;
128127

129-
const InAsm = 1 << 3;
128+
const InAsm = 1 << 5;
130129

131130
/// `true` only for [Callee::Expr].
132-
const IsCallee = 1 << 4;
131+
const IsCallee = 1 << 6;
133132

134133
/// `true` if we are try block. `true` means we cannot be sure about control
135134
/// flow.
136-
const InTryBlock = 1 << 5;
135+
const InTryBlock = 1 << 7;
136+
137137
/// `true` while handling `test` of if / while / for.
138-
const InCond = 1 << 6;
138+
const InCond = 1 << 8;
139139

140140
/// `true` if we are in `arg` of `delete arg`.
141-
const IsDeleteArg = 1 << 7;
141+
const IsDeleteArg = 1 << 9;
142+
142143
/// `true` if we are in `arg` of `++arg` or `--arg`.
143-
const IsUpdateArg = 1 << 8;
144-
const IsLhsOfAssign = 1 << 9;
144+
const IsUpdateArg = 1 << 10;
145+
146+
const IsLhsOfAssign = 1 << 11;
147+
145148
/// `false` for `d` in `d[0] = foo`.
146-
const IsExactLhsOfAssign = 1 << 10;
149+
const IsExactLhsOfAssign = 1 << 12;
147150

148151
/// `true` for loop bodies and conditions of loops.
149-
const ExecutedMultipleTime = 1 << 11;
152+
const ExecutedMultipleTime = 1 << 13;
150153

151154
/// `true` while handling `expr` of `!expr`
152-
const InBangArg = 1 << 12;
153-
const InVarDeclOfForInOrOfLoop = 1 << 13;
155+
const InBangArg = 1 << 14;
156+
157+
const InVarDeclOfForInOrOfLoop = 1 << 15;
154158

155-
const DontUseNegatedIife = 1 << 14;
159+
const DontUseNegatedIife = 1 << 16;
156160

157161
/// `true` while handling top-level export decls.
158-
const IsExported = 1 << 15;
162+
const IsExported = 1 << 17;
159163

160164
/// `true` while handling top level items.
161-
const TopLevel = 1 << 16;
165+
const TopLevel = 1 << 18;
162166

163167
/// `true` while we are in a function or something similar.
164-
const InFnLike = 1 << 17;
168+
const InFnLike = 1 << 19;
165169

166-
const InBlock = 1 << 18;
170+
const InBlock = 1 << 20;
167171

168-
const InObjOfNonComputedMember = 1 << 19;
172+
const InObjOfNonComputedMember = 1 << 21;
169173

170-
const InTplExpr = 1 << 20;
174+
const InTplExpr = 1 << 22;
171175

172176
/// True while handling callee, except an arrow expression in callee.
173-
const IsThisAwareCallee = 1 << 21;
177+
const IsThisAwareCallee = 1 << 23;
174178

175-
const IsNestedIfReturnMerging = 1 << 22;
179+
const IsNestedIfReturnMerging = 1 << 24;
176180

177-
const DontInvokeIife = 1 << 23;
181+
const DontInvokeIife = 1 << 25;
178182

179-
const InWithStmt = 1 << 24;
183+
const InWithStmt = 1 << 26;
180184

181-
const InParam = 1 << 25;
185+
const InParam = 1 << 27;
182186

183187
/// `true` while we are inside a class body.
184-
const InClass = 1 << 26;
188+
const InClass = 1 << 28;
185189
}
186190
}
187191

@@ -2336,7 +2340,6 @@ impl VisitMut for Optimizer<'_> {
23362340
fn visit_mut_param(&mut self, n: &mut Param) {
23372341
let ctx = Ctx {
23382342
bit_ctx: self.ctx.bit_ctx.with(BitCtx::InParam, true),
2339-
var_kind: None,
23402343
..self.ctx.clone()
23412344
};
23422345
let mut o = self.with_ctx(ctx);
@@ -2769,8 +2772,9 @@ impl VisitMut for Optimizer<'_> {
27692772
.ctx
27702773
.bit_ctx
27712774
.with(BitCtx::IsUpdateArg, false)
2772-
.with(BitCtx::HasConstAnn, false),
2773-
var_kind: None,
2775+
.with(BitCtx::IsConst, false)
2776+
.with(BitCtx::IsLet, false)
2777+
.with(BitCtx::IsVar, false),
27742778
..self.ctx.clone()
27752779
};
27762780

@@ -2787,8 +2791,12 @@ impl VisitMut for Optimizer<'_> {
27872791
.ctx
27882792
.bit_ctx
27892793
.with(BitCtx::IsUpdateArg, false)
2790-
.with(BitCtx::HasConstAnn, self.has_const_ann(n.ctxt)),
2791-
var_kind: Some(n.kind),
2794+
.with(
2795+
BitCtx::IsConst,
2796+
n.kind == VarDeclKind::Const || self.has_const_ann(n.ctxt),
2797+
)
2798+
.with(BitCtx::IsVar, n.kind == VarDeclKind::Var)
2799+
.with(BitCtx::IsLet, n.kind == VarDeclKind::Let),
27922800
..self.ctx.clone()
27932801
};
27942802

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ impl Optimizer<'_> {
7171

7272
#[cfg(debug_assertions)]
7373
{
74-
if let Some(VarDeclKind::Const | VarDeclKind::Let) = self.ctx.var_kind {
75-
if had_init && var.init.is_none() {
76-
unreachable!("const/let variable without initializer: {:#?}", var);
77-
}
74+
if self
75+
.ctx
76+
.bit_ctx
77+
.intersects(BitCtx::IsConst.union(BitCtx::IsLet))
78+
&& had_init
79+
&& var.init.is_none()
80+
{
81+
unreachable!("const/let variable without initializer: {:#?}", var);
7882
}
7983
}
8084
}
@@ -202,7 +206,11 @@ impl Optimizer<'_> {
202206

203207
if v.ref_count == 0 && v.usage_count == 0 {
204208
if let Some(e) = init {
205-
if let Some(VarDeclKind::Const | VarDeclKind::Let) = self.ctx.var_kind {
209+
if self
210+
.ctx
211+
.bit_ctx
212+
.intersects(BitCtx::IsConst.union(BitCtx::IsLet))
213+
{
206214
if let Expr::Lit(Lit::Null(..)) = e {
207215
return;
208216
}
@@ -212,7 +220,11 @@ impl Optimizer<'_> {
212220
if let Some(ret) = ret {
213221
*e = ret;
214222
} else {
215-
if let Some(VarDeclKind::Const | VarDeclKind::Let) = self.ctx.var_kind {
223+
if self
224+
.ctx
225+
.bit_ctx
226+
.intersects(BitCtx::IsConst.union(BitCtx::IsLet))
227+
{
216228
*e = Null { span: DUMMY_SP }.into();
217229
} else {
218230
*e = Invalid { span: DUMMY_SP }.into();

0 commit comments

Comments
 (0)