Skip to content

Commit

Permalink
feat(es/transforms/compat): Add single-property optimization to `dest…
Browse files Browse the repository at this point in the history
…ructuring` (#2511)

swc_ecma_transforms_compat:
 - `destructuring`: Convert single-property destructuring to direct field access.
  • Loading branch information
kdy1 authored Oct 22, 2021
1 parent e327c9a commit f33d321
Show file tree
Hide file tree
Showing 48 changed files with 312 additions and 274 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecmascript/transforms/compat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_compat"
repository = "https://github.com/swc-project/swc.git"
version = "0.45.1"
version = "0.45.2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
Expand Down
31 changes: 31 additions & 0 deletions ecmascript/transforms/compat/src/es2015/destructuring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,21 @@ impl AssignFolder {
"destructuring pattern binding requires initializer"
);

if props.len() == 1 {
match &props[0] {
ObjectPatProp::Assign(p @ AssignPatProp { value: None, .. }) => {
decls.push(VarDeclarator {
span: decl.span,
name: Pat::Ident(p.key.clone().into()),
init: Some(Box::new(decl.init.unwrap().make_member(p.key.clone()))),
definite: false,
});
return;
}
_ => {}
}
}

let can_be_null = can_be_null(decl.init.as_ref().unwrap());
let ref_ident = make_ref_ident(self.c, decls, decl.init);

Expand Down Expand Up @@ -713,6 +728,22 @@ impl Fold for AssignFolder {
})
}
Pat::Object(ObjectPat { span, props, .. }) => {
if props.len() == 1 {
match &props[0] {
ObjectPatProp::Assign(p @ AssignPatProp { value: None, .. }) => {
return Expr::Assign(AssignExpr {
span,
op: op!("="),
left: PatOrExpr::Pat(Box::new(Pat::Ident(
p.key.clone().into(),
))),
right: Box::new(right.make_member(p.key.clone())),
});
}
_ => {}
}
}

let ref_ident = make_ref_ident(self.c, &mut self.vars, None);

let mut exprs = vec![];
Expand Down
30 changes: 27 additions & 3 deletions ecmascript/transforms/compat/tests/es2015_destructuring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ test!(
var { x: { y } = {} } = z;"#,
r#"var z = {
};
var tmp = z.x, ref = tmp === void 0 ? {} : tmp, y = ref.y;"#
var tmp = z.x, y = (tmp === void 0 ? {} : tmp).y;"#
);

test!(
Expand Down Expand Up @@ -527,7 +527,7 @@ const bar = {
qux: 'baz'
}
};
const _Foo = bar[Foo], qux = _Foo.qux;"
const qux = bar[Foo].qux;"
);

test!(
Expand Down Expand Up @@ -1225,7 +1225,7 @@ var { x: { y } = {} } = z;
"#,
r#"
var z = {};
var tmp = z.x, ref = tmp === void 0 ? {} : tmp, y = ref.y;
var tmp = z.x, y = (tmp === void 0 ? {} : tmp).y;
"#
);

Expand Down Expand Up @@ -1607,3 +1607,27 @@ test!(
ref = b, ref1 = ref[0], a = ref1 === void 0 ? 1 : ref1, ref;
"
);

test!(
syntax(),
|_| tr(),
next_001,
"
const { NODE_ENV }= process.env;
",
"
const NODE_ENV = process.env.NODE_ENV;
"
);

