improve unused efficiency (#3813)

This commit is contained in:
Alex Lam S.L
2020-04-23 01:31:35 +01:00
committed by GitHub
parent 3ae24329eb
commit 99cf3a38c5
3 changed files with 39 additions and 43 deletions

View File

@@ -3984,16 +3984,6 @@ merge(Compressor.prototype, {
return self;
});
OPT(AST_Lambda, function(self, compressor) {
self.body = tighten_body(self.body, compressor);
if (compressor.option("side_effects")
&& self.body.length == 1
&& self.body[0] === compressor.has_directive("use strict")) {
self.body.length = 0;
}
return self;
});
AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
if (!compressor.option("unused")) return;
if (compressor.has_directive("use asm")) return;
@@ -4153,31 +4143,33 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_Call) calls_to_drop_args.push(node);
if (scope !== self) return;
if (node instanceof AST_Function && node.name && drop_fn_name(node.name.definition())) {
unused_fn_names.push(node);
}
if (node instanceof AST_Lambda && !(node instanceof AST_Accessor)) {
var trim = compressor.drop_fargs(node, parent);
for (var a = node.argnames, i = a.length; --i >= 0;) {
var sym = a[i];
if (!(sym.definition().id in in_use_ids)) {
sym.__unused = true;
if (trim) {
log(sym, "Dropping unused function argument {name} [{file}:{line},{col}]", template(sym));
a.pop();
}
} else {
trim = false;
if (node instanceof AST_Lambda) {
if (drop_funcs && node !== self && node instanceof AST_Defun) {
var def = node.name.definition();
if (!(def.id in in_use_ids)) {
log(node.name, "Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
def.eliminated++;
return in_list ? List.skip : make_node(AST_EmptyStatement, node);
}
}
fns_with_marked_args.push(node);
}
if (drop_funcs && node instanceof AST_Defun && node !== self) {
var def = node.name.definition();
if (!(def.id in in_use_ids)) {
log(node.name, "Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
def.eliminated++;
return make_node(AST_EmptyStatement, node);
if (node instanceof AST_Function && node.name && drop_fn_name(node.name.definition())) {
unused_fn_names.push(node);
}
if (!(node instanceof AST_Accessor)) {
var trim = compressor.drop_fargs(node, parent);
for (var a = node.argnames, i = a.length; --i >= 0;) {
var sym = a[i];
if (!(sym.definition().id in in_use_ids)) {
sym.__unused = true;
if (trim) {
log(sym, "Dropping unused function argument {name} [{file}:{line},{col}]", template(sym));
a.pop();
}
} else {
trim = false;
}
}
fns_with_marked_args.push(node);
}
}
if (node instanceof AST_Definitions && !(parent instanceof AST_ForIn && parent.init === node)) {
@@ -4312,7 +4304,7 @@ merge(Compressor.prototype, {
// Certain combination of unused name + side effect leads to invalid AST:
// https://github.com/mishoo/UglifyJS2/issues/1830
// We fix it at this stage by moving the label inwards, back to the `for`.
descend(node, this);
descend(node, tt);
if (node.body instanceof AST_BlockStatement) {
var block = node.body;
node.body = block.body.pop();
@@ -4324,7 +4316,7 @@ merge(Compressor.prototype, {
if (node instanceof AST_Scope) {
var save_scope = scope;
scope = node;
descend(node, this);
descend(node, tt);
scope = save_scope;
return node;
}
@@ -4376,6 +4368,12 @@ merge(Compressor.prototype, {
});
tt.push(compressor.parent());
self.transform(tt);
if (self instanceof AST_Lambda
&& self.body.length == 1
&& self.body[0] instanceof AST_Directive
&& self.body[0].value == "use strict") {
self.body.length = 0;
}
unused_fn_names.forEach(function(fn) {
fn.name = null;
});