Skip to content

Commit

Permalink
fix(es/minifier): Fix collapse_vars (#6235)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #6217
  • Loading branch information
kdy1 committed Oct 23, 2022
1 parent 2dbff69 commit 48bb0cc
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 35 deletions.
64 changes: 64 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6217/1/input/.swcrc
@@ -0,0 +1,64 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2019",
"loose": false,
"minify": {
"compress": {
"arguments": false,
"arrows": false,
"booleans": false,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": false,
"computed_props": false,
"conditionals": false,
"dead_code": false,
"directives": false,
"drop_console": false,
"drop_debugger": false,
"evaluate": false,
"expression": false,
"hoist_funs": false,
"hoist_props": false,
"hoist_vars": false,
"if_return": false,
"join_vars": false,
"keep_classnames": false,
"keep_fargs": false,
"keep_fnames": false,
"keep_infinity": false,
"loops": false,
"negate_iife": false,
"properties": false,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": false,
"switches": false,
"typeofs": false,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": false,
"const_to_let": false,
"pristine_globals": false
},
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}
13 changes: 13 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6217/1/input/input.js
@@ -0,0 +1,13 @@
const a = 0;
let b;
for (let i = 0; i < 2; ++i) {
if (i === 0) {
b = 1;
} else {
b = 2;
}
console.log(a, b);
}

let c = 1, d;
while (c--) { }
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6217/1/output/input.js
@@ -0,0 +1,4 @@
let b;
for(let i = 0; i < 2; ++i)console.log(0, b = 0 === i ? 1 : 2);
let c = 1, d;
while(c--);
64 changes: 64 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6217/2/input/.swcrc
@@ -0,0 +1,64 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2019",
"loose": false,
"minify": {
"compress": {
"arguments": false,
"arrows": false,
"booleans": false,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": false,
"computed_props": false,
"conditionals": false,
"dead_code": false,
"directives": false,
"drop_console": false,
"drop_debugger": false,
"evaluate": false,
"expression": false,
"hoist_funs": false,
"hoist_props": false,
"hoist_vars": false,
"if_return": false,
"join_vars": false,
"keep_classnames": false,
"keep_fargs": false,
"keep_fnames": false,
"keep_infinity": false,
"loops": false,
"negate_iife": false,
"properties": false,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": false,
"switches": false,
"typeofs": false,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": false,
"const_to_let": false,
"pristine_globals": false
},
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}
14 changes: 14 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6217/2/input/input.js
@@ -0,0 +1,14 @@
const a = 0;
let b;
for (let i = 0; i < 2; ++i) {
if (i === 0) {
b = 1;
} else {
b = 2;
}
console.log(a, b);
}

let c, d;
c = 1
while (c--) { }
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6217/2/output/input.js
@@ -0,0 +1,4 @@
let b, c, d;
for(let i = 0; i < 2; ++i)console.log(0, b = 0 === i ? 1 : 2);
c = 1;
while(c--);
4 changes: 4 additions & 0 deletions crates/swc_ecma_minifier/src/compress/pure/vars.rs
Expand Up @@ -441,6 +441,10 @@ impl VisitMut for VarMover {
let has_init = d.iter().any(|v| v.init.is_some());

if has_init {
if self.target == VarDeclKind::Let {
return;
}

let mut new = Vec::with_capacity(d.len());

d.take().into_iter().for_each(|v| {
Expand Down
64 changes: 33 additions & 31 deletions crates/swc_ecma_minifier/tests/benches-full/d3.js
Expand Up @@ -3288,6 +3288,7 @@
this._triangles = new Uint32Array(3 * maxTriangles), this._halfedges = new Int32Array(3 * maxTriangles), this._hashSize = Math.ceil(Math.sqrt(n)), this._hullPrev = new Uint32Array(n), this._hullNext = new Uint32Array(n), this._hullTri = new Uint32Array(n), this._hullHash = new Int32Array(this._hashSize).fill(-1), this._ids = new Uint32Array(n), this._dists = new Float64Array(n), this.update();
}
update() {
let i0, i1, i2;
const { coords , _hullPrev: hullPrev , _hullNext: hullNext , _hullTri: hullTri , _hullHash: hullHash } = this, n = coords.length >> 1;
let minX = 1 / 0, minY = 1 / 0, maxX = -1 / 0, maxY = -1 / 0;
for(let i = 0; i < n; i++){
Expand All @@ -3296,42 +3297,42 @@
}
const cx = (minX + maxX) / 2, cy = (minY + maxY) / 2;
let minDist = 1 / 0;
for(let i1 = 0; i1 < n; i1++){
const d = dist(cx, cy, coords[2 * i1], coords[2 * i1 + 1]);
d < minDist && (i0 = i1, minDist = d);
for(let i3 = 0; i3 < n; i3++){
const d = dist(cx, cy, coords[2 * i3], coords[2 * i3 + 1]);
d < minDist && (i0 = i3, minDist = d);
}
const i0x = coords[2 * i0], i0y = coords[2 * i0 + 1];
minDist = 1 / 0;
for(let i2 = 0; i2 < n; i2++){
if (i2 === i0) continue;
const d1 = dist(i0x, i0y, coords[2 * i2], coords[2 * i2 + 1]);
d1 < minDist && d1 > 0 && (i11 = i2, minDist = d1);
}
let i1x = coords[2 * i11], i1y = coords[2 * i11 + 1], minRadius = 1 / 0;
for(let i3 = 0; i3 < n; i3++){
if (i3 === i0 || i3 === i11) continue;
for(let i4 = 0; i4 < n; i4++){
if (i4 === i0) continue;
const d1 = dist(i0x, i0y, coords[2 * i4], coords[2 * i4 + 1]);
d1 < minDist && d1 > 0 && (i1 = i4, minDist = d1);
}
let i1x = coords[2 * i1], i1y = coords[2 * i1 + 1], minRadius = 1 / 0;
for(let i5 = 0; i5 < n; i5++){
if (i5 === i0 || i5 === i1) continue;
const r = function(ax, ay, bx, by, cx, cy) {
const dx = bx - ax, dy = by - ay, ex = cx - ax, ey = cy - ay, bl = dx * dx + dy * dy, cl = ex * ex + ey * ey, d = 0.5 / (dx * ey - dy * ex), x = (ey * bl - dy * cl) * d, y = (dx * cl - ex * bl) * d;
return x * x + y * y;
}(i0x, i0y, i1x, i1y, coords[2 * i3], coords[2 * i3 + 1]);
r < minRadius && (i21 = i3, minRadius = r);
}(i0x, i0y, i1x, i1y, coords[2 * i5], coords[2 * i5 + 1]);
r < minRadius && (i2 = i5, minRadius = r);
}
let i2x = coords[2 * i21], i2y = coords[2 * i21 + 1];
let i2x = coords[2 * i2], i2y = coords[2 * i2 + 1];
if (minRadius === 1 / 0) {
for(let i4 = 0; i4 < n; i4++)this._dists[i4] = coords[2 * i4] - coords[0] || coords[2 * i4 + 1] - coords[1];
for(let i6 = 0; i6 < n; i6++)this._dists[i6] = coords[2 * i6] - coords[0] || coords[2 * i6 + 1] - coords[1];
quicksort(this._ids, this._dists, 0, n - 1);
const hull = new Uint32Array(n);
let j = 0;
for(let i5 = 0, d0 = -1 / 0; i5 < n; i5++){
const id = this._ids[i5];
for(let i7 = 0, d0 = -1 / 0; i7 < n; i7++){
const id = this._ids[i7];
this._dists[id] > d0 && (hull[j++] = id, d0 = this._dists[id]);
}
this.hull = hull.subarray(0, j), this.triangles = new Uint32Array(0), this.halfedges = new Uint32Array(0);
return;
}
if (orient(i0x, i0y, i1x, i1y, i2x, i2y)) {
const i6 = i11, x1 = i1x, y1 = i1y;
i11 = i21, i1x = i2x, i1y = i2y, i21 = i6, i2x = x1, i2y = y1;
const i8 = i1, x1 = i1x, y1 = i1y;
i1 = i2, i1x = i2x, i1y = i2y, i2 = i8, i2x = x1, i2y = y1;
}
const center = function(ax, ay, bx, by, cx, cy) {
const dx = bx - ax, dy = by - ay, ex = cx - ax, ey = cy - ay, bl = dx * dx + dy * dy, cl = ex * ex + ey * ey, d = 0.5 / (dx * ey - dy * ex);
Expand All @@ -3341,13 +3342,13 @@
};
}(i0x, i0y, i1x, i1y, i2x, i2y);
this._cx = center.x, this._cy = center.y;
for(let i7 = 0; i7 < n; i7++)this._dists[i7] = dist(coords[2 * i7], coords[2 * i7 + 1], center.x, center.y);
for(let i9 = 0; i9 < n; i9++)this._dists[i9] = dist(coords[2 * i9], coords[2 * i9 + 1], center.x, center.y);
quicksort(this._ids, this._dists, 0, n - 1), this._hullStart = i0;
let hullSize = 3;
hullNext[i0] = hullPrev[i21] = i11, hullNext[i11] = hullPrev[i0] = i21, hullNext[i21] = hullPrev[i11] = i0, hullTri[i0] = 0, hullTri[i11] = 1, hullTri[i21] = 2, hullHash.fill(-1), hullHash[this._hashKey(i0x, i0y)] = i0, hullHash[this._hashKey(i1x, i1y)] = i11, hullHash[this._hashKey(i2x, i2y)] = i21, this.trianglesLen = 0, this._addTriangle(i0, i11, i21, -1, -1, -1);
for(let i0, i11, i21, xp, yp, k = 0; k < this._ids.length; k++){
const i8 = this._ids[k], x2 = coords[2 * i8], y2 = coords[2 * i8 + 1];
if (k > 0 && 0.0000000000000002220446049250313 >= Math.abs(x2 - xp) && 0.0000000000000002220446049250313 >= Math.abs(y2 - yp) || (xp = x2, yp = y2, i8 === i0 || i8 === i11 || i8 === i21)) continue;
hullNext[i0] = hullPrev[i2] = i1, hullNext[i1] = hullPrev[i0] = i2, hullNext[i2] = hullPrev[i1] = i0, hullTri[i0] = 0, hullTri[i1] = 1, hullTri[i2] = 2, hullHash.fill(-1), hullHash[this._hashKey(i0x, i0y)] = i0, hullHash[this._hashKey(i1x, i1y)] = i1, hullHash[this._hashKey(i2x, i2y)] = i2, this.trianglesLen = 0, this._addTriangle(i0, i1, i2, -1, -1, -1);
for(let k = 0, xp, yp; k < this._ids.length; k++){
const i10 = this._ids[k], x2 = coords[2 * i10], y2 = coords[2 * i10 + 1];
if (k > 0 && 0.0000000000000002220446049250313 >= Math.abs(x2 - xp) && 0.0000000000000002220446049250313 >= Math.abs(y2 - yp) || (xp = x2, yp = y2, i10 === i0 || i10 === i1 || i10 === i2)) continue;
let start = 0;
for(let j1 = 0, key = this._hashKey(x2, y2); j1 < this._hashSize && (-1 === (start = hullHash[(key + j1) % this._hashSize]) || start === hullNext[start]); j1++);
let e = start = hullPrev[start], q;
Expand All @@ -3356,15 +3357,15 @@
break;
}
if (-1 === e) continue;
let t = this._addTriangle(e, i8, hullNext[e], -1, -1, hullTri[e]);
hullTri[i8] = this._legalize(t + 2), hullTri[e] = t, hullSize++;
let t = this._addTriangle(e, i10, hullNext[e], -1, -1, hullTri[e]);
hullTri[i10] = this._legalize(t + 2), hullTri[e] = t, hullSize++;
let n1 = hullNext[e];
for(; q = hullNext[n1], orient(x2, y2, coords[2 * n1], coords[2 * n1 + 1], coords[2 * q], coords[2 * q + 1]);)t = this._addTriangle(n1, i8, q, hullTri[i8], -1, hullTri[n1]), hullTri[i8] = this._legalize(t + 2), hullNext[n1] = n1, hullSize--, n1 = q;
if (e === start) for(; orient(x2, y2, coords[2 * (q = hullPrev[e])], coords[2 * q + 1], coords[2 * e], coords[2 * e + 1]);)t = this._addTriangle(q, i8, e, -1, hullTri[e], hullTri[q]), this._legalize(t + 2), hullTri[q] = t, hullNext[e] = e, hullSize--, e = q;
this._hullStart = hullPrev[i8] = e, hullNext[e] = hullPrev[n1] = i8, hullNext[i8] = n1, hullHash[this._hashKey(x2, y2)] = i8, hullHash[this._hashKey(coords[2 * e], coords[2 * e + 1])] = e;
for(; q = hullNext[n1], orient(x2, y2, coords[2 * n1], coords[2 * n1 + 1], coords[2 * q], coords[2 * q + 1]);)t = this._addTriangle(n1, i10, q, hullTri[i10], -1, hullTri[n1]), hullTri[i10] = this._legalize(t + 2), hullNext[n1] = n1, hullSize--, n1 = q;
if (e === start) for(; orient(x2, y2, coords[2 * (q = hullPrev[e])], coords[2 * q + 1], coords[2 * e], coords[2 * e + 1]);)t = this._addTriangle(q, i10, e, -1, hullTri[e], hullTri[q]), this._legalize(t + 2), hullTri[q] = t, hullNext[e] = e, hullSize--, e = q;
this._hullStart = hullPrev[i10] = e, hullNext[e] = hullPrev[n1] = i10, hullNext[i10] = n1, hullHash[this._hashKey(x2, y2)] = i10, hullHash[this._hashKey(coords[2 * e], coords[2 * e + 1])] = e;
}
this.hull = new Uint32Array(hullSize);
for(let i9 = 0, e1 = this._hullStart; i9 < hullSize; i9++)this.hull[i9] = e1, e1 = hullNext[e1];
for(let i11 = 0, e1 = this._hullStart; i11 < hullSize; i11++)this.hull[i11] = e1, e1 = hullNext[e1];
this.triangles = this._triangles.subarray(0, this.trianglesLen), this.halfedges = this._halfedges.subarray(0, this.trianglesLen);
}
_hashKey(x, y) {
Expand Down Expand Up @@ -3631,8 +3632,9 @@
return V[v] || V[v + 1] ? this._clipInfinite(i, points, V[v], V[v + 1], V[v + 2], V[v + 3]) : this._clipFinite(i, points);
}
_clipFinite(i, points) {
let e0, e1;
const n = points.length;
let P = null, e0, e1, x0, y0, x1 = points[n - 2], y1 = points[n - 1], c0, c1 = this._regioncode(x1, y1);
let P = null, x0, y0, x1 = points[n - 2], y1 = points[n - 1], c0, c1 = this._regioncode(x1, y1);
for(let j = 0; j < n; j += 2)if (x0 = x1, y0 = y1, x1 = points[j], y1 = points[j + 1], c0 = c1, c1 = this._regioncode(x1, y1), 0 === c0 && 0 === c1) e0 = e1, e1 = 0, P ? P.push(x1, y1) : P = [
x1,
y1
Expand Down

Large diffs are not rendered by default.

1 comment on commit 48bb0cc

@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: 48bb0cc Previous: e4fe840 Ratio
es/full/bugs-1 330466 ns/iter (± 17476) 355400 ns/iter (± 36968) 0.93
es/full/minify/libraries/antd 1778429726 ns/iter (± 25039169) 1811454597 ns/iter (± 38003432) 0.98
es/full/minify/libraries/d3 420899933 ns/iter (± 18968822) 393229153 ns/iter (± 15423522) 1.07
es/full/minify/libraries/echarts 1530756720 ns/iter (± 18502448) 1547064241 ns/iter (± 48450832) 0.99
es/full/minify/libraries/jquery 98248786 ns/iter (± 4281056) 102090045 ns/iter (± 2525431) 0.96
es/full/minify/libraries/lodash 132422486 ns/iter (± 10466979) 116027910 ns/iter (± 3111979) 1.14
es/full/minify/libraries/moment 62867333 ns/iter (± 2580900) 62199599 ns/iter (± 7207774) 1.01
es/full/minify/libraries/react 21230147 ns/iter (± 1218990) 21210005 ns/iter (± 813021) 1.00
es/full/minify/libraries/terser 282838156 ns/iter (± 11186122) 330256402 ns/iter (± 11377486) 0.86
es/full/minify/libraries/three 550146154 ns/iter (± 15057353) 580301508 ns/iter (± 25364145) 0.95
es/full/minify/libraries/typescript 3279492102 ns/iter (± 31170846) 3280295049 ns/iter (± 23532606) 1.00
es/full/minify/libraries/victory 783806163 ns/iter (± 14292383) 782087702 ns/iter (± 14015832) 1.00
es/full/minify/libraries/vue 149003863 ns/iter (± 5359185) 147424260 ns/iter (± 3849611) 1.01
es/full/codegen/es3 33125 ns/iter (± 971) 32822 ns/iter (± 2268) 1.01
es/full/codegen/es5 33080 ns/iter (± 1755) 32348 ns/iter (± 823) 1.02
es/full/codegen/es2015 32964 ns/iter (± 594) 32328 ns/iter (± 1239) 1.02
es/full/codegen/es2016 32867 ns/iter (± 1273) 32401 ns/iter (± 693) 1.01
es/full/codegen/es2017 33111 ns/iter (± 645) 32722 ns/iter (± 1368) 1.01
es/full/codegen/es2018 32936 ns/iter (± 1414) 32543 ns/iter (± 1411) 1.01
es/full/codegen/es2019 33512 ns/iter (± 1076) 32489 ns/iter (± 1972) 1.03
es/full/codegen/es2020 33538 ns/iter (± 1069) 32928 ns/iter (± 405) 1.02
es/full/all/es3 189430771 ns/iter (± 4845827) 189124039 ns/iter (± 4798009) 1.00
es/full/all/es5 180788323 ns/iter (± 5885743) 180291595 ns/iter (± 8839620) 1.00
es/full/all/es2015 145451221 ns/iter (± 4753537) 143373696 ns/iter (± 5764054) 1.01
es/full/all/es2016 142768655 ns/iter (± 3759121) 142534850 ns/iter (± 3827517) 1.00
es/full/all/es2017 141460070 ns/iter (± 4031847) 141811895 ns/iter (± 4774052) 1.00
es/full/all/es2018 138973019 ns/iter (± 3348169) 140683347 ns/iter (± 7212672) 0.99
es/full/all/es2019 140564007 ns/iter (± 7007476) 139783809 ns/iter (± 3691302) 1.01
es/full/all/es2020 134548355 ns/iter (± 5005863) 136331026 ns/iter (± 6546232) 0.99
es/full/parser 693512 ns/iter (± 24219) 694424 ns/iter (± 23902) 1.00
es/full/base/fixer 25647 ns/iter (± 714) 25860 ns/iter (± 531) 0.99
es/full/base/resolver_and_hygiene 89477 ns/iter (± 4516) 91362 ns/iter (± 3610) 0.98
serialization of ast node 214 ns/iter (± 3) 214 ns/iter (± 12) 1
serialization of serde 217 ns/iter (± 2) 217 ns/iter (± 4) 1

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

Please sign in to comment.