enhance reduce_vars (#5248)

This commit is contained in:
Alex Lam S.L
2021-12-31 23:53:47 +00:00
committed by GitHub
parent e7ce1051fe
commit 8d0422b6f3
4 changed files with 15 additions and 15 deletions

View File

@@ -492,7 +492,6 @@ Compressor.prototype.compress = function(node) {
def.reassigned = 0; def.reassigned = 0;
def.recursive_refs = 0; def.recursive_refs = 0;
def.references = []; def.references = [];
def.should_replace = undefined;
def.single_use = undefined; def.single_use = undefined;
} }
@@ -11599,8 +11598,8 @@ Compressor.prototype.compress = function(node) {
def.replaced++; def.replaced++;
return value; return value;
} }
var local = self.fixed !== def.fixed; var state;
if (fixed && (local || def.should_replace !== false)) { if (fixed && (state = self.fixed || def.fixed).should_replace !== false) {
var ev, init; var ev, init;
if (fixed instanceof AST_This) { if (fixed instanceof AST_This) {
if (!is_funarg(def) && same_scope(def)) init = fixed; if (!is_funarg(def) && same_scope(def)) init = fixed;
@@ -11613,20 +11612,22 @@ Compressor.prototype.compress = function(node) {
init = make_node_from_constant(ev, fixed); init = make_node_from_constant(ev, fixed);
} }
if (init) { if (init) {
if (!local && def.should_replace === undefined) { if (state.should_replace === undefined) {
var value_length = init.optimize(compressor).print_to_string().length; var value_length = init.optimize(compressor).print_to_string().length;
if (!has_symbol_ref(fixed)) { if (!has_symbol_ref(fixed)) {
value_length = Math.min(value_length, fixed.print_to_string().length); value_length = Math.min(value_length, fixed.print_to_string().length);
} }
var name_length = def.name.length; var name_length = def.name.length;
if (compressor.option("unused") && !compressor.exposed(def)) { if (compressor.option("unused") && !compressor.exposed(def)) {
var referenced = def.references.length - def.replaced; var refs = def.references.length - def.replaced - def.assignments;
name_length += (name_length + 2 + value_length) / (referenced - def.assignments); refs = Math.min(refs, def.references.filter(function(ref) {
return ref.fixed === state;
}).length);
name_length += (name_length + 2 + value_length) / Math.max(1, refs);
} }
var delta = value_length - Math.floor(name_length); state.should_replace = value_length - Math.floor(name_length) < compressor.eval_threshold;
def.should_replace = delta < compressor.eval_threshold;
} }
if (local || def.should_replace) { if (state.should_replace) {
var value; var value;
if (has_symbol_ref(fixed)) { if (has_symbol_ref(fixed)) {
value = init.optimize(compressor); value = init.optimize(compressor);

View File

@@ -2752,8 +2752,7 @@ issue_3944: {
} }
expect: { expect: {
void function f() { void function f() {
while (a = 0 == (a = void 0), console.log(a), void 0); while (0 == void 0, console.log(false), void 0);
var a;
f; f;
}(); }();
} }

View File

@@ -3228,10 +3228,10 @@ issue_4653: {
f(a++ + (b = b), b |= console.log(a)); f(a++ + (b = b), b |= console.log(a));
} }
expect: { expect: {
var b = 1; var b, a = 1;
(function(c, d) { (function(c, d) {
c || console.log(d); c || console.log(d);
})(+b + (b = void 0), b |= console.log(2)); })(+a + (b = b), b |= console.log(2));
} }
expect_stdout: [ expect_stdout: [
"2", "2",

View File

@@ -905,7 +905,7 @@ use_before_var: {
} }
expect: { expect: {
function f(){ function f(){
console.log(void 0); console.log(t);
var t = 1; var t = 1;
} }
} }
@@ -981,7 +981,7 @@ inner_var_for_1: {
expect: { expect: {
function f() { function f() {
var a = 1; var a = 1;
x(1, void 0, d); x(1, b, d);
for (var b = 2, c = 3; x(1, 2, 3, d); x(1, 2, 3, d)) { for (var b = 2, c = 3; x(1, 2, 3, d); x(1, 2, 3, d)) {
var d = 4, e = 5; var d = 4, e = 5;
x(1, 2, 3, d, e); x(1, 2, 3, d, e);