Skip to content

Commit

Permalink
enhance inline (#3832)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Apr 30, 2020
1 parent d900006 commit f80d5b8
Show file tree
Hide file tree
Showing 3 changed files with 582 additions and 21 deletions.
36 changes: 34 additions & 2 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ merge(Compressor.prototype, {
// output and performance.
descend(node, this);
var opt = node.optimize(this);
if (is_scope) {
if (is_scope && opt === node) {
opt.drop_unused(this);
descend(opt, this);
}
Expand Down Expand Up @@ -3981,6 +3981,38 @@ merge(Compressor.prototype, {
return self;
});

OPT(AST_Function, function(self, compressor) {
self.body = tighten_body(self.body, compressor);
if (compressor.option("inline")) for (var i = 0; i < self.body.length; i++) {
var stat = self.body[i];
if (stat instanceof AST_Directive) continue;
if (stat instanceof AST_Return) {
var call = stat.value;
if (!call || call.TYPE != "Call") break;
var fn = call.expression;
if (fn instanceof AST_SymbolRef) {
fn = fn.fixed_value();
}
if (!(fn instanceof AST_Lambda)) break;
if (fn.uses_arguments) break;
if (fn.contains_this()) break;
var j = fn.argnames.length;
if (j > 0 && compressor.option("inline") < 2) break;
if (j > self.argnames.length) break;
if (j < self.argnames.length && !compressor.drop_fargs(fn, call)) break;
while (--j >= 0) {
var arg = call.args[j];
if (!(arg instanceof AST_SymbolRef)) break;
if (arg.definition() !== self.argnames[j].definition()) break;
}
if (j >= 0) break;
return call.expression;
}
break;
}
return self;
});

AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
if (!compressor.option("unused")) return;
if (compressor.has_directive("use asm")) return;
Expand Down Expand Up @@ -6133,7 +6165,7 @@ merge(Compressor.prototype, {

function can_substitute_directly() {
if (var_assigned) return;
if (compressor.option("inline") <= 1 && fn.argnames.length) return;
if (compressor.option("inline") < 2 && fn.argnames.length) return;
if (!fn.variables.all(function(def) {
return def.references.length < 2 && def.orig[0] instanceof AST_SymbolFunarg;
})) return;
Expand Down

0 comments on commit f80d5b8

Please sign in to comment.