Avoid |call| when calling actions with one parameter.

This speeds up the benchmark suite execution by 0.18%, which may just be a
measurement error. (Standrad statistic tests would tell more, but I don't want
to mess with them now.) The code is little bit nicer this way though.

Going further and avoiding |apply| seems to slow thigs down a bit, possibly
because multiple array accesses. I may try improved version without array
accesses (where Action passes the Sequence variable names to save the results
into) sometime later.

Detailed results (benchmark suite totals):

---------------------------------
 Test #     Before       After
---------------------------------
      1   29.08 kB/s   28.91 kB/s
      2   28.72 kB/s   28.75 kB/s
      3   28.78 kB/s   28.88 kB/s
      4   28.57 kB/s   28.90 kB/s
      5   28.84 kB/s   28.81 kB/s
---------------------------------
Average   28.80 kB/s   28.85 kB/s
---------------------------------

Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.9 Safari/533.2
redux
David Majda 14 years ago
parent e3aa4df090
commit cc7f1d96eb

@ -1020,37 +1020,39 @@ PEG.Grammar.Action.prototype.compile = function(resultVar) {
var expressionResultVar = PEG.Compiler.generateUniqueIdentifier("result");
var paramCount = this._expression instanceof PEG.Grammar.Sequence
? this._expression.getElements().length
: 1;
var params = PEG.ArrayUtils.map(
PEG.ArrayUtils.range(1, paramCount + 1),
function(n) { return "$" + n; }
).join(", ");
var actionFunction = PEG.Compiler.formatCode(
"function(${params}) { ${action} }",
{
params: params,
action: this._action
}
);
var invokeFunctionName = this._expression instanceof PEG.Grammar.Sequence
? "apply"
: "call";
if (this._expression instanceof PEG.Grammar.Sequence) {
var params = PEG.ArrayUtils.map(
PEG.ArrayUtils.range(1, this._expression.getElements().length + 1),
function(n) { return "$" + n; }
).join(", ");
var invocationCode = PEG.Compiler.formatCode(
"(function(${params}) { ${action} }).apply(null, ${expressionResultVar})",
{
params: params,
action: this._action,
expressionResultVar: expressionResultVar
}
);
} else {
var invocationCode = PEG.Compiler.formatCode(
"(function($1) { ${action} })(${expressionResultVar})",
{
action: this._action,
expressionResultVar: expressionResultVar
}
);
}
return PEG.Compiler.formatCode(
"${expressionCode}",
"var ${resultVar} = ${expressionResultVar} !== null",
" ? (${actionFunction}).${invokeFunctionName}(this, ${expressionResultVar})",
" ? ${invocationCode}",
" : null;",
{
expressionCode: this._expression.compile(expressionResultVar),
expressionResultVar: expressionResultVar,
actionFunction: actionFunction,
invokeFunctionName: invokeFunctionName,
invocationCode: invocationCode,
resultVar: resultVar
}
);

Loading…
Cancel
Save