Skip to content

Commit 126d432

Browse files
authored
perf(es/minifier): Use bitflags to reduce context size of InfectionCollector (#10387)
Related: #10379 Context size 3 bytes -> 1 byte
1 parent fd52c5c commit 126d432

File tree

2 files changed

+44
-92
lines changed

2 files changed

+44
-92
lines changed

crates/swc_ecma_usage_analyzer/src/alias/ctx.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::ops::{Deref, DerefMut};
22

3+
use bitflags::bitflags;
4+
35
use super::InfectionCollector;
46

57
impl InfectionCollector {
@@ -14,11 +16,13 @@ impl InfectionCollector {
1416
}
1517
}
1618

17-
#[derive(Debug, Default, Clone, Copy)]
18-
pub(crate) struct Ctx {
19-
pub track_expr_ident: bool,
20-
pub is_callee: bool,
21-
pub is_pat_decl: bool,
19+
bitflags! {
20+
#[derive(Debug, Default, Clone, Copy)]
21+
pub struct Ctx: u8 {
22+
const TrackExprIdent = 1 << 0;
23+
const IsCallee = 1 << 1;
24+
const IsPatDecl = 1 << 2;
25+
}
2226
}
2327

2428
pub(super) struct WithCtx<'a> {

crates/swc_ecma_usage_analyzer/src/alias/mod.rs

Lines changed: 35 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct AliasConfig {
2626
/// on.
2727
pub ignore_named_child_scope: bool,
2828
}
29+
2930
impl AliasConfig {
3031
pub fn marks(mut self, arg: Option<Marks>) -> Self {
3132
self.marks = arg;
@@ -98,10 +99,7 @@ where
9899
config,
99100
unresolved_ctxt,
100101

101-
ctx: Ctx {
102-
track_expr_ident: true,
103-
..Default::default()
104-
},
102+
ctx: Ctx::TrackExprIdent,
105103

106104
bindings: FxHashSet::default(),
107105
accesses: FxHashSet::default(),
@@ -138,10 +136,7 @@ where
138136
config,
139137
unresolved_ctxt,
140138

141-
ctx: Ctx {
142-
track_expr_ident: true,
143-
..Default::default()
144-
},
139+
ctx: Ctx::TrackExprIdent,
145140

146141
bindings: FxHashSet::default(),
147142
accesses: FxHashSet::default(),
@@ -191,7 +186,7 @@ impl InfectionCollector {
191186

192187
self.accesses.insert((
193188
e,
194-
if self.ctx.is_callee {
189+
if self.ctx.contains(Ctx::IsCallee) {
195190
AccessKind::Call
196191
} else {
197192
AccessKind::Reference
@@ -204,15 +199,15 @@ impl Visit for InfectionCollector {
204199
noop_visit_type!();
205200

206201
fn visit_arrow_expr(&mut self, n: &ArrowExpr) {
207-
let old = self.ctx.is_pat_decl;
202+
let old = self.ctx.contains(Ctx::IsPatDecl);
208203

209204
for p in &n.params {
210-
self.ctx.is_pat_decl = true;
205+
self.ctx.insert(Ctx::IsPatDecl);
211206
p.visit_with(self);
212207
}
213208

214209
n.body.visit_with(self);
215-
self.ctx.is_pat_decl = old;
210+
self.ctx.set(Ctx::IsPatDecl, old);
216211
}
217212

218213
fn visit_assign_expr(&mut self, n: &AssignExpr) {
@@ -230,7 +225,7 @@ impl Visit for InfectionCollector {
230225
fn visit_assign_pat_prop(&mut self, node: &AssignPatProp) {
231226
node.value.visit_with(self);
232227

233-
if self.ctx.is_pat_decl {
228+
if self.ctx.contains(Ctx::IsPatDecl) {
234229
self.add_binding(&node.key.clone().into());
235230
}
236231
}
@@ -258,29 +253,18 @@ impl Visit for InfectionCollector {
258253
| op!("<<")
259254
| op!(">>")
260255
| op!(">>>") => {
261-
let ctx = Ctx {
262-
track_expr_ident: false,
263-
is_callee: false,
264-
..self.ctx
265-
};
256+
let ctx = self.ctx - Ctx::TrackExprIdent - Ctx::IsCallee;
266257
e.visit_children_with(&mut *self.with_ctx(ctx));
267258
}
268259
_ => {
269-
let ctx = Ctx {
270-
track_expr_ident: true,
271-
is_callee: false,
272-
..self.ctx
273-
};
260+
let ctx = (self.ctx | Ctx::TrackExprIdent) - Ctx::IsCallee;
274261
e.visit_children_with(&mut *self.with_ctx(ctx));
275262
}
276263
}
277264
}
278265

279266
fn visit_callee(&mut self, n: &Callee) {
280-
let ctx = Ctx {
281-
is_callee: true,
282-
..self.ctx
283-
};
267+
let ctx = self.ctx | Ctx::IsCallee;
284268
n.visit_children_with(&mut *self.with_ctx(ctx));
285269
}
286270

@@ -292,19 +276,12 @@ impl Visit for InfectionCollector {
292276

293277
fn visit_cond_expr(&mut self, e: &CondExpr) {
294278
{
295-
let ctx = Ctx {
296-
track_expr_ident: false,
297-
is_callee: false,
298-
..self.ctx
299-
};
279+
let ctx = self.ctx - Ctx::TrackExprIdent - Ctx::IsCallee;
300280
e.test.visit_with(&mut *self.with_ctx(ctx));
301281
}
302282

303283
{
304-
let ctx = Ctx {
305-
track_expr_ident: true,
306-
..self.ctx
307-
};
284+
let ctx = self.ctx | Ctx::TrackExprIdent;
308285
e.cons.visit_with(&mut *self.with_ctx(ctx));
309286
e.alt.visit_with(&mut *self.with_ctx(ctx));
310287
}
@@ -319,17 +296,13 @@ impl Visit for InfectionCollector {
319296

320297
match e {
321298
Expr::Ident(i) => {
322-
if self.ctx.track_expr_ident {
299+
if self.ctx.contains(Ctx::TrackExprIdent) {
323300
self.add_usage(i.to_id());
324301
}
325302
}
326303

327304
_ => {
328-
let ctx = Ctx {
329-
track_expr_ident: true,
330-
is_pat_decl: false,
331-
..self.ctx
332-
};
305+
let ctx = (self.ctx | Ctx::TrackExprIdent) - Ctx::IsPatDecl;
333306
e.visit_children_with(&mut *self.with_ctx(ctx));
334307
}
335308
}
@@ -368,42 +341,35 @@ impl Visit for InfectionCollector {
368341

369342
fn visit_member_expr(&mut self, n: &MemberExpr) {
370343
{
371-
let ctx = Ctx {
372-
track_expr_ident: self.config.need_all,
373-
..self.ctx
374-
};
344+
let mut ctx = self.ctx;
345+
ctx.set(Ctx::TrackExprIdent, self.config.need_all);
375346
n.obj.visit_with(&mut *self.with_ctx(ctx));
376347
}
377348

378349
{
379-
let ctx = Ctx {
380-
track_expr_ident: self.config.need_all,
381-
..self.ctx
382-
};
350+
let mut ctx = self.ctx;
351+
ctx.set(Ctx::TrackExprIdent, self.config.need_all);
383352
n.prop.visit_with(&mut *self.with_ctx(ctx));
384353
}
385354
}
386355

387356
fn visit_member_prop(&mut self, n: &MemberProp) {
388357
if let MemberProp::Computed(c) = &n {
389-
c.visit_with(&mut *self.with_ctx(Ctx {
390-
is_callee: false,
391-
..self.ctx
392-
}));
358+
c.visit_with(&mut *self.with_ctx(self.ctx - Ctx::IsCallee));
393359
}
394360
}
395361

396362
fn visit_param(&mut self, node: &Param) {
397-
let old = self.ctx.is_pat_decl;
398-
self.ctx.is_pat_decl = true;
363+
let old = self.ctx.contains(Ctx::IsPatDecl);
364+
self.ctx.insert(Ctx::IsPatDecl);
399365
node.visit_children_with(self);
400-
self.ctx.is_pat_decl = old;
366+
self.ctx.set(Ctx::IsPatDecl, old);
401367
}
402368

403369
fn visit_pat(&mut self, node: &Pat) {
404370
node.visit_children_with(self);
405371

406-
if self.ctx.is_pat_decl {
372+
if self.ctx.contains(Ctx::IsPatDecl) {
407373
if let Pat::Ident(i) = node {
408374
self.add_binding(i)
409375
}
@@ -412,10 +378,7 @@ impl Visit for InfectionCollector {
412378

413379
fn visit_prop_name(&mut self, n: &PropName) {
414380
if let PropName::Computed(c) = &n {
415-
c.visit_with(&mut *self.with_ctx(Ctx {
416-
is_callee: false,
417-
..self.ctx
418-
}));
381+
c.visit_with(&mut *self.with_ctx(self.ctx - Ctx::IsCallee));
419382
}
420383
}
421384

@@ -431,10 +394,7 @@ impl Visit for InfectionCollector {
431394

432395
fn visit_super_prop_expr(&mut self, n: &SuperPropExpr) {
433396
if let SuperProp::Computed(c) = &n.prop {
434-
c.visit_with(&mut *self.with_ctx(Ctx {
435-
is_callee: false,
436-
..self.ctx
437-
}));
397+
c.visit_with(&mut *self.with_ctx(self.ctx - Ctx::IsCallee));
438398
}
439399
}
440400

@@ -446,40 +406,28 @@ impl Visit for InfectionCollector {
446406
| op!("!")
447407
| op!("typeof")
448408
| op!("void") => {
449-
let ctx = Ctx {
450-
track_expr_ident: false,
451-
is_callee: false,
452-
..self.ctx
453-
};
409+
let ctx = self.ctx - Ctx::TrackExprIdent - Ctx::IsCallee;
454410
e.visit_children_with(&mut *self.with_ctx(ctx));
455411
}
456412

457413
_ => {
458-
let ctx = Ctx {
459-
track_expr_ident: true,
460-
is_callee: false,
461-
..self.ctx
462-
};
414+
let ctx = (self.ctx | Ctx::TrackExprIdent) - Ctx::IsCallee;
463415
e.visit_children_with(&mut *self.with_ctx(ctx));
464416
}
465417
}
466418
}
467419

468420
fn visit_update_expr(&mut self, e: &UpdateExpr) {
469-
let ctx = Ctx {
470-
track_expr_ident: false,
471-
is_callee: false,
472-
..self.ctx
473-
};
421+
let ctx = self.ctx - Ctx::TrackExprIdent - Ctx::IsCallee;
474422
e.arg.visit_with(&mut *self.with_ctx(ctx));
475423
}
476424

477425
fn visit_var_declarator(&mut self, n: &VarDeclarator) {
478426
{
479-
let old = self.ctx.is_pat_decl;
480-
self.ctx.is_pat_decl = true;
427+
let old = self.ctx.contains(Ctx::IsPatDecl);
428+
self.ctx.insert(Ctx::IsPatDecl);
481429
n.name.visit_with(self);
482-
self.ctx.is_pat_decl = old;
430+
self.ctx.set(Ctx::IsPatDecl, old);
483431
}
484432

485433
if self.config.ignore_named_child_scope {
@@ -489,10 +437,10 @@ impl Visit for InfectionCollector {
489437
}
490438

491439
{
492-
let old = self.ctx.is_pat_decl;
493-
self.ctx.is_pat_decl = false;
440+
let old = self.ctx.contains(Ctx::IsPatDecl);
441+
self.ctx.remove(Ctx::IsPatDecl);
494442
n.init.visit_with(self);
495-
self.ctx.is_pat_decl = old;
443+
self.ctx.set(Ctx::IsPatDecl, old);
496444
}
497445
}
498446
}

0 commit comments

Comments
 (0)