Skip to content

Commit

Permalink
fix(es/minifier): Consider function body cost while inlining function (
Browse files Browse the repository at this point in the history
  • Loading branch information
Austaras committed Oct 15, 2022
1 parent 3352c45 commit b40d486
Show file tree
Hide file tree
Showing 66 changed files with 1,234 additions and 1,520 deletions.
9 changes: 2 additions & 7 deletions crates/swc/tests/tsc-references/ES5For-of10.2.minified.js
@@ -1,10 +1,5 @@
//// [ES5For-of10.ts]
function foo() {
return {
x: 0
};
}
for(var _i = 0, _iter = []; _i < _iter.length; _i++){
foo().x = _iter[_i];
for(var _i1 = 0, _iter1 = []; _i1 < _iter1.length; _i1++)foo().x = _iter1[_i1], foo().x;
_iter[_i];
for(var _i1 = 0, _iter1 = []; _i1 < _iter1.length; _i1++)_iter1[_i1];
}
7 changes: 1 addition & 6 deletions crates/swc/tests/tsc-references/ES5For-of34.2.minified.js
@@ -1,11 +1,6 @@
//// [ES5For-of34.ts]
function foo() {
return {
x: 0
};
}
for(var _i = 0, _iter = [
"a",
"b",
"c"
]; _i < _iter.length; _i++)foo().x = _iter[_i], foo().x;
]; _i < _iter.length; _i++)_iter[_i];
7 changes: 1 addition & 6 deletions crates/swc/tests/tsc-references/ES5For-of8.2.minified.js
@@ -1,11 +1,6 @@
//// [ES5For-of8.ts]
function foo() {
return {
x: 0
};
}
for(var _i = 0, _iter = [
"a",
"b",
"c"
]; _i < _iter.length; _i++)foo().x = _iter[_i], foo().x;
]; _i < _iter.length; _i++)_iter[_i];
9 changes: 2 additions & 7 deletions crates/swc/tests/tsc-references/ES5For-of9.2.minified.js
@@ -1,10 +1,5 @@
//// [ES5For-of9.ts]
function foo() {
return {
x: 0
};
}
for(var _i = 0, _iter = []; _i < _iter.length; _i++){
foo().x = _iter[_i];
for(var _i1 = 0, _iter1 = []; _i1 < _iter1.length; _i1++)foo().x = _iter1[_i1], foo().x;
_iter[_i];
for(var _i1 = 0, _iter1 = []; _i1 < _iter1.length; _i1++)_iter1[_i1];
}
@@ -1,8 +1,5 @@
//// [bitwiseNotOperatorWithStringType.ts]
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
function foo() {
return "abc";
}
var STRING, M, A = function() {
"use strict";
function A() {
Expand All @@ -17,4 +14,4 @@ var STRING, M, A = function() {
M.n = n;
}(M || (M = {}));
var objA = new A();
objA.a, M.n, foo(), A.foo(), STRING.charAt(0), foo(), objA.a, M.n;
objA.a, M.n, A.foo(), STRING.charAt(0), objA.a, M.n;
Expand Up @@ -14,10 +14,4 @@ var A = function A() {
}
return B;
}(A);
function NewA(Factory) {
return new A;
}
function NewB(Factory) {
return new B;
}
NewA(A), NewA(B), NewB(A), NewB(B);
new A, new A, new B, new B;
Expand Up @@ -5,16 +5,17 @@ import _to_consumable_array from "@swc/helpers/src/_to_consumable_array.mjs";
var _undefined = _sliced_to_array(void 0, 2);
_undefined[0], _undefined[1];
var _undefined1 = _sliced_to_array(void 0, 2);
function foo() {
return [
1,
2,
3
];
}
_undefined1[0], _undefined1[1];
var ref = _sliced_to_array(foo(), 2);
ref[0], ref[1], _to_array(foo()).slice(0);
var ref = _sliced_to_array([
1,
2,
3
], 2);
ref[0], ref[1], _to_array([
1,
2,
3
]).slice(0);
var ref1 = _sliced_to_array(_to_consumable_array([
1,
2,
Expand Down
Expand Up @@ -5,16 +5,17 @@ import _to_consumable_array from "@swc/helpers/src/_to_consumable_array.mjs";
var _undefined = _sliced_to_array(void 0, 2);
_undefined[0], _undefined[1];
var _undefined1 = _sliced_to_array(void 0, 2);
function foo() {
return [
1,
2,
3
];
}
_undefined1[0], _undefined1[1];
var ref = _sliced_to_array(foo(), 2);
ref[0], ref[1], _to_array(foo()).slice(0);
var ref = _sliced_to_array([
1,
2,
3
], 2);
ref[0], ref[1], _to_array([
1,
2,
3
]).slice(0);
var ref1 = _sliced_to_array(_to_consumable_array([
1,
2,
Expand Down
Expand Up @@ -7,15 +7,15 @@ var [a0, a1] = void 0, [a2 = !1, a3 = 1] = void 0, [b0, b1, b2] = [
1,
2,
"string"
];
function foo() {
return [
1,
2,
3
];
}
var [b6, b7] = foo(), [...b8] = foo(), [c0, c1] = [
], [b6, b7] = [
1,
2,
3
], [...b8] = [
1,
2,
3
], [c0, c1] = [
1,
2,
3
Expand Down
@@ -1,11 +1,5 @@
//// [destructuringWithLiteralInitializers.ts]
import _sliced_to_array from "@swc/helpers/src/_sliced_to_array.mjs";
function f2(param) {
param.x, param.y;
}
function f3(param) {
param.x, param.y;
}
function f4() {
var ref = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {
x: 0,
Expand Down Expand Up @@ -51,16 +45,28 @@ function g5() {
}({
x: 1,
y: 1
}), f2({
}), function(param) {
param.x, param.y;
}({
x: 1
}), f2({
}), function(param) {
param.x, param.y;
}({
x: 1,
y: 1
}), f3({}), f3({
}), function(param) {
param.x, param.y;
}({}), function(param) {
param.x, param.y;
}({
x: 1
}), f3({
}), function(param) {
param.x, param.y;
}({
y: 1
}), f3({
}), function(param) {
param.x, param.y;
}({
x: 1,
y: 1
}), f4(), f4({
Expand Down
Expand Up @@ -2,19 +2,13 @@
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
import _inherits from "@swc/helpers/src/_inherits.mjs";
import _create_super from "@swc/helpers/src/_create_super.mjs";
function rec1() {
return rec2();
}
function rec2() {
return rec1();
}
function rec3() {
return rec4();
return rec2();
}
function rec4() {
return rec3();
return rec4();
}
rec1(), rec2(), rec3(), rec4();
rec2(), rec2(), rec4(), rec4();
var Base = function Base() {
"use strict";
_class_call_check(this, Base);
Expand Down
@@ -1,11 +1 @@
//// [genericCallWithFunctionTypedArguments.ts]
function foo(x) {
return x(null);
}
foo(function(x) {
return "";
}), foo(function(x) {
return "";
}), foo(function(x) {
return "";
});
Expand Up @@ -3,7 +3,4 @@ var i, i2, a;
function foo(x) {
return new x(null);
}
function foo2(x, cb) {
return new cb(x);
}
foo(i), foo(i), foo(i2), foo(a), foo2(1, i2), foo2(1, a), foo2(1, i), foo2("", i2), new i(null), new a(null), new i2(1), new i2("");
foo(i), foo(i), foo(i2), foo(a), new i2(1), new a(1), new i(1), new i2(""), new i(null), new a(null), new i2(1), new i2("");
Expand Up @@ -22,21 +22,19 @@ var i, Base = function Base() {
}
return Derived2;
}(Base);
function f(a) {
return [
a.x,
a.y
];
}
function f2(a) {
return function(x) {
return a.y;
};
}
f({
!function(a) {
a.x, a.y;
}({
x: new Derived(),
y: new Derived2()
}), f({
}), function(a) {
a.x, a.y;
}({
x: new Base(),
y: new Derived2()
}), f2({
Expand Down
Expand Up @@ -14,13 +14,4 @@ var Base = function Base() {
}
return Derived;
}(Base);
function f3(x, y) {
return y(null);
}
new Base(), new Derived(), new Derived(), new Derived(), f3(new Base(), function(x) {
return x;
}), f3(new Derived(), function(x) {
return x;
}), f3(null, null), f3(null, function(x) {
return x;
});
new Base(), new Derived(), new Derived(), new Derived(), new Base(), new Derived(), (null)(null);
Expand Up @@ -2,7 +2,7 @@
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
import _inherits from "@swc/helpers/src/_inherits.mjs";
import _create_super from "@swc/helpers/src/_create_super.mjs";
var Base = function Base() {
var y, y1, Base = function Base() {
"use strict";
_class_call_check(this, Base);
}, Derived = function(Base) {
Expand All @@ -22,13 +22,8 @@ var Base = function Base() {
}
return Derived2;
}(Base);
function f3(y, x) {
return y(null);
}
new Derived(), new Derived2(), new Derived(), new Derived2(), new Derived(), new Derived2(), f3(function(x) {
new Derived(), new Derived2(), new Derived(), new Derived2(), new Derived(), new Derived2(), y = function(x) {
return x;
}, new Base()), f3(function(x) {
}, new Base(), y(null), y1 = function(x) {
return x;
}, new Derived()), f3(function(x) {
return x;
}, null);
}, new Derived(), y1(null);
@@ -1,7 +1,4 @@
//// [iteratorSpreadInCall11.ts]
function foo(...s) {
return s[0];
}
class SymbolIterator {
next() {
return {
Expand All @@ -13,4 +10,6 @@ class SymbolIterator {
return this;
}
}
foo(...new SymbolIterator);
!function(...s) {
s[0];
}(...new SymbolIterator);
@@ -1,7 +1,4 @@
//// [iteratorSpreadInCall7.ts]
function foo(...s) {
return s[0];
}
class SymbolIterator {
next() {
return {
Expand All @@ -24,4 +21,6 @@ class _StringIterator {
return this;
}
}
foo(...new SymbolIterator, ...new _StringIterator);
!function(...s) {
s[0];
}(...new SymbolIterator, ...new _StringIterator);
@@ -1,8 +1,5 @@
//// [logicalAssignment10.ts]
var _obj, _ref, _ref1, _ref2, ref, ref1, count = 0, obj = {};
function incr() {
return ++count;
}
null !== (ref = (_obj = obj)[_ref = incr()]) && void 0 !== ref || (_obj[_ref] = incr()), null !== (ref1 = (_ref1 = ({
null !== (ref = (_obj = obj)[_ref = ++count]) && void 0 !== ref || (_obj[_ref] = ++count), null !== (ref1 = (_ref1 = ({
obj
}).obj)[_ref2 = incr()]) && void 0 !== ref1 || (_ref1[_ref2] = incr());
}).obj)[_ref2 = ++count]) && void 0 !== ref1 || (_ref1[_ref2] = ++count);
@@ -1,8 +1,5 @@
//// [logicalAssignment10.ts]
var _obj, _ref, _ref1, _ref2, count = 0, obj = {};
function incr() {
return ++count;
}
(_obj = obj)[_ref = incr()], _obj[_ref] = incr(), (_ref1 = ({
(_obj = obj)[_ref = ++count], _obj[_ref] = ++count, (_ref1 = ({
obj
}).obj)[_ref2 = incr()], _ref1[_ref2] = incr();
}).obj)[_ref2 = ++count], _ref1[_ref2] = ++count;
@@ -1,9 +1,6 @@
//// [logicalAssignment10.ts]
var count = 0, obj = {};
function incr() {
return ++count;
}
const oobj = {
obj
};
obj[incr()] ??= incr(), oobj.obj[incr()] ??= incr();
obj[++count] ??= ++count, oobj.obj[++count] ??= ++count;
@@ -1,9 +1,6 @@
//// [logicalAssignment10.ts]
var count = 0, obj = {};
function incr() {
return ++count;
}
const oobj = {
obj
};
obj[incr()] ??= incr(), oobj.obj[incr()] ??= incr();
obj[++count] ??= ++count, oobj.obj[++count] ??= ++count;

1 comment on commit b40d486

@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: b40d486 Previous: 899021e Ratio
es/full/minify/libraries/antd 2015562752 ns/iter (± 28005356) 1834331506 ns/iter (± 39885588) 1.10
es/full/minify/libraries/d3 384817667 ns/iter (± 23995060) 418094059 ns/iter (± 14690679) 0.92
es/full/minify/libraries/echarts 1706567840 ns/iter (± 47308913) 1530692132 ns/iter (± 29775002) 1.11
es/full/minify/libraries/jquery 114085605 ns/iter (± 3107774) 103102409 ns/iter (± 4695676) 1.11
es/full/minify/libraries/lodash 137797128 ns/iter (± 13707766) 117076105 ns/iter (± 6573008) 1.18
es/full/minify/libraries/moment 74946097 ns/iter (± 15800127) 61189968 ns/iter (± 3739323) 1.22
es/full/minify/libraries/react 25100276 ns/iter (± 1373092) 20542165 ns/iter (± 2542483) 1.22
es/full/minify/libraries/terser 478940130 ns/iter (± 336414217) 347706361 ns/iter (± 24249666) 1.38
es/full/minify/libraries/three 627116485 ns/iter (± 24200158) 609781058 ns/iter (± 80759084) 1.03
es/full/minify/libraries/typescript 4218395148 ns/iter (± 204695328) 3542738000 ns/iter (± 116026114) 1.19
es/full/minify/libraries/victory 926313940 ns/iter (± 87392427) 842689386 ns/iter (± 58118087) 1.10
es/full/minify/libraries/vue 174570347 ns/iter (± 8157904) 162035246 ns/iter (± 32361176) 1.08
es/full/codegen/es3 41927 ns/iter (± 1199) 34212 ns/iter (± 655) 1.23
es/full/codegen/es5 41501 ns/iter (± 1364) 33959 ns/iter (± 1318) 1.22
es/full/codegen/es2015 41220 ns/iter (± 2111) 34331 ns/iter (± 1718) 1.20
es/full/codegen/es2016 41122 ns/iter (± 2960) 34011 ns/iter (± 800) 1.21
es/full/codegen/es2017 40630 ns/iter (± 2716) 34160 ns/iter (± 958) 1.19
es/full/codegen/es2018 41199 ns/iter (± 4842) 34482 ns/iter (± 1860) 1.19
es/full/codegen/es2019 41724 ns/iter (± 27729) 34339 ns/iter (± 2857) 1.22
es/full/codegen/es2020 41720 ns/iter (± 13576) 34066 ns/iter (± 1591) 1.22
es/full/all/es3 239405001 ns/iter (± 23327302) 203693619 ns/iter (± 15604482) 1.18
es/full/all/es5 226843921 ns/iter (± 21595910) 198572596 ns/iter (± 16426858) 1.14
es/full/all/es2015 183606446 ns/iter (± 17213743) 158778254 ns/iter (± 10797416) 1.16
es/full/all/es2016 182694742 ns/iter (± 14969074) 158913360 ns/iter (± 15748840) 1.15
es/full/all/es2017 183324645 ns/iter (± 13771465) 160350772 ns/iter (± 15680348) 1.14
es/full/all/es2018 176973367 ns/iter (± 11613859) 155373213 ns/iter (± 11634451) 1.14
es/full/all/es2019 179108311 ns/iter (± 16975959) 154751170 ns/iter (± 11488696) 1.16
es/full/all/es2020 174247112 ns/iter (± 20964067) 149262786 ns/iter (± 10724500) 1.17
es/full/parser 885301 ns/iter (± 76033) 733323 ns/iter (± 44394) 1.21
es/full/base/fixer 33082 ns/iter (± 1310) 27213 ns/iter (± 1544) 1.22
es/full/base/resolver_and_hygiene 120100 ns/iter (± 11597) 94711 ns/iter (± 3773) 1.27
serialization of ast node 253 ns/iter (± 14) 209 ns/iter (± 15) 1.21
serialization of serde 256 ns/iter (± 15) 210 ns/iter (± 3) 1.22

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

Please sign in to comment.