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

Loading…
Cancel
Save