Skip to content

Commit

Permalink
improve Dictionary performance (#5202)
Browse files Browse the repository at this point in the history
- workaround `__proto__` quirks on v8
  • Loading branch information
alexlamsl committed Dec 5, 2021
1 parent 860aa95 commit b079910
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 221 deletions.
2 changes: 1 addition & 1 deletion bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ function read_file(path, default_value) {
}

function parse_js(value, options, flag) {
if (!options || typeof options != "object") options = {};
if (!options || typeof options != "object") options = Object.create(null);
if (typeof value == "string") try {
UglifyJS.parse(value, {
expression: true
Expand Down
297 changes: 151 additions & 146 deletions lib/compress.js

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions lib/propmangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"use strict";

var builtins = function() {
var names = [];
var names = new Dictionary();
// NaN will be included due to Number.NaN
[
"null",
Expand Down Expand Up @@ -72,10 +72,10 @@ var builtins = function() {
Object.getOwnPropertyNames(ctor.prototype).map(add);
}
});
return makePredicate(names);
return names;

function add(name) {
names.push(name);
names.set(name, true);
}
}();

Expand Down Expand Up @@ -116,17 +116,17 @@ function mangle_properties(ast, options) {
reserved: null,
}, true);

var reserved = Object.create(options.builtins ? null : builtins);
var reserved = options.builtins ? new Dictionary() : builtins.clone();
if (Array.isArray(options.reserved)) options.reserved.forEach(function(name) {
reserved[name] = true;
reserved.set(name, true);
});

var cname = -1;
var cache;
if (options.cache) {
cache = options.cache.props;
cache.each(function(name) {
reserved[name] = true;
reserved.set(name, true);
});
} else {
cache = new Dictionary();
Expand All @@ -141,8 +141,8 @@ function mangle_properties(ast, options) {
var debug_suffix;
if (debug) debug_suffix = options.debug === true ? "" : options.debug;

var names_to_mangle = Object.create(null);
var unmangleable = Object.create(reserved);
var names_to_mangle = new Dictionary();
var unmangleable = reserved.clone();

// step 1: find candidates to mangle
ast.walk(new TreeWalker(function(node) {
Expand Down Expand Up @@ -211,20 +211,20 @@ function mangle_properties(ast, options) {
// only function declarations after this line

function can_mangle(name) {
if (unmangleable[name]) return false;
if (unmangleable.has(name)) return false;
if (/^-?[0-9]+(\.[0-9]+)?(e[+-][0-9]+)?$/.test(name)) return false;
return true;
}

function should_mangle(name) {
if (reserved[name]) return false;
if (reserved.has(name)) return false;
if (regex && !regex.test(name)) return false;
return cache.has(name) || names_to_mangle[name];
return cache.has(name) || names_to_mangle.has(name);
}

function add(name) {
if (can_mangle(name)) names_to_mangle[name] = true;
if (!should_mangle(name)) unmangleable[name] = true;
if (can_mangle(name)) names_to_mangle.set(name, true);
if (!should_mangle(name)) unmangleable.set(name, true);
}

function mangle(name) {
Expand Down
31 changes: 15 additions & 16 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,12 @@ function names_in_use(scope, options) {
if (!names) {
scope.cname = -1;
scope.cname_holes = [];
scope.names_in_use = names = Object.create(null);
scope.names_in_use = names = new Dictionary();
var cache = options.cache && options.cache.props;
scope.enclosed.forEach(function(def) {
if (def.unmangleable(options)) names[def.name] = true;
if (def.unmangleable(options)) names.set(def.name, true);
if (def.global && cache && cache.has(def.name)) {
names[cache.get(def.name)] = true;
names.set(cache.get(def.name), true);
}
});
}
Expand All @@ -526,34 +526,33 @@ function next_mangled_name(def, options) {
var scope = def.scope;
var in_use = names_in_use(scope, options);
var holes = scope.cname_holes;
var names = Object.create(null);
var names = new Dictionary();
var scopes = [ scope ];
def.forEach(function(sym) {
var scope = sym.scope;
do {
if (scopes.indexOf(scope) < 0) {
for (var name in names_in_use(scope, options)) {
names[name] = true;
}
scopes.push(scope);
} else break;
if (member(scope, scopes)) break;
names_in_use(scope, options).each(function(marker, name) {
names.set(name, marker);
});
scopes.push(scope);
} while (scope = scope.parent_scope);
});
var name;
for (var i = 0; i < holes.length; i++) {
name = base54(holes[i]);
if (names[name]) continue;
if (names.has(name)) continue;
holes.splice(i, 1);
in_use[name] = true;
in_use.set(name, true);
return name;
}
while (true) {
name = base54(++scope.cname);
if (in_use[name] || RESERVED_WORDS[name] || options.reserved.has[name]) continue;
if (!names[name]) break;
if (in_use.has(name) || RESERVED_WORDS[name] || options.reserved.has[name]) continue;
if (!names.has(name)) break;
holes.push(scope.cname);
}
in_use[name] = true;
in_use.set(name, true);
return name;
}

Expand Down Expand Up @@ -598,7 +597,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
if (options.cache && options.cache.props) {
var mangled_names = names_in_use(this, options);
options.cache.props.each(function(mangled_name) {
mangled_names[mangled_name] = true;
mangled_names.set(mangled_name, true);
});
}

Expand Down
20 changes: 11 additions & 9 deletions lib/sourcemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,23 @@ function vlq_encode(num) {
}

function create_array_map() {
var map = Object.create(null);
var map = new Dictionary();
var array = [];
array.index = function(name) {
if (!HOP(map, name)) {
map[name] = array.length;
var index = map.get(name);
if (!(index >= 0)) {
index = array.length;
array.push(name);
map.set(name, index);
}
return map[name];
return index;
};
return array;
}

function SourceMap(options) {
var sources = create_array_map();
var sources_content = options.includeSources && Object.create(null);
var sources_content = options.includeSources && new Dictionary();
var names = create_array_map();
var mappings = "";
if (options.orig) Object.keys(options.orig).forEach(function(name) {
Expand All @@ -110,7 +112,7 @@ function SourceMap(options) {
if (!sources_content || !map.sourcesContent) return;
for (var i = 0; i < map.sources.length; i++) {
var content = map.sourcesContent[i];
if (content) sources_content[map.sources[i]] = content;
if (content) sources_content.set(map.sources[i], content);
}
});
var prev_source;
Expand Down Expand Up @@ -144,8 +146,8 @@ function SourceMap(options) {
add(source, gen_line, gen_col, orig_line, orig_col, name);
} : add,
setSourceContent: sources_content ? function(source, content) {
if (!(source in sources_content)) {
sources_content[source] = content;
if (!sources_content.has(source)) {
sources_content.set(source, content);
}
} : noop,
toString: function() {
Expand All @@ -155,7 +157,7 @@ function SourceMap(options) {
sourceRoot: options.root || undefined,
sources: sources,
sourcesContent: sources_content ? sources.map(function(source) {
return sources_content[source] || null;
return sources_content.get(source) || null;
}) : undefined,
names: names,
mappings: mappings,
Expand Down
74 changes: 41 additions & 33 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,6 @@ function defaults(args, defs, croak) {
return defs;
}

function merge(obj, ext) {
var count = 0;
for (var i in ext) if (HOP(ext, i)) {
obj[i] = ext[i];
count++;
}
return count;
}

function noop() {}
function return_false() { return false; }
function return_true() { return true; }
Expand Down Expand Up @@ -171,63 +162,80 @@ function all(array, predicate) {
}

function Dictionary() {
this._values = Object.create(null);
this._size = 0;
this.values = Object.create(null);
}
Dictionary.prototype = {
set: function(key, val) {
if (!this.has(key)) ++this._size;
this._values["$" + key] = val;
if (key == "__proto__") {
this.proto_value = val;
} else {
this.values[key] = val;
}
return this;
},
add: function(key, val) {
if (this.has(key)) {
this.get(key).push(val);
var list = this.get(key);
if (list) {
list.push(val);
} else {
this.set(key, [ val ]);
}
return this;
},
get: function(key) { return this._values["$" + key] },
get: function(key) {
return key == "__proto__" ? this.proto_value : this.values[key];
},
del: function(key) {
if (this.has(key)) {
--this._size;
delete this._values["$" + key];
if (key == "__proto__") {
delete this.proto_value;
} else {
delete this.values[key];
}
return this;
},
has: function(key) { return ("$" + key) in this._values },
has: function(key) {
return key == "__proto__" ? "proto_value" in this : key in this.values;
},
all: function(predicate) {
for (var i in this._values)
if (!predicate(this._values[i], i.substr(1)))
return false;
for (var i in this.values)
if (!predicate(this.values[i], i)) return false;
if ("proto_value" in this && !predicate(this.proto_value, "__proto__")) return false;
return true;
},
each: function(f) {
for (var i in this._values)
f(this._values[i], i.substr(1));
for (var i in this.values)
f(this.values[i], i);
if ("proto_value" in this) f(this.proto_value, "__proto__");
},
size: function() {
return this._size;
return Object.keys(this.values).length + ("proto_value" in this);
},
map: function(f) {
var ret = [];
for (var i in this._values)
ret.push(f(this._values[i], i.substr(1)));
for (var i in this.values)
ret.push(f(this.values[i], i));
if ("proto_value" in this) ret.push(f(this.proto_value, "__proto__"));
return ret;
},
clone: function() {
var ret = new Dictionary();
for (var i in this._values)
ret._values[i] = this._values[i];
ret._size = this._size;
this.each(function(value, i) {
ret.set(i, value);
});
return ret;
},
toObject: function() { return this._values }
toObject: function() {
var obj = {};
this.each(function(value, i) {
obj["$" + i] = value;
});
return obj;
},
};
Dictionary.fromObject = function(obj) {
var dict = new Dictionary();
dict._size = merge(dict._values, obj);
for (var i in obj)
if (HOP(obj, i)) dict.set(i.slice(1), obj[i]);
return dict;
};

Expand Down
22 changes: 21 additions & 1 deletion test/compress/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3203,7 +3203,7 @@ issue_4552: {
expect_stdout: "NaN"
}

issue_4886: {
issue_4886_1: {
options = {
evaluate: true,
unsafe: true,
Expand All @@ -3222,3 +3222,23 @@ issue_4886: {
}
expect_stdout: "true"
}

issue_4886_2: {
options = {
evaluate: true,
unsafe: true,
}
input: {
console.log("foo" in {
"foo": null,
__proto__: 42,
});
}
expect: {
console.log("foo" in {
"foo": null,
__proto__: 42,
});
}
expect_stdout: "true"
}
4 changes: 2 additions & 2 deletions test/compress/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ numeric_literal: {
expect_exact: [
'var obj = {',
' 0: 0,',
' "-0": 1,',
' 42: 3,',
' 37: 4,',
' 42: 3,',
' "-0": 1,',
' o: 5,',
' 1e42: 8,',
' b: 7',
Expand Down

0 comments on commit b079910

Please sign in to comment.