Skip to content

Commit

Permalink
perf(es/transforms/compat): Migrate computed_props to VisitMut (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj authored Nov 24, 2021
1 parent d4ffce1 commit 1609580
Showing 1 changed file with 45 additions and 37 deletions.
82 changes: 45 additions & 37 deletions crates/swc_ecma_transforms_compat/src/es2015/computed_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use swc_common::{Mark, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper;
use swc_ecma_utils::{quote_ident, ExprFactory, StmtLike};
use swc_ecma_visit::{noop_fold_type, noop_visit_type, Fold, FoldWith, Node, Visit, VisitWith};
use swc_ecma_visit::{
as_folder, noop_visit_mut_type, noop_visit_type, Fold, Node, Visit, VisitMut, VisitMutWith,
VisitWith,
};

/// `@babel/plugin-transform-computed-properties`
///
Expand Down Expand Up @@ -37,7 +40,7 @@ use swc_ecma_visit::{noop_fold_type, noop_visit_type, Fold, FoldWith, Node, Visi
/// TODO(kdy1): cache reference like (_f = f, mutatorMap[_f].get = function(){})
/// instead of (mutatorMap[f].get = function(){}
pub fn computed_properties(c: Config) -> impl Fold {
ComputedProps { c }
as_folder(ComputedProps { c })
}

#[derive(Debug, Clone, Copy, Default, Deserialize)]
Expand All @@ -58,17 +61,16 @@ struct ObjectLitFolder {
c: Config,
}

/// TODO: VisitMut
impl Fold for ObjectLitFolder {
noop_fold_type!();
impl VisitMut for ObjectLitFolder {
noop_visit_mut_type!();

fn fold_expr(&mut self, expr: Expr) -> Expr {
let expr = expr.fold_children_with(self);
fn visit_mut_expr(&mut self, expr: &mut Expr) {
expr.visit_mut_children_with(self);

match expr {
Expr::Object(ObjectLit { mut props, span }) => {
if !is_complex(&props) {
return Expr::Object(ObjectLit { span, props });
Expr::Object(ObjectLit { props, span }) => {
if !is_complex(props) {
return;
}

let mark = Mark::fresh(Mark::root());
Expand Down Expand Up @@ -103,7 +105,9 @@ impl Fold for ObjectLitFolder {
}))
});

for prop in props {
let mut single_cnt_prop = None;

for prop in props.drain(..) {
let span = prop.span();

let ((key, is_compute), value) = match prop {
Expand Down Expand Up @@ -234,12 +238,13 @@ impl Fold for ObjectLitFolder {
};

if !self.c.loose && props_cnt == 1 {
return Expr::Call(CallExpr {
single_cnt_prop = Some(Expr::Call(CallExpr {
span,
callee: helper!(define_property, "defineProperty"),
args: vec![exprs.pop().unwrap().as_arg(), key.as_arg(), value.as_arg()],
type_args: Default::default(),
});
}));
break;
}
exprs.push(if self.c.loose {
let left = if is_compute {
Expand All @@ -263,8 +268,13 @@ impl Fold for ObjectLitFolder {
});
}

if let Some(single_expr) = single_cnt_prop {
*expr = single_expr;
return;
}

self.vars.push(VarDeclarator {
span,
span: *span,
name: Pat::Ident(obj_ident.clone().into()),
init: None,
definite: false,
Expand All @@ -280,7 +290,7 @@ impl Fold for ObjectLitFolder {
definite: false,
});
exprs.push(Box::new(Expr::Call(CallExpr {
span,
span: *span,
callee: helper!(define_enumerable_properties, "defineEnumerableProperties"),
args: vec![obj_ident.clone().as_arg(), mutator_map.as_arg()],
type_args: Default::default(),
Expand All @@ -289,14 +299,13 @@ impl Fold for ObjectLitFolder {

// Last value
exprs.push(Box::new(Expr::Ident(obj_ident)));
Expr::Seq(SeqExpr {
*expr = Expr::Seq(SeqExpr {
span: DUMMY_SP,
exprs,
})
});
}

_ => expr,
}
_ => {}
};
}
}

Expand All @@ -322,55 +331,54 @@ impl Visit for ComplexVisitor {
}
}

/// TODO: VisitMut
impl Fold for ComputedProps {
noop_fold_type!();
impl VisitMut for ComputedProps {
noop_visit_mut_type!();

fn fold_module_items(&mut self, n: Vec<ModuleItem>) -> Vec<ModuleItem> {
self.fold_stmt_like(n)
fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
self.visit_mut_stmt_like(n);
}

fn fold_stmts(&mut self, n: Vec<Stmt>) -> Vec<Stmt> {
self.fold_stmt_like(n)
fn visit_mut_stmts(&mut self, n: &mut Vec<Stmt>) {
self.visit_mut_stmt_like(n);
}
}

impl ComputedProps {
fn fold_stmt_like<T>(&mut self, stmts: Vec<T>) -> Vec<T>
fn visit_mut_stmt_like<T>(&mut self, stmts: &mut Vec<T>)
where
T: StmtLike + VisitWith<ShouldWork> + FoldWith<Self> + FoldWith<ObjectLitFolder>,
T: StmtLike + VisitWith<ShouldWork> + VisitMutWith<Self> + VisitMutWith<ObjectLitFolder>,
Vec<T>: VisitWith<ShouldWork>,
{
// let stmts = stmts.fold_children_with(self);
let mut buf = Vec::with_capacity(stmts.len());
let mut stmts_updated = Vec::with_capacity(stmts.len());

for stmt in stmts {
for mut stmt in stmts.drain(..) {
if !contains_computed_expr(&stmt) {
buf.push(stmt);
stmts_updated.push(stmt);
continue;
}

let mut folder = ObjectLitFolder {
c: self.c,
..Default::default()
};
let stmt = stmt.fold_with(&mut folder);

stmt.visit_mut_with(&mut folder);

// Add variable declaration
// e.g. var ref
if !folder.vars.is_empty() {
buf.push(T::from_stmt(Stmt::Decl(Decl::Var(VarDecl {
stmts_updated.push(T::from_stmt(Stmt::Decl(Decl::Var(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
decls: folder.vars,
declare: false,
}))));
}

buf.push(stmt);
stmts_updated.push(stmt);
}

buf
*stmts = stmts_updated;
}
}

Expand Down

1 comment on commit 1609580

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 1609580 Previous: d4ffce1 Ratio
base_tr_fixer 26972 ns/iter (± 3188) 27120 ns/iter (± 1532) 0.99
base_tr_resolver_and_hygiene 150709 ns/iter (± 31476) 150278 ns/iter (± 32712) 1.00
codegen_es2015 62206 ns/iter (± 3228) 59848 ns/iter (± 1857) 1.04
codegen_es2016 61518 ns/iter (± 2270) 60242 ns/iter (± 2154) 1.02
codegen_es2017 61679 ns/iter (± 5262) 60253 ns/iter (± 5716) 1.02
codegen_es2018 61455 ns/iter (± 5731) 61108 ns/iter (± 2579) 1.01
codegen_es2019 61519 ns/iter (± 3215) 61064 ns/iter (± 1742) 1.01
codegen_es2020 62069 ns/iter (± 3857) 60967 ns/iter (± 570) 1.02
codegen_es3 61155 ns/iter (± 6422) 61043 ns/iter (± 1736) 1.00
codegen_es5 62450 ns/iter (± 4397) 61427 ns/iter (± 474) 1.02
full_es2015 209181316 ns/iter (± 7702755) 210763304 ns/iter (± 15299914) 0.99
full_es2016 167475157 ns/iter (± 6368270) 167522245 ns/iter (± 8313948) 1.00
full_es2017 178037053 ns/iter (± 10270631) 176054795 ns/iter (± 10375517) 1.01
full_es2018 173935853 ns/iter (± 7575875) 180658012 ns/iter (± 19282826) 0.96
full_es2019 170563467 ns/iter (± 7985389) 171610603 ns/iter (± 10026351) 0.99
full_es2020 170857634 ns/iter (± 6798512) 175969065 ns/iter (± 19665541) 0.97
full_es3 240709637 ns/iter (± 19665805) 247322641 ns/iter (± 15598211) 0.97
full_es5 225929893 ns/iter (± 9940956) 223256609 ns/iter (± 12531213) 1.01
parser 758879 ns/iter (± 19881) 762163 ns/iter (± 21195) 1.00
ser_ast_node 177 ns/iter (± 17) 186 ns/iter (± 8) 0.95
ser_serde 190 ns/iter (± 4) 193 ns/iter (± 6) 0.98
emit_colors 20306597 ns/iter (± 25854869) 22309448 ns/iter (± 29034261) 0.91
emit_large 108260167 ns/iter (± 166030530) 110333708 ns/iter (± 167819288) 0.98
base_clone 2727146 ns/iter (± 63579) 2697770 ns/iter (± 259327) 1.01
fold_span 4658450 ns/iter (± 231008) 4522000 ns/iter (± 319882) 1.03
fold_span_panic 4884834 ns/iter (± 364153) 4983115 ns/iter (± 330300) 0.98
visit_mut_span 3387849 ns/iter (± 371448) 3319265 ns/iter (± 236863) 1.02
visit_mut_span_panic 3410391 ns/iter (± 195564) 3361580 ns/iter (± 219307) 1.01
ast_clone 19622 ns/iter (± 462) 19509 ns/iter (± 676) 1.01
ast_clone_to_stable 58804 ns/iter (± 5795) 58842 ns/iter (± 1832) 1.00
ast_clone_to_stable_then_to_unstable 108209 ns/iter (± 2556) 108010 ns/iter (± 7172) 1.00
json_deserialize 2321204 ns/iter (± 66970) 2314784 ns/iter (± 33793) 1.00
json_serialize 101944 ns/iter (± 1493) 101533 ns/iter (± 3757) 1.00
boxing_boxed 178 ns/iter (± 1) 170 ns/iter (± 1) 1.05
boxing_boxed_clone 81 ns/iter (± 3) 81 ns/iter (± 4) 1
boxing_unboxed 155 ns/iter (± 7) 154 ns/iter (± 3) 1.01
boxing_unboxed_clone 76 ns/iter (± 4) 98 ns/iter (± 0) 0.78
time_10 380 ns/iter (± 18) 375 ns/iter (± 15) 1.01
time_15 765 ns/iter (± 25) 885 ns/iter (± 33) 0.86
time_20 1377 ns/iter (± 104) 1403 ns/iter (± 20) 0.98
time_40 4800 ns/iter (± 154) 4820 ns/iter (± 83) 1.00
time_5 129 ns/iter (± 15) 122 ns/iter (± 5) 1.06
time_60 10266 ns/iter (± 297) 10209 ns/iter (± 555) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.