diff --git a/lib/scope.js b/lib/scope.js index a0b7c648..17665e88 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -586,32 +586,30 @@ function _default_mangler_options(options) { webkit : false, }); if (!Array.isArray(options.reserved)) options.reserved = []; - // Never mangle arguments + // Never mangle `arguments` push_uniq(options.reserved, "arguments"); options.reserved.has = makePredicate(options.reserved); return options; } +// We only need to mangle declaration nodes. Special logic wired into the code +// generator will display the mangled name if it is present (and for +// `AST_SymbolRef`s it will use the mangled name of the `AST_SymbolDeclaration` +// that it points to). AST_Toplevel.DEFMETHOD("mangle_names", function(options) { options = _default_mangler_options(options); - - // We only need to mangle declaration nodes. Special logic wired - // into the code generator will display the mangled name if it's - // present (and for AST_SymbolRef-s it'll use the mangled name of - // the AST_SymbolDeclaration that it points to). - var lname = -1; - if (options.cache && options.cache.props) { var mangled_names = names_in_use(this, options); options.cache.props.each(function(mangled_name) { mangled_names.set(mangled_name, true); }); } - + var cutoff = 10; + var lname = -1; var redefined = []; var tw = new TreeWalker(function(node, descend) { if (node instanceof AST_LabeledStatement) { - // lname is incremented when we get to the AST_Label + // `lname` is incremented when we get to the `AST_Label` var save_nesting = lname; descend(); if (!options.v8 || !in_label(tw)) lname = save_nesting; @@ -633,9 +631,9 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) { }); }, true); } - node.to_mangle = []; + var to_mangle = node.to_mangle = []; node.variables.each(function(def) { - if (!defer_redef(def)) node.to_mangle.push(def); + if (!defer_redef(def)) to_mangle.push(def); }); descend(); if (options.cache && node instanceof AST_Toplevel) { @@ -646,7 +644,23 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) { sym.scope = node; sym.reference(options); } - node.to_mangle.forEach(mangle); + if (to_mangle.length > cutoff) { + var indices = to_mangle.map(function(def, index) { + return index; + }).sort(function(i, j) { + return to_mangle[j].references.length - to_mangle[i].references.length || i - j; + }); + to_mangle = indices.slice(0, cutoff).sort(function(i, j) { + return i - j; + }).map(function(index) { + return to_mangle[index]; + }).concat(indices.slice(cutoff).sort(function(i, j) { + return i - j; + }).map(function(index) { + return to_mangle[index]; + })); + } + to_mangle.forEach(mangle); return true; } if (node instanceof AST_Label) {