improve gzip ratio via locality

This commit is contained in:
alexlamsl 2017-12-15 12:47:31 +08:00
parent 8f5d8a9563
commit 6339bda4bb
2 changed files with 25 additions and 20 deletions

View File

@ -3867,7 +3867,7 @@ merge(Compressor.prototype, {
}
}
if (fn instanceof AST_Function) {
var def, scope;
var def, scope, value;
if (compressor.option("inline")
&& !fn.uses_arguments
&& !fn.uses_eval
@ -3876,23 +3876,13 @@ merge(Compressor.prototype, {
: compressor.option("unused")
&& (def = exp.definition()).references.length == 1
&& !recursive_ref(compressor, def))
&& !self.has_pure_annotation(compressor)
&& !fn.contains_this()
&& (scope = can_flatten_args(fn))
&& !self.has_pure_annotation(compressor)) {
var value;
if (stat instanceof AST_Return) {
value = stat.value;
} else if (stat instanceof AST_SimpleStatement) {
value = make_node(AST_UnaryPrefix, stat, {
operator: "void",
expression: stat.body
});
}
if (value) {
var expressions = flatten_args(fn, scope);
expressions.push(value);
return make_sequence(self, expressions).optimize(compressor);
}
&& (value = flatten_body(stat))) {
var expressions = flatten_args(fn, scope);
expressions.push(value);
return make_sequence(self, expressions).optimize(compressor);
}
if (compressor.option("side_effects") && all(fn.body, is_empty)) {
var args = self.args.concat(make_node(AST_Undefined, self));
@ -3957,11 +3947,26 @@ merge(Compressor.prototype, {
for (i = len, len = self.args.length; i < len; i++) {
expressions.push(self.args[i]);
}
if (decls.length) scope.body.push(make_node(AST_Var, fn, {
definitions: decls
}));
if (decls.length) {
for (i = 0; compressor.parent(i) !== scope;) i++;
i = scope.body.indexOf(compressor.parent(i - 1)) + 1;
scope.body.splice(i, 0, make_node(AST_Var, fn, {
definitions: decls
}));
}
return expressions;
}
function flatten_body(stat) {
if (stat instanceof AST_Return) {
return stat.value;
} else if (stat instanceof AST_SimpleStatement) {
return make_node(AST_UnaryPrefix, stat, {
operator: "void",
expression: stat.body
});
}
}
});
OPT(AST_New, function(self, compressor){

View File

@ -767,8 +767,8 @@ issue_2476: {
expect: {
for (var sum = 0, i = 0; i < 10; i++)
sum += (x = i, y = i + 1, z = 3 * i, x < y ? x * y + z : x * z - y);
console.log(sum);
var x, y, z;
console.log(sum);
}
expect_stdout: "465"
}