Skip to content

Commit

Permalink
feat(es/minifier): Mark more expressions as pure (#6204)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Oct 20, 2022
1 parent d9ef284 commit cc848db
Show file tree
Hide file tree
Showing 58 changed files with 47 additions and 106 deletions.
2 changes: 1 addition & 1 deletion crates/swc/tests/tsc-references/asOperator3.2.minified.js
Expand Up @@ -17,4 +17,4 @@ function _templateObject1() {
return data;
}, data;
}
"".concat(579), "leading ".concat(579), "".concat(579, " trailing"), "Hello ".concat(123, " World"), "".concat(1, " end of string"), tag(_templateObject(), 123), tag(_templateObject1());
tag(_templateObject(), 123), tag(_templateObject1());
@@ -1,3 +1,3 @@
//// [commaOperatorWithSecondOperandAnyType.ts]
var NUMBER, x;
++NUMBER, "string".charAt(0), x("any"), x.doSomeThing(), ++NUMBER, "string".charAt(0), x("any"), x.doSomeThing();
++NUMBER, x("any"), x.doSomeThing(), ++NUMBER, x("any"), x.doSomeThing();
@@ -1,7 +1,2 @@
//// [computedPropertyNames12_ES5.ts]
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
var n, a, _ref = (void 0) + n;
(function C() {
"use strict";
_class_call_check(this, C), this[n] = n, this[_ref] = 2, this["hello bye"] = 0;
})["hello ".concat(a, " bye")] = 0;
@@ -1,4 +1,2 @@
//// [computedPropertyNames13_ES5.ts]
var a;
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
"hello ".concat(a, " bye");
@@ -1,5 +1,3 @@
//// [computedPropertyNames16_ES5.ts]
var a;
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
import _create_class from "@swc/helpers/src/_create_class.mjs";
"hello ".concat(a, " bye");

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

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

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

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

