Skip to content

Commit

Permalink
feat(es/minifier): Inline lazy inited vars in condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Austaras committed Oct 8, 2022
1 parent 5163200 commit 98455e9
Show file tree
Hide file tree
Showing 43 changed files with 342 additions and 338 deletions.
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 {
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 @@ -93,13 +103,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);
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

0 comments on commit 98455e9

Please sign in to comment.