no base54.reset()

slightly less improved gzip results
This commit is contained in:
alexlamsl 2018-02-23 00:28:59 +08:00
parent 13062a4758
commit 423d0146be
2 changed files with 83 additions and 71 deletions

View File

@ -458,9 +458,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
this.walk(tw); this.walk(tw);
to_mangle.forEach(function(def){ def.mangle(options) }); to_mangle.forEach(function(def){ def.mangle(options) });
if (options.group_voids) { if (!options.group_voids) return;
base54.reset();
base54.sort();
if (options.toplevel) this.group_voids(options); if (options.toplevel) this.group_voids(options);
else this.walk(new TreeWalker(function(node) { else this.walk(new TreeWalker(function(node) {
if (node instanceof AST_Scope && !(node instanceof AST_Toplevel)) { if (node instanceof AST_Scope && !(node instanceof AST_Toplevel)) {
@ -468,7 +466,6 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
return true; return true;
} }
})); }));
}
function collect(symbol) { function collect(symbol) {
if (!member(symbol.name, options.reserved)) { if (!member(symbol.name, options.reserved)) {
@ -477,11 +474,11 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
} }
}); });
AST_Scope.DEFMETHOD("find_colliding_names", function(options, all) { AST_Toplevel.DEFMETHOD("find_colliding_names", function(options) {
var cache = options.cache && options.cache.props; var cache = options.cache && options.cache.props;
var avoid = Object.create(null); var avoid = Object.create(null);
options.reserved.forEach(to_avoid); options.reserved.forEach(to_avoid);
if (this.globals) this.globals.each(add_def); this.globals.each(add_def);
this.walk(new TreeWalker(function(node) { this.walk(new TreeWalker(function(node) {
if (node instanceof AST_Scope) node.variables.each(add_def); if (node instanceof AST_Scope) node.variables.each(add_def);
if (node instanceof AST_SymbolCatch) add_def(node.definition()); if (node instanceof AST_SymbolCatch) add_def(node.definition());
@ -493,9 +490,9 @@ AST_Scope.DEFMETHOD("find_colliding_names", function(options, all) {
} }
function add_def(def) { function add_def(def) {
var name = def.mangled_name || def.name; var name = def.name;
if (def.global && cache && cache.has(name)) name = cache.get(name); if (def.global && cache && cache.has(name)) name = cache.get(name);
else if (!all && !def.unmangleable(options)) return; else if (!def.unmangleable(options)) return;
to_avoid(name); to_avoid(name);
} }
}); });
@ -536,27 +533,38 @@ AST_Toplevel.DEFMETHOD("expand_names", function(options) {
}); });
AST_Scope.DEFMETHOD("group_voids", function(options) { AST_Scope.DEFMETHOD("group_voids", function(options) {
var avoid = this.find_colliding_names(options, true); var self = this, enclosed = [];
var cname = 0; var scopes = [], count = 0;
var name; self.walk(new TreeWalker(function(node, descend) {
do { if (node instanceof AST_Scope && node !== self) {
name = base54(cname++); scopes.unshift(node);
} while (avoid[name] || !is_identifier(name)); descend();
var count = 0; if (scopes[0] === node) scopes.shift();
this.transform(new TreeTransformer(function(node) { return true;
if (node instanceof AST_Undefined }
|| node instanceof AST_UnaryPrefix if (count > 0 && scopes.length == 0) return;
&& node.operator == "void" if (is_undefined(node)) {
&& node.expression.is_constant()) {
count++; count++;
return new AST_SymbolRef({ var scope;
while (scope = scopes.shift()) {
scope.enclosed.forEach(function(def) {
push_uniq(enclosed, def);
});
}
}
}));
if (count < 1) return;
var save = self.enclosed;
self.enclosed = enclosed;
var name = next_mangled(self, options);
self.enclosed = save;
this.transform(new TreeTransformer(function(node) {
if (is_undefined(node)) return new AST_SymbolRef({
name: name, name: name,
start: node.start, start: node.start,
end: node.end end: node.end
}); });
}
})); }));
if (count) {
for (var i = 0, len = this.body.length; i < len; i++) { for (var i = 0, len = this.body.length; i < len; i++) {
var stat = this.body[i]; var stat = this.body[i];
if (stat instanceof AST_Var) { if (stat instanceof AST_Var) {
@ -569,6 +577,12 @@ AST_Scope.DEFMETHOD("group_voids", function(options) {
start: this.start, start: this.start,
end: this.end end: this.end
})); }));
function is_undefined(node) {
return node instanceof AST_Undefined
|| node instanceof AST_UnaryPrefix
&& node.operator == "void"
&& node.expression.is_constant();
} }
function make_var_def(node) { function make_var_def(node) {

View File

@ -26,13 +26,13 @@ group_voids: {
console.log(void 0); console.log(void 0);
for (var a = 4;;); for (var a = 4;;);
var b = 4; var b = 4;
function f5() { function g() {
var c = 5; var c = 5;
var d = 5; var d = 5;
console.log(void 0); console.log(void 0);
} }
} }
function f6() { function f5() {
try { try {
var a = 6; var a = 6;
console.log(void 0); console.log(void 0);
@ -47,35 +47,35 @@ group_voids: {
if (void 0 === b) if (void 0 === b)
c = void 0; c = void 0;
function f1() { function f1() {
var o = 1, a; var o = 1, n;
console.log(a); console.log(n);
} }
function f2(o) { function f2(o) {
var n = 2, a; var n = 2, v;
console.log(a); console.log(v);
} }
function f3() { function f3() {
var o = 3, a; var o = 3, n;
console.log(a); console.log(n);
} }
function f4() { function f4() {
console.log(a); console.log(i);
for(var o = 4;;); for(var o = 4;;);
var n = 4, a; var n = 4, i;
function v() { function v() {
var o = 5; var o = 5;
var n = 5; var n = 5;
console.log(a); console.log(i);
} }
} }
function f6() { function f5() {
try { try {
var o = 6; var o = 6;
console.log(a); console.log(n);
} catch (o) { } catch (o) {
console.log(a); console.log(n);
} }
var a; var n;
} }
} }
} }
@ -108,13 +108,13 @@ group_voids_toplevel: {
console.log(void 0); console.log(void 0);
for (var a = 4;;); for (var a = 4;;);
var b = 4; var b = 4;
function f5() { function g() {
var c = 5; var c = 5;
var d = 5; var d = 5;
console.log(void 0); console.log(void 0);
} }
} }
function f6() { function f5() {
try { try {
var a = 6; var a = 6;
console.log(void 0); console.log(void 0);
@ -168,8 +168,7 @@ group_voids_catch: {
group_voids: true, group_voids: true,
} }
input: { input: {
f(); (function() {
function f() {
var a = 1; var a = 1;
console.log(void 0); console.log(void 0);
try { try {
@ -177,19 +176,18 @@ group_voids_catch: {
} catch (undefined) { } catch (undefined) {
console.log(void 0); console.log(void 0);
} }
} })();
} }
expect: { expect: {
f(); (function() {
function f() { var o = 1, c;
var o = 1, a; console.log(c);
console.log(a);
try { try {
throw "FAIL"; throw "FAIL";
} catch (o) { } catch (o) {
console.log(a); console.log(c);
}
} }
})();
} }
expect_stdout: [ expect_stdout: [
"undefined", "undefined",