test!(
syntax(),
|_| tr(),
next_002,
"
({ NODE_ENV }= process.env);
",
"
NODE_ENV = process.env.NODE_ENV;
"
);
2 changes: 1 addition & 1 deletion ecmascript/transforms/module/tests/common_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4282,7 +4282,7 @@ let SomeClass = function() {
_createClass(SomeClass, [{
key: 'call',
value: function call() {
var _props = this.props, myFunction = _props.myFunction;
var myFunction = this.props.myFunction;
if (myFunction) {
myFunction();
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/fixture/issue-2232/case1/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ var Foo = /*#__PURE__*/ function() {
var ref;
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref1, ref2;
var ref1;
ref1 = _param, ref = ref1.input, toNumber = ref.toNumber, messageBody = ref.messageBody, ref, ref1, all = _objectWithoutProperties(_param.input, [
"toNumber",
"messageBody"
]), ref2 = _param1, dataSources = ref2.dataSources, ref2;
]), dataSources = _param1.dataSources;
case 1:
case "end":
return _ctx.stop();
Expand Down
4 changes: 2 additions & 2 deletions tests/fixture/issue-2232/case2/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ var resolver = {
var ref;
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref1, ref2;
var ref1;
ref1 = _param, ref = ref1.input, toNumber = ref.toNumber, messageBody = ref.messageBody, ref, ref1, all = _objectWithoutProperties(_param.input, [
"toNumber",
"messageBody"
]), ref2 = _param1, dataSources = ref2.dataSources, ref2;
]), dataSources = _param1.dataSources;
case 1:
case "end":
return _ctx.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ function _fn5() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref;
ref = y, x = ref.x, ref;
x = y.x;
case 1:
case "end":
return _ctx.stop();
Expand Down Expand Up @@ -225,11 +224,10 @@ function _fn9() {
_fn9 = _asyncToGenerator(regeneratorRuntime.mark(function _callee(x) {
var x;
return regeneratorRuntime.wrap(function _callee$(_ctx) {
var ref;
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref2;
ref2 = y, ref = ref2.z, x = ref.x, ref, ref2;
var ref;
ref = y, x = ref.z.x, ref;
case 1:
case "end":
return _ctx.stop();
Expand All @@ -248,9 +246,8 @@ function _fn10() {
var ref;
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref3;
var ref4;
ref3 = y, ref = y, ref4 = ref, x = ref4.x, ref4, ref3;
var ref2;
ref2 = y, ref = y, x = ref.x, ref2;
case 1:
case "end":
return _ctx.stop();
Expand Down Expand Up @@ -306,8 +303,8 @@ function _fn13() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref, ref5;
ref = y, ref5 = ref[0], x = ref5 === void 0 ? y : ref5, ref;
var ref, ref3;
ref = y, ref3 = ref[0], x = ref3 === void 0 ? y : ref3, ref;
case 1:
case "end":
return _ctx.stop();
Expand Down Expand Up @@ -363,8 +360,8 @@ function _fn16() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref, ref6;
ref = y, ref6 = ref[0], x = ref6[0], ref6, ref;
var ref, ref4;
ref = y, ref4 = ref[0], x = ref4[0], ref4, ref;
case 1:
case "end":
return _ctx.stop();
Expand All @@ -382,8 +379,8 @@ function _fn17() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref, ref7, ref8;
ref = y, ref7 = ref[0], ref8 = ref7 === void 0 ? y : ref7, x = ref8[0], ref8, ref;
var ref, ref5, ref6;
ref = y, ref5 = ref[0], ref6 = ref5 === void 0 ? y : ref5, x = ref6[0], ref6, ref;
case 1:
case "end":
return _ctx.stop();
Expand Down Expand Up @@ -665,8 +662,7 @@ function _fn31() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
var ref;
for(ref = y, x = ref.x, ref;;){
for(x = y.x;;){
}
case 1:
case "end":
Expand Down Expand Up @@ -806,8 +802,7 @@ function _fn36() {
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
_ctx.prev = 1;
for(_iterator = y[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
var ref;
ref = _step.value, x = ref.x, ref;
x = _step.value.x;
}
_ctx.next = 9;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var p6 = function(param) {
var a = param.a;
};
var p7 = function(param) {
var _a = param.a, b = _a.b;
var b = param.a.b;
};
var p8 = function(param) {
var _a = param.a, a = _a === void 0 ? 1 : _a;
Expand Down
Loading

1 comment on commit f33d321

@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: f33d321 Previous: e327c9a Ratio
base_tr_fixer 31676 ns/iter (± 5370) 30664 ns/iter (± 3181) 1.03
base_tr_resolver_and_hygiene 171634 ns/iter (± 23594) 175395 ns/iter (± 21440) 0.98
codegen_es2015 62828 ns/iter (± 5820) 62762 ns/iter (± 22199) 1.00
codegen_es2016 62652 ns/iter (± 9624) 62414 ns/iter (± 6397) 1.00
codegen_es2017 64733 ns/iter (± 7475) 61669 ns/iter (± 14953) 1.05
codegen_es2018 62643 ns/iter (± 8940) 61581 ns/iter (± 4883) 1.02
codegen_es2019 61573 ns/iter (± 9246) 61400 ns/iter (± 3837) 1.00
codegen_es2020 60953 ns/iter (± 10901) 61305 ns/iter (± 7175) 0.99
codegen_es3 61203 ns/iter (± 11083) 61259 ns/iter (± 5648) 1.00
codegen_es5 61767 ns/iter (± 9773) 61454 ns/iter (± 8700) 1.01
config_for_file 19050 ns/iter (± 3906) 18085 ns/iter (± 5121) 1.05
full_es2015 216408027 ns/iter (± 19420147) 209429724 ns/iter (± 12943913) 1.03
full_es2016 163013970 ns/iter (± 12758709) 157321498 ns/iter (± 11223776) 1.04
full_es2017 172199813 ns/iter (± 17233135) 166273658 ns/iter (± 15302231) 1.04
full_es2018 168949755 ns/iter (± 14651501) 167279206 ns/iter (± 17389380) 1.01
full_es2019 167035114 ns/iter (± 18288433) 163095187 ns/iter (± 10926797) 1.02
full_es2020 166167664 ns/iter (± 15555341) 161481790 ns/iter (± 15070498) 1.03
full_es3 254770749 ns/iter (± 33038767) 251576958 ns/iter (± 32815635) 1.01
full_es5 232710006 ns/iter (± 18517313) 228907531 ns/iter (± 14196295) 1.02
parser 812122 ns/iter (± 132650) 804316 ns/iter (± 111915) 1.01
transforms_es2015 1237374 ns/iter (± 256839) 1194209 ns/iter (± 224498) 1.04
transforms_es2016 752016 ns/iter (± 154092) 697344 ns/iter (± 126335) 1.08
transforms_es2017 719007 ns/iter (± 141618) 665195 ns/iter (± 163954) 1.08
transforms_es2018 714726 ns/iter (± 125514) 663485 ns/iter (± 189241) 1.08
transforms_es2019 718393 ns/iter (± 117916) 671775 ns/iter (± 289750) 1.07
transforms_es2020 731562 ns/iter (± 214444) 656121 ns/iter (± 143495) 1.11
transforms_es3 1404536 ns/iter (± 367589) 1364916 ns/iter (± 274928) 1.03
transforms_es5 1244445 ns/iter (± 309808) 1187716 ns/iter (± 163447) 1.05
ser_ast_node 175 ns/iter (± 20) 171 ns/iter (± 20) 1.02
ser_serde 188 ns/iter (± 43) 186 ns/iter (± 11) 1.01
emit_colors 19919789 ns/iter (± 26966052) 14436354 ns/iter (± 20378974) 1.38
emit_large 74772352 ns/iter (± 95577845) 90841699 ns/iter (± 137132058) 0.82
base_clone 2849249 ns/iter (± 1248604) 2724275 ns/iter (± 320749) 1.05
fold_span 4557403 ns/iter (± 1147269) 4495586 ns/iter (± 482528) 1.01
fold_span_panic 4954531 ns/iter (± 1941421) 4870329 ns/iter (± 816678) 1.02
visit_mut_span 3225550 ns/iter (± 654481) 3258276 ns/iter (± 288569) 0.99
visit_mut_span_panic 3379009 ns/iter (± 1911467) 3297394 ns/iter (± 530335) 1.02
boxing_boxed 176 ns/iter (± 26) 168 ns/iter (± 39) 1.05
boxing_boxed_clone 80 ns/iter (± 17) 77 ns/iter (± 6) 1.04
boxing_unboxed 145 ns/iter (± 32) 149 ns/iter (± 20) 0.97
boxing_unboxed_clone 93 ns/iter (± 15) 97 ns/iter (± 10) 0.96

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

Please sign in to comment.