Skip to content

Commit

Permalink
fix(es/minifier): Check if object shorthand is skippable for seq inli…
Browse files Browse the repository at this point in the history
…ner (#8036)

**Related issue:**

 - Closes #7984
  • Loading branch information
Austaras authored Sep 30, 2023
1 parent c062536 commit 01391e3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
52 changes: 22 additions & 30 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1894,12 +1894,29 @@ impl Optimizer<'_> {
}
PropOrSpread::Prop(prop) => {
// Inline into key
let key = match &mut **prop {
let computed = match &mut **prop {
Prop::Shorthand(_) | Prop::Assign(_) => None,
Prop::KeyValue(prop) => prop.key.as_mut_computed(),
Prop::Getter(prop) => prop.key.as_mut_computed(),
Prop::Setter(prop) => prop.key.as_mut_computed(),
Prop::Method(prop) => prop.key.as_mut_computed(),
};

if let Some(computed) = computed {
if self.merge_sequential_expr(a, &mut computed.expr)? {
return Ok(true);
}

if !self.is_skippable_for_seq(Some(a), &computed.expr) {
return Ok(false);
}
}

match &mut **prop {
Prop::Shorthand(shorthand) => {
// We can't ignore shorthand properties
//
// https://github.com/swc-project/swc/issues/6914

let mut new_b = Box::new(Expr::Ident(shorthand.clone()));
if self.merge_sequential_expr(a, &mut new_b)? {
*prop = Box::new(Prop::KeyValue(KeyValueProp {
Expand All @@ -1908,40 +1925,15 @@ impl Optimizer<'_> {
shorthand.span.with_ctxt(SyntaxContext::empty()),
)
.into(),
value: new_b,
value: new_b.clone(),
}));
}

continue;
}
Prop::KeyValue(prop) => Some(&mut prop.key),
Prop::Assign(_) => None,
Prop::Getter(prop) => Some(&mut prop.key),
Prop::Setter(prop) => Some(&mut prop.key),
Prop::Method(prop) => Some(&mut prop.key),
};

if let Some(PropName::Computed(key)) = key {
if self.merge_sequential_expr(a, &mut key.expr)? {
return Ok(true);
}

if !self.is_skippable_for_seq(Some(a), &key.expr) {
return Ok(false);
}
}

match &mut **prop {
Prop::KeyValue(prop) => {
if self.merge_sequential_expr(a, &mut prop.value)? {
return Ok(true);
}

if !self.is_skippable_for_seq(Some(a), &prop.value) {
if !self.is_skippable_for_seq(Some(a), &new_b) {
return Ok(false);
}
}
Prop::Assign(prop) => {
Prop::KeyValue(prop) => {
if self.merge_sequential_expr(a, &mut prop.value)? {
return Ok(true);
}
Expand Down
17 changes: 17 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/7984/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
getInitialProps = (code) => {
let statusCode, message;

if (code) {
statusCode = code;
}

switch (statusCode) {
case 404:
message = "404";
break;
default:
message = "500";
}

return { statusCode, message };
};
7 changes: 7 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/7984/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
getInitialProps = (code)=>{
let statusCode, message;
return code && (statusCode = code), message = 404 === statusCode ? "404" : "500", {
statusCode,
message
};
};

1 comment on commit 01391e3

@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: 01391e3 Previous: 06b6eb9 Ratio
es/full/bugs-1 278685 ns/iter (± 8799) 281753 ns/iter (± 8214) 0.99
es/full/minify/libraries/antd 1306400999 ns/iter (± 7553557) 1310902953 ns/iter (± 6290217) 1.00
es/full/minify/libraries/d3 280564034 ns/iter (± 4398582) 276267250 ns/iter (± 4052925) 1.02
es/full/minify/libraries/echarts 1050579974 ns/iter (± 18955008) 1068183291 ns/iter (± 24378636) 0.98
es/full/minify/libraries/jquery 84228909 ns/iter (± 211767) 83842395 ns/iter (± 312468) 1.00
es/full/minify/libraries/lodash 97849488 ns/iter (± 386897) 97117168 ns/iter (± 457172) 1.01
es/full/minify/libraries/moment 50011759 ns/iter (± 77942) 50227921 ns/iter (± 141711) 1.00
es/full/minify/libraries/react 18061141 ns/iter (± 28242) 17986911 ns/iter (± 69693) 1.00
es/full/minify/libraries/terser 217781149 ns/iter (± 785750) 217090952 ns/iter (± 564132) 1.00
es/full/minify/libraries/three 384443615 ns/iter (± 2360071) 383859496 ns/iter (± 1302227) 1.00
es/full/minify/libraries/typescript 2638394815 ns/iter (± 10720442) 2642355444 ns/iter (± 16280606) 1.00
es/full/minify/libraries/victory 565654261 ns/iter (± 5060400) 562936170 ns/iter (± 8543489) 1.00
es/full/minify/libraries/vue 119507821 ns/iter (± 981115) 119196850 ns/iter (± 380357) 1.00
es/full/codegen/es3 34122 ns/iter (± 99) 34217 ns/iter (± 137) 1.00
es/full/codegen/es5 34013 ns/iter (± 390) 34228 ns/iter (± 45) 0.99
es/full/codegen/es2015 34157 ns/iter (± 86) 34376 ns/iter (± 1709) 0.99
es/full/codegen/es2016 34072 ns/iter (± 85) 34225 ns/iter (± 125) 1.00
es/full/codegen/es2017 33967 ns/iter (± 131) 34247 ns/iter (± 55) 0.99
es/full/codegen/es2018 34195 ns/iter (± 254) 34321 ns/iter (± 133) 1.00
es/full/codegen/es2019 34235 ns/iter (± 116) 34352 ns/iter (± 62) 1.00
es/full/codegen/es2020 34272 ns/iter (± 146) 34201 ns/iter (± 51) 1.00
es/full/all/es3 165339227 ns/iter (± 1567430) 165378325 ns/iter (± 495175) 1.00
es/full/all/es5 157435136 ns/iter (± 726426) 157742655 ns/iter (± 863676) 1.00
es/full/all/es2015 116426100 ns/iter (± 505324) 117999119 ns/iter (± 868427) 0.99
es/full/all/es2016 116052403 ns/iter (± 763786) 116922708 ns/iter (± 758007) 0.99
es/full/all/es2017 115111908 ns/iter (± 897604) 116568097 ns/iter (± 2323613) 0.99
es/full/all/es2018 113507113 ns/iter (± 676021) 114292795 ns/iter (± 612608) 0.99
es/full/all/es2019 112016010 ns/iter (± 880499) 113934104 ns/iter (± 696130) 0.98
es/full/all/es2020 108530499 ns/iter (± 361150) 109367898 ns/iter (± 353843) 0.99
es/full/parser 484037 ns/iter (± 5246) 491699 ns/iter (± 6408) 0.98
es/full/base/fixer 20314 ns/iter (± 191) 17924 ns/iter (± 65) 1.13
es/full/base/resolver_and_hygiene 82457 ns/iter (± 150) 80678 ns/iter (± 214) 1.02
serialization of serde 291 ns/iter (± 2) 287 ns/iter (± 0) 1.01

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

Please sign in to comment.