improve mangle
Assign shorter names to symbols with higher frequency of occurrence.
This commit is contained in:
parent
bd7be07c38
commit
f4d7f31a58
32
lib/scope.js
32
lib/scope.js
|
|
@ -415,14 +415,10 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
|||
var to_mangle = [];
|
||||
|
||||
if (options.cache) {
|
||||
this.globals.each(function(symbol){
|
||||
if (options.reserved.indexOf(symbol.name) < 0) {
|
||||
to_mangle.push(symbol);
|
||||
}
|
||||
});
|
||||
push_symbols(this.globals);
|
||||
}
|
||||
|
||||
var tw = new TreeWalker(function(node, descend){
|
||||
this.walk(new TreeWalker(function(node, descend) {
|
||||
if (node instanceof AST_LabeledStatement) {
|
||||
// lname is incremented when we get to the AST_Label
|
||||
var save_nesting = lname;
|
||||
|
|
@ -431,13 +427,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
|||
return true; // don't descend again in TreeWalker
|
||||
}
|
||||
if (node instanceof AST_Scope) {
|
||||
var p = tw.parent(), a = [];
|
||||
node.variables.each(function(symbol){
|
||||
if (options.reserved.indexOf(symbol.name) < 0) {
|
||||
a.push(symbol);
|
||||
}
|
||||
});
|
||||
to_mangle.push.apply(to_mangle, a);
|
||||
push_symbols(node.variables);
|
||||
return;
|
||||
}
|
||||
if (node instanceof AST_Label) {
|
||||
|
|
@ -450,13 +440,25 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
|||
to_mangle.push(node.definition());
|
||||
return;
|
||||
}
|
||||
});
|
||||
this.walk(tw);
|
||||
}));
|
||||
to_mangle.forEach(function(def){ def.mangle(options) });
|
||||
|
||||
if (options.cache) {
|
||||
options.cache.cname = this.cname;
|
||||
}
|
||||
|
||||
function push_symbols(dict) {
|
||||
var a = [];
|
||||
dict.each(function(symbol) {
|
||||
if (options.reserved.indexOf(symbol.name) < 0) {
|
||||
a.push(symbol);
|
||||
}
|
||||
});
|
||||
a.sort(function(m, n) {
|
||||
return n.references.length - m.references.length;
|
||||
});
|
||||
to_mangle.push.apply(to_mangle, a);
|
||||
}
|
||||
});
|
||||
|
||||
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){
|
||||
|
|
|
|||
|
|
@ -265,7 +265,7 @@ issue_203: {
|
|||
}
|
||||
expect: {
|
||||
var m = {};
|
||||
var fn = Function("n,o", "o.exports=42");
|
||||
var fn = Function("o,n", "n.exports=42");
|
||||
fn(null, m, m.exports);
|
||||
console.log(m.exports);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,8 +17,8 @@ mangle_keep_fnames_false: {
|
|||
expect: {
|
||||
"use strict";
|
||||
function total() {
|
||||
return function t(n, r, u) {
|
||||
return n + r + u;
|
||||
return function c(t, r, u) {
|
||||
return t + r + u;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,5 +71,5 @@ undefined_redefined_mangle: {
|
|||
return typeof n == "undefined";
|
||||
}
|
||||
}
|
||||
expect_exact: "function f(n){var r=1;return void 0===r}"
|
||||
expect_exact: "function f(r){var n=1;return void 0===n}"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ describe("minify", function() {
|
|||
compressed += result.code;
|
||||
});
|
||||
assert.strictEqual(JSON.stringify(cache).slice(0, 20), '{"cname":5,"props":{');
|
||||
assert.strictEqual(compressed, 'function n(n){return 3*n}function r(n){return n/2}function c(o){l("Foo:",2*o)}var l=console.log.bind(console);var f=n(3),i=r(12);l("qux",f,i),c(11);');
|
||||
assert.strictEqual(compressed, 'function n(n){return 3*n}function r(n){return n/2}function l(o){c("Foo:",2*o)}var c=console.log.bind(console);var f=n(3),i=r(12);c("qux",f,i),l(11);');
|
||||
assert.strictEqual(run_code(compressed), run_code(original));
|
||||
});
|
||||
|
||||
|
|
@ -69,7 +69,7 @@ describe("minify", function() {
|
|||
compressed += result.code;
|
||||
});
|
||||
assert.strictEqual(JSON.stringify(cache).slice(0, 28), '{"vars":{"cname":5,"props":{');
|
||||
assert.strictEqual(compressed, 'function n(n){return 3*n}function r(n){return n/2}function c(o){l("Foo:",2*o)}var l=console.log.bind(console);var f=n(3),i=r(12);l("qux",f,i),c(11);');
|
||||
assert.strictEqual(compressed, 'function n(n){return 3*n}function r(n){return n/2}function l(o){c("Foo:",2*o)}var c=console.log.bind(console);var f=n(3),i=r(12);c("qux",f,i),l(11);');
|
||||
assert.strictEqual(run_code(compressed), run_code(original));
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user