Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/minifier): Inline lazy inited vars in condition #6089

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Promise.all(assignAll).then(function() {
switch(a.label){
case 0:
for(c in e = function(u) {
var t, e;
var t;
return n(this, function(n) {
switch(n.label){
case 0:
Expand Down
35 changes: 17 additions & 18 deletions crates/swc/tests/vercel/full/next-33088/output/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import r from "@swc/helpers/src/_async_to_generator.mjs";
import t from "@swc/helpers/src/_sliced_to_array.mjs";
import t from "@swc/helpers/src/_async_to_generator.mjs";
import r from "@swc/helpers/src/_sliced_to_array.mjs";
import e from "@swc/helpers/src/_ts_generator.mjs";
import { jsx as n, jsxs as s, Fragment as a } from "react/jsx-runtime";
import * as c from "react";
export default function i() {
var i = t(c.useState({
var i = r(c.useState({
hits: []
}), 2), o = i[0], u = i[1], l = t(c.useState("react"), 2), h = l[0], f = l[1];
}), 2), o = i[0], u = i[1], l = r(c.useState("react"), 2), h = l[0], f = l[1];
return c.useEffect(function() {
"" !== h && function() {
t.apply(this, arguments);
r.apply(this, arguments);
}();
function t() {
return (t = r(function() {
var r, t;
return e(this, function(r) {
switch(r.label){
function r() {
return (r = t(function() {
return e(this, function(t) {
switch(t.label){
case 0:
return [
4,
Expand All @@ -24,10 +23,10 @@ export default function i() {
case 1:
return [
4,
r.sent().json()
t.sent().json()
];
case 2:
return u(r.sent()), [
return u(t.sent()), [
2
];
}
Expand All @@ -40,18 +39,18 @@ export default function i() {
children: [
n("input", {
value: h,
onChange: function(r) {
return f(r.target.value);
onChange: function(t) {
return f(t.target.value);
}
}),
n("ul", {
children: o.hits.map(function(r) {
children: o.hits.map(function(t) {
return n("li", {
children: n("a", {
href: r.url,
children: r.title
href: t.url,
children: t.title
})
}, r.objectID);
}, t.objectID);
})
})
]
Expand Down
18 changes: 9 additions & 9 deletions crates/swc/tests/vercel/full/utf8-1/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ s = s || "";
var _ = null, u = __webpack_hash__, d = s + (s.endsWith("/") ? "" : "/") + "_next/static/webpack/";
function p() {
return (p = e(function() {
var e, a, r, n, c;
return t(this, function(e) {
switch(e.label){
var e, a;
return t(this, function(t) {
switch(t.label){
case 0:
if (!(_ !== __webpack_hash__) || "idle" !== module.hot.status()) return [
2
];
e.label = 1;
t.label = 1;
case 1:
return e.trys.push([
return t.trys.push([
1,
4,
,
Expand All @@ -31,17 +31,17 @@ function p() {
case 2:
return [
4,
e.sent().json()
t.sent().json()
];
case 3:
return a = e.sent(), r = "/" === i ? "index" : i, (Array.isArray(a.c) ? a.c : Object.keys(a.c)).some(function(e) {
return -1 !== e.indexOf("pages".concat(r.startsWith("/") ? r : "/".concat(r))) || -1 !== e.indexOf("pages".concat(r.startsWith("/") ? r : "/".concat(r)).replace(/\//g, "\\"));
return e = t.sent(), a = "/" === i ? "index" : i, (Array.isArray(e.c) ? e.c : Object.keys(e.c)).some(function(e) {
return -1 !== e.indexOf("pages".concat(a.startsWith("/") ? a : "/".concat(a))) || -1 !== e.indexOf("pages".concat(a.startsWith("/") ? a : "/".concat(a)).replace(/\//g, "\\"));
}) ? document.location.reload(!0) : u = _, [
3,
5
];
case 4:
return console.error("Error occurred checking for update", e.sent()), document.location.reload(!0), [
return console.error("Error occurred checking for update", t.sent()), document.location.reload(!0), [
3,
5
];
Expand Down
19 changes: 11 additions & 8 deletions crates/swc_ecma_minifier/src/analyzer/storage/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,22 @@ impl Storage for ProgramData {
Entry::Occupied(mut e) => {
e.get_mut().inline_prevented |= var_info.inline_prevented;

e.get_mut().ref_count += var_info.ref_count;
e.get_mut().cond_init |= if !inited && e.get().var_initialized {
true
} else {
var_info.cond_init
};
if var_info.var_initialized {
if e.get().var_initialized || e.get().ref_count > 0 {
Copy link
Member Author

Choose a reason for hiding this comment

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

This line needs to happen before ref_count merge and after cond_init merge

e.get_mut().assign_count += 1;
e.get_mut().reassigned_with_assignment = true;
} else {
// If it is referred outside child scope, it will
// be marked as var_initialized false
e.get_mut().var_initialized = true;
}
}
e.get_mut().ref_count += var_info.ref_count;

e.get_mut().reassigned_with_assignment |= var_info.reassigned_with_assignment;
e.get_mut().reassigned_with_var_decl |= var_info.reassigned_with_var_decl;
Expand Down Expand Up @@ -94,13 +104,6 @@ impl Storage for ProgramData {
e.get_mut().is_fn_local &= var_info.is_fn_local;
e.get_mut().used_in_non_child_fn |= var_info.used_in_non_child_fn;

if var_info.var_initialized {
if e.get().var_initialized || e.get().ref_count > 0 {
e.get_mut().assign_count += 1;
e.get_mut().reassigned_with_assignment = true;
}
}

match kind {
ScopeKind::Fn => {
e.get_mut().is_fn_local = false;
Expand Down
36 changes: 18 additions & 18 deletions crates/swc_ecma_minifier/tests/benches-full/d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1969,19 +1969,19 @@
var t0, i0;
function tween() {
var fullname1, i, i1 = value.apply(this, arguments);
Austaras marked this conversation as resolved.
Show resolved Hide resolved
return i1 !== i0 && (t0 = (i0 = i1) && (fullname1 = fullname, i = i1, function(t) {
this.setAttributeNS(fullname1.space, fullname1.local, i.call(this, t));
})), t0;
return i1 !== i0 && (t0 = (i0 = i1) && function(t) {
this.setAttributeNS(fullname.space, fullname.local, i1.call(this, t));
}), t0;
}
return tween._value = value, tween;
}
function attrTween(name, value) {
var t0, i0;
function tween() {
var name1, i, i1 = value.apply(this, arguments);
return i1 !== i0 && (t0 = (i0 = i1) && (name1 = name, i = i1, function(t) {
this.setAttribute(name1, i.call(this, t));
})), t0;
return i1 !== i0 && (t0 = (i0 = i1) && function(t) {
this.setAttribute(name, i1.call(this, t));
}), t0;
}
return tween._value = value, tween;
}
Expand Down Expand Up @@ -2124,9 +2124,9 @@
var t, i0;
function tween() {
var name1, i, priority1, i1 = value.apply(this, arguments);
return i1 !== i0 && (t = (i0 = i1) && (name1 = name, i = i1, priority1 = priority, function(t) {
this.style.setProperty(name1, i.call(this, t), priority1);
})), t;
return i1 !== i0 && (t = (i0 = i1) && function(t) {
this.style.setProperty(name, i1.call(this, t), priority);
}), t;
}
return tween._value = value, tween;
}(name, value, null == priority ? "" : priority));
Expand All @@ -2149,9 +2149,9 @@
var t0, i0;
function tween() {
var i, i1 = value.apply(this, arguments);
return i1 !== i0 && (t0 = (i0 = i1) && (i = i1, function(t) {
this.textContent = i.call(this, t);
})), t0;
return i1 !== i0 && (t0 = (i0 = i1) && function(t) {
this.textContent = i1.call(this, t);
}), t0;
}
return tween._value = value, tween;
}(value));
Expand Down Expand Up @@ -5698,12 +5698,12 @@
}
return projection.stream = function(stream) {
var rotate1;
return cache && cacheStream === stream ? cache : cache = transformRadians((rotate1 = rotate, transformer({
return cache && cacheStream === stream ? cache : cache = transformRadians(transformer({
point: function(x, y) {
var r = rotate1(x, y);
var r = rotate(x, y);
return this.stream.point(r[0], r[1]);
}
}))(preclip(projectResample(postclip(cacheStream = stream)))));
})(preclip(projectResample(postclip(cacheStream = stream)))));
}, projection.preclip = function(_) {
return arguments.length ? (preclip = _, theta = void 0, reset()) : preclip;
}, projection.postclip = function(_) {
Expand Down Expand Up @@ -6203,7 +6203,7 @@
for(var node, nodes = parent.children, i = -1, n = nodes.length, k = parent.value && (x1 - x0) / parent.value; ++i < n;)(node = nodes[i]).y0 = y0, node.y1 = y1, node.x0 = x0, node.x1 = x0 += node.value * k;
}
equalEarthRaw.invert = function(x, y) {
for(var delta, fy, fpy, l = y, l2 = l * l, l6 = l2 * l2 * l2, i = 0; i < 12 && (fy = l * (1.340264 + -0.081106 * l2 + l6 * (0.000893 + 0.003796 * l2)) - y, l -= delta = fy / (1.340264 + -0.24331799999999998 * l2 + l6 * (0.0062510000000000005 + 0.034164 * l2)), l6 = (l2 = l * l) * l2 * l2, !(1e-12 > abs$2(delta))); ++i);
for(var delta, fy, l = y, l2 = l * l, l6 = l2 * l2 * l2, i = 0; i < 12 && (fy = l * (1.340264 + -0.081106 * l2 + l6 * (0.000893 + 0.003796 * l2)) - y, l -= delta = fy / (1.340264 + -0.24331799999999998 * l2 + l6 * (0.0062510000000000005 + 0.034164 * l2)), l6 = (l2 = l * l) * l2 * l2, !(1e-12 > abs$2(delta))); ++i);
return [
M * x * (1.340264 + -0.24331799999999998 * l2 + l6 * (0.0062510000000000005 + 0.034164 * l2)) / cos$1(l),
asin(sin$1(l) / M)
Expand Down Expand Up @@ -9046,7 +9046,7 @@
var previousNode, x = 0;
root.eachAfter(function(node) {
var children, children1 = node.children;
children1 ? (node.x = (children = children1).reduce(meanXReduce, 0) / children.length, node.y = 1 + children1.reduce(maxYReduce, 0)) : (node.x = previousNode ? x += separation(node, previousNode) : 0, node.y = 0, previousNode = node);
children1 ? (node.x = children1.reduce(meanXReduce, 0) / children1.length, node.y = 1 + children1.reduce(maxYReduce, 0)) : (node.x = previousNode ? x += separation(node, previousNode) : 0, node.y = 0, previousNode = node);
});
var left = function(node) {
for(var children; children = node.children;)node = children[0];
Expand Down Expand Up @@ -10671,7 +10671,7 @@
for(var vim, v1, ancestor1, shift, vip = v, vop = v, vim1 = w, vom = vip.parent.children[0], sip = vip.m, sop = vop.m, sim = vim1.m, som = vom.m; vim1 = nextRight(vim1), vip = nextLeft(vip), vim1 && vip;)vom = nextLeft(vom), (vop = nextRight(vop)).a = v, (shift = vim1.z + sim - vip.z - sip + separation(vim1._, vip._)) > 0 && (function(wm, wp, shift) {
var change = shift / (wp.i - wm.i);
wp.c -= change, wp.s += shift, wm.c += change, wp.z += shift, wp.m += shift;
}((vim = vim1, v1 = v, ancestor1 = ancestor, vim.a.parent === v1.parent ? vim.a : ancestor1), v, shift), sip += shift, sop += shift), sim += vim1.m, sip += vip.m, som += vom.m, sop += vop.m;
}((vim = vim1, ancestor1 = ancestor, vim.a.parent === v.parent ? vim.a : ancestor1), v, shift), sip += shift, sop += shift), sim += vim1.m, sip += vip.m, som += vom.m, sop += vop.m;
vim1 && !nextRight(vop) && (vop.t = vim1, vop.m += sim - sop), vip && !nextLeft(vom) && (vom.t = vip, vom.m += sip - som, ancestor = v);
}
return ancestor;
Expand Down