perf($parse): Inline constants

Inline constants definitions in function calls, array definitions
and object values.

For the expression [1, {foo: "bar"}, 1 + 2] it changes it from

```js
// After some reordering and cleanup
var v1 = 1;
var v2 = "bar";
var v3 = {foo: v2};
var v4 = 1;
var v5 = 2;
var v6 = plus(v4, v5);
var v7 = [v1, v3, v6];
return v7;
```

to

```js
return [1, {foo: "bar"}, plus(1, 2)];
```

Expression parts that are not constants did not change, and still generate a lot
of intermediate variables.

Closes: #14293
This commit is contained in:
Lucas Mirelmann
2016-03-21 20:57:21 +01:00
parent 3cd00fa3dd
commit bd7d5f6345
+9 -9
View File
@@ -924,7 +924,7 @@ ASTCompiler.prototype = {
case AST.Literal:
expression = this.escape(ast.value);
this.assign(intoId, expression);
recursionFn(expression);
recursionFn(intoId || expression);
break;
case AST.UnaryExpression:
this.recurse(ast.argument, undefined, undefined, function(expr) { right = expr; });
@@ -1046,7 +1046,7 @@ ASTCompiler.prototype = {
self.if_(self.notNull(right), function() {
self.addEnsureSafeFunction(right);
forEach(ast.arguments, function(expr) {
self.recurse(expr, self.nextId(), undefined, function(argument) {
self.recurse(expr, ast.constant ? undefined : self.nextId(), undefined, function(argument) {
args.push(self.ensureSafeObject(argument));
});
});
@@ -1087,18 +1087,18 @@ ASTCompiler.prototype = {
case AST.ArrayExpression:
args = [];
forEach(ast.elements, function(expr) {
self.recurse(expr, self.nextId(), undefined, function(argument) {
self.recurse(expr, ast.constant ? undefined : self.nextId(), undefined, function(argument) {
args.push(argument);
});
});
expression = '[' + args.join(',') + ']';
this.assign(intoId, expression);
recursionFn(expression);
recursionFn(intoId || expression);
break;
case AST.ObjectExpression:
args = [];
forEach(ast.properties, function(property) {
self.recurse(property.value, self.nextId(), undefined, function(expr) {
self.recurse(property.value, ast.constant ? undefined : self.nextId(), undefined, function(expr) {
args.push(self.escape(
property.key.type === AST.Identifier ? property.key.name :
('' + property.key.value)) +
@@ -1107,19 +1107,19 @@ ASTCompiler.prototype = {
});
expression = '{' + args.join(',') + '}';
this.assign(intoId, expression);
recursionFn(expression);
recursionFn(intoId || expression);
break;
case AST.ThisExpression:
this.assign(intoId, 's');
recursionFn('s');
recursionFn(intoId || 's');
break;
case AST.LocalsExpression:
this.assign(intoId, 'l');
recursionFn('l');
recursionFn(intoId || 'l');
break;
case AST.NGValueParameter:
this.assign(intoId, 'v');
recursionFn('v');
recursionFn(intoId || 'v');
break;
}
},