@@ -1,4 +1,3 @@
//// [exponentiationOperatorWithTemplateStringInvalid.ts]
"".concat(3), "2".concat(3), "".concat(3, "4"), "2".concat(3, "4"), "".concat(3), "2".concat(3), "".concat(3, "4"), "2".concat(3, "4");
var k = 10;
k = Math.pow(k, "".concat(3)), k = Math.pow(k, "2".concat(3)), k = Math.pow(k, "2".concat(3, "4")), k = Math.pow(k, "2".concat(3, "4"));
@@ -1,2 +1,2 @@
//// [stringPropertyAccess.ts]
"".charAt(0), "".hasOwnProperty("charAt"), "".charAt(0), "".hasOwnProperty("toFixed");
"".hasOwnProperty("charAt"), "".hasOwnProperty("toFixed");
@@ -1,2 +1 @@
//// [stringPropertyAccessWithError.ts]
"".charAt("invalid");
@@ -1,2 +1,2 @@
//// [templateLiteralTypes2.ts]
takesLiteral("foo.bar.baz"), takesLiteral("foo.bar.baz"), takesLiteral("foo.bar.".concat(someString)), takesLiteral("foo.bar.".concat(someString)), takesLiteral("foo.bar.".concat(someUnion)), "".concat(22, "px"), "rotate(".concat(12, "dig)");
takesLiteral("foo.bar.baz"), takesLiteral("foo.bar.baz"), takesLiteral("foo.bar.".concat(someString)), takesLiteral("foo.bar.".concat(someString)), takesLiteral("foo.bar.".concat(someUnion));
@@ -1,5 +1,5 @@
//// [templateLiteralTypes3.ts]
"".concat("abc", " abc"), "".concat("abc", " abc"), chain("a"), noSpread([
chain("a"), noSpread([
"1.2.3",
"1.2.4"
]), noSpread([
Expand Down
@@ -1,2 +1 @@
//// [templateStringBinaryOperations.ts]
"".concat(3), "2".concat(3), "".concat(3, "4"), "2".concat(3, "4"), "".concat(3), "2".concat(3), "".concat(3, "4"), "2".concat(3, "4"), "".concat(3), "2".concat(3), "".concat(3, "4"), "2".concat(3, "4"), "".concat(-1), "2".concat(-1), "".concat(-1, "5"), "2".concat(-1, "5"), "".concat(-1), "2".concat(-1), "".concat(-1, "5"), "2".concat(-1, "5"), "".concat(-1), "2".concat(-1), "".concat(-1, "5"), "2".concat(-1, "5"), "".concat(12), "2".concat(12), "".concat(12, "5"), "2".concat(12, "5"), "".concat(12), "2".concat(12), "".concat(12, "5"), "2".concat(12, "5"), "".concat(12), "2".concat(12), "".concat(12, "5"), "2".concat(12, "5"), "".concat(0), "2".concat(0), "".concat(0, "5"), "2".concat(0, "5"), "".concat(0), "2".concat(0), "".concat(0, "5"), "2".concat(0, "5"), "".concat(0), "2".concat(0), "".concat(0, "5"), "2".concat(0, "5");
@@ -1,2 +1 @@
//// [templateStringBinaryOperationsInvalid.ts]
"".concat(3), "2".concat(3), "".concat(3, "4"), "2".concat(3, "4"), "".concat(3), "2".concat(3), "".concat(3, "4"), "2".concat(3, "4"), "".concat(3), "2".concat(3), "".concat(3, "4"), "2".concat(3, "4"), "".concat(3), "2".concat(3), "".concat(3, "4"), "2".concat(3, "4"), "".concat(3), "2".concat(3), "".concat(3, "4"), "2".concat(3, "4"), "".concat(3), "2".concat(3), "".concat(3, "4"), "2".concat(3, "4"), "".concat(-1), "2".concat(-1), "".concat(-1, "5"), "2".concat(-1, "5"), "".concat(-1), "2".concat(-1), "".concat(-1, "5"), "2".concat(-1, "5"), "".concat(12), "2".concat(12), "".concat(12, "5"), "2".concat(12, "5"), "".concat(12), "2".concat(12), "".concat(12, "5"), "2".concat(12, "5"), "".concat(0), "2".concat(0), "".concat(0, "5"), "2".concat(0, "5"), "".concat(0), "2".concat(0), "".concat(0, "5"), "2".concat(0, "5"), "".concat(-1), "2".concat(-1), "".concat(-1, "5"), "2".concat(-1, "5"), "".concat(-1), "2".concat(-1), "".concat(-1, "5"), "2".concat(-1, "5"), "".concat(12), "2".concat(12), "".concat(12, "5"), "2".concat(12, "5"), "".concat(12), "2".concat(12), "".concat(12, "5"), "2".concat(12, "5"), "".concat(0), "2".concat(0), "".concat(0, "5"), "2".concat(0, "5"), "".concat(0), "2".concat(0), "".concat(0, "5"), "2".concat(0, "5"), "".concat(-1), "2".concat(-1), "".concat(-1, "5"), "2".concat(-1, "5"), "".concat(-1), "2".concat(-1), "".concat(-1, "5"), "2".concat(-1, "5"), "".concat(12), "2".concat(12), "".concat(12, "5"), "2".concat(12, "5"), "".concat(12), "2".concat(12), "".concat(12, "5"), "2".concat(12, "5"), "".concat(0), "2".concat(0), "".concat(0, "5"), "2".concat(0, "5"), "".concat(0), "2".concat(0), "".concat(0, "5"), "2".concat(0, "5");
@@ -1,2 +1 @@
//// [templateStringInArray.ts]
"abc".concat(123, "def");
@@ -1,2 +1 @@
//// [templateStringInDivision.ts]
"abc".concat(1, "def");
@@ -1,2 +1 @@
//// [templateStringInEqualityChecks.ts]
"abc" === "abc".concat(0, "abc") || "abc" !== "abc".concat(0, "abc") && "abc0abc" == "abc".concat(0, "abc") && "abc".concat(0, "abc");
@@ -1,2 +1 @@
//// [templateStringInModulo.ts]
"abc".concat(1, "def");
@@ -1,2 +1 @@
//// [templateStringInMultiplication.ts]
"abc".concat(1, "def");
@@ -1,2 +1 @@
//// [templateStringInParentheses.ts]
"abc".concat(0, "abc");
@@ -1,2 +1 @@
//// [templateStringInPropertyAssignment.ts]
"abc".concat(123, "def", 456, "ghi");
@@ -1,7 +1 @@
//// [templateStringInSwitchAndCase.ts]
switch("abc".concat(0, "abc")){
case "abc":
case "123":
case "abc".concat(0, "abc"):
"def".concat(1, "def");
}
@@ -1,2 +1 @@
//// [templateStringInTypeAssertion.ts]
"abc".concat(123, "def");
@@ -1,2 +1 @@
//// [templateStringInUnaryPlus.ts]
"abc".concat(123, "def");
@@ -1,2 +1,2 @@
//// [templateStringInWhile.ts]
for(; "abc".concat(0, "abc");)"def".concat(1, "def");
for(; "abc".concat(0, "abc"););
@@ -1,2 +1 @@
//// [templateStringWithEmbeddedAddition.ts]
"abc".concat(20, "def");
@@ -1,6 +1 @@
//// [templateStringWithEmbeddedArray.ts]
"abc".concat([
1,
2,
3
], "def");
@@ -1,4 +1 @@
//// [templateStringWithEmbeddedArrowFunction.ts]
"abc".concat(function(x) {
return x;
}, "def");
@@ -1,2 +1 @@
//// [templateStringWithEmbeddedComments.ts]
"head".concat(10, "\nmiddle", 20, "\ntail");
@@ -1,2 +1 @@
//// [templateStringWithEmbeddedConditional.ts]
"abc".concat(!1, "def");
@@ -1,2 +1 @@
//// [templateStringWithEmbeddedDivision.ts]
"abc".concat(1, "def");
@@ -1,4 +1 @@
//// [templateStringWithEmbeddedFunctionExpression.ts]
"abc".concat(function y() {
return y;
}, "def");
@@ -1,5 +1 @@
//// [templateStringWithEmbeddedInOperator.ts]
"abc".concat("hi" in {
hi: 10,
hello: 20
}, "def");
@@ -1,3 +1,3 @@
//// [templateStringWithEmbeddedInstanceOf.ts]
import _instanceof from "@swc/helpers/src/_instanceof.mjs";
"abc".concat(_instanceof("hello", String), "def");
_instanceof("hello", String);
@@ -1,2 +1 @@
//// [templateStringWithEmbeddedModulo.ts]
"abc".concat(0, "def");
@@ -1,2 +1 @@
//// [templateStringWithEmbeddedMultiplication.ts]
"abc".concat(42, "def");
@@ -1,2 +1 @@
//// [templateStringWithEmbeddedNewOperator.ts]
"abc".concat(new String("Hi"), "def");
@@ -1,2 +1 @@
//// [templateStringWithEmbeddedNewOperatorES6.ts]
new String("Hi");
@@ -1,5 +1 @@
//// [templateStringWithEmbeddedObjectLiteral.ts]
"abc".concat({
x: 10,
y: 20
}, "def");
@@ -1,2 +1 @@
//// [templateStringWithEmbeddedTypeAssertionOnAddition.ts]
"abc".concat(20, "def");
@@ -1,3 +1,3 @@
//// [templateStringWithEmbeddedTypeOfOperator.ts]
import _type_of from "@swc/helpers/src/_type_of.mjs";
"abc".concat(_type_of("hi"), "def");
_type_of("hi");
@@ -1,2 +1 @@
//// [templateStringWithEmbeddedUnaryPlus.ts]
"abc".concat(Infinity, "def");
@@ -1,2 +1 @@
//// [templateStringWithEmptyLiteralPortions.ts]
"".concat(0), "1".concat(0), "".concat(0, "2"), "1".concat(0, "2"), "".concat(0, 0), "1".concat(0, 0), "".concat(0, "2", 0), "1".concat(0, "2", 0), "".concat(0, 0, "3"), "1".concat(0, 0, "3"), "".concat(0, "2", 0, "3"), "1".concat(0, "2", 0, "3");
@@ -1,2 +1 @@
//// [templateStringWithOpenCommentInStringPortion.ts]
" /**head ".concat(10, " // still middle ", 20, " /* still tail ");
@@ -1,2 +1 @@
//// [templateStringWithPropertyAccessES6.ts]
"abc0abc".indexOf("abc");
@@ -1,2 +1 @@
//// [templateStringsWithTypeErrorInFunctionExpressionsInSubstitutionExpression.ts]
"".concat(function(x) {});
1 change: 0 additions & 1 deletion crates/swc/tests/vercel/full/utf8-2/output/index.js
@@ -1 +0,0 @@
"".concat(100, "testing");
Expand Up @@ -1823,8 +1823,6 @@ function copyBytes(src, dst, off = 0) {
return src.byteLength;
}
const DEFAULT_BUF_SIZE = 4096;
"\r".charCodeAt(0);
"\n".charCodeAt(0);
class AbstractBufBase {
usedBufferBytes = 0;
err = null;
Expand Down
2 changes: 0 additions & 2 deletions crates/swc_bundler/tests/fixture/deno-9591/output/entry.ts
Expand Up @@ -1833,8 +1833,6 @@ function copyBytes(src, dst, off = 0) {
return src.byteLength;
}
const DEFAULT_BUF_SIZE = 4096;
"\r".charCodeAt(0);
"\n".charCodeAt(0);
class AbstractBufBase {
usedBufferBytes = 0;
err = null;
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/scripts/test.sh
Expand Up @@ -6,6 +6,6 @@ export UPDATE=1
export SWC_CHECK=0
export SWC_RUN=0

cargo test -q -p swc_ecma_minifier --release -p swc --no-fail-fast --test projects --test tsc --test compress --test mangle --features concurrent $@
cargo test -q -p swc_ecma_minifier -p swc --no-fail-fast --test projects --test tsc --test compress --test mangle --features concurrent $@

# find ../swc/tests/ -type f -empty -delete
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/iife.rs
Expand Up @@ -301,7 +301,7 @@ where
/// If a parameter is not used, we can ignore return value of the
/// corresponding argument.
pub(super) fn ignore_unused_args_of_iife(&mut self, e: &mut CallExpr) {
if !self.options.unused {
if !self.options.unused && !self.options.reduce_vars {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion crates/swc_ecma_minifier/src/compress/pure/misc.rs
Expand Up @@ -1096,7 +1096,9 @@ impl Pure<'_> {
Expr::New(NewExpr { callee, args, .. })
if callee.is_one_of_global_ref_to(
&self.expr_ctx,
&["Map", "Set", "Array", "Object", "Boolean", "Number"],
&[
"Map", "Set", "Array", "Object", "Boolean", "Number", "String",
],
) =>
{
report_change!("Dropping a pure new expression");
Expand Down

2 comments on commit cc848db

@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: cc848db Previous: aa3fab1 Ratio
es/full/bugs-1 361341 ns/iter (± 37986) 436307 ns/iter (± 32385) 0.83
es/full/minify/libraries/antd 1970925365 ns/iter (± 97019830) 2126277437 ns/iter (± 66544545) 0.93
es/full/minify/libraries/d3 469844760 ns/iter (± 35278960) 429381788 ns/iter (± 11305454) 1.09
es/full/minify/libraries/echarts 1723843083 ns/iter (± 155929745) 1812696108 ns/iter (± 70089579) 0.95
es/full/minify/libraries/jquery 116960351 ns/iter (± 9732582) 173313745 ns/iter (± 101346846) 0.67
es/full/minify/libraries/lodash 132184701 ns/iter (± 4100701) 143424466 ns/iter (± 4605003) 0.92
es/full/minify/libraries/moment 80288984 ns/iter (± 17742530) 71211711 ns/iter (± 5020616) 1.13
es/full/minify/libraries/react 26810802 ns/iter (± 3088453) 25783015 ns/iter (± 2638855) 1.04
es/full/minify/libraries/terser 386230068 ns/iter (± 59321492) 346419831 ns/iter (± 23592199) 1.11
es/full/minify/libraries/three 732739609 ns/iter (± 47152743) 625352795 ns/iter (± 40312972) 1.17
es/full/minify/libraries/typescript 4424121904 ns/iter (± 265980401) 4148999821 ns/iter (± 117806755) 1.07
es/full/minify/libraries/victory 1036680382 ns/iter (± 110054270) 917375856 ns/iter (± 31948624) 1.13
es/full/minify/libraries/vue 459858033 ns/iter (± 106735268) 187323170 ns/iter (± 22261199) 2.45
es/full/codegen/es3 39031 ns/iter (± 7039) 42938 ns/iter (± 1727) 0.91
es/full/codegen/es5 40508 ns/iter (± 6356) 42750 ns/iter (± 5070) 0.95
es/full/codegen/es2015 40911 ns/iter (± 12898) 42222 ns/iter (± 2050) 0.97
es/full/codegen/es2016 43661 ns/iter (± 10816) 42368 ns/iter (± 3179) 1.03
es/full/codegen/es2017 44226 ns/iter (± 9747) 42892 ns/iter (± 2919) 1.03
es/full/codegen/es2018 41469 ns/iter (± 13014) 43464 ns/iter (± 7032) 0.95
es/full/codegen/es2019 41094 ns/iter (± 8278) 42526 ns/iter (± 1851) 0.97
es/full/codegen/es2020 38558 ns/iter (± 5932) 43033 ns/iter (± 2202) 0.90
es/full/all/es3 232301760 ns/iter (± 61228780) 242975838 ns/iter (± 23480303) 0.96
es/full/all/es5 241090816 ns/iter (± 83246084) 230817583 ns/iter (± 16504197) 1.04
es/full/all/es2015 165743067 ns/iter (± 19607532) 181934898 ns/iter (± 14632648) 0.91
es/full/all/es2016 173814183 ns/iter (± 27493236) 181028295 ns/iter (± 15751721) 0.96
es/full/all/es2017 161714159 ns/iter (± 21427994) 182751339 ns/iter (± 9941067) 0.88
es/full/all/es2018 188642783 ns/iter (± 29828753) 181901217 ns/iter (± 14655668) 1.04
es/full/all/es2019 179093990 ns/iter (± 21437731) 178134235 ns/iter (± 18258760) 1.01
es/full/all/es2020 150752978 ns/iter (± 20152963) 174217115 ns/iter (± 17574202) 0.87
es/full/parser 774222 ns/iter (± 108428) 932897 ns/iter (± 133091) 0.83
es/full/base/fixer 27997 ns/iter (± 3767) 32752 ns/iter (± 860) 0.85
es/full/base/resolver_and_hygiene 100277 ns/iter (± 8106) 118428 ns/iter (± 4266) 0.85
serialization of ast node 220 ns/iter (± 27) 263 ns/iter (± 24) 0.84
serialization of serde 262 ns/iter (± 34) 272 ns/iter (± 29) 0.96

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

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: cc848db Previous: aa3fab1 Ratio
es/full/minify/libraries/vue 459858033 ns/iter (± 106735268) 187323170 ns/iter (± 22261199) 2.45

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

CC: @kdy1

Please sign in to comment.