From cc7f1d96eb6243bbbee0640d54041eef80a13ea2 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 25 Apr 2010 16:50:44 +0200 Subject: [PATCH] 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 --- lib/compiler.js | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 6a14076..ed7bf1d 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -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 } );