From 24d38f74b93aa1d2947a158a43cac617190f12fe Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 11 Apr 2010 16:46:45 +0200 Subject: [PATCH] Replaced two-level rule cache with a one-level one. This leads to simpler code and also speeds up the benchmark suite execution by 5,89 % on V8. Detailed results (benchmark suite totals): --------------------------------- Test # Before After --------------------------------- 1 24,70 kB/s 26,14 kB/s 2 24,49 kB/s 26,05 kB/s 3 24,67 kB/s 25,99 kB/s 4 24,65 kB/s 26,13 kB/s 5 24,71 kB/s 26,14 kB/s --------------------------------- Average 24,64 kB/s 26.10 kB/s --------------------------------- Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.7 Safari/533.2 --- lib/compiler.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index eb73fd0..511e56a 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -394,8 +394,8 @@ PEG.Grammar.Rule.prototype.compile = function() { return PEG.Compiler.formatCode( "result._parse_${name} = function(context) {", - " this._cache[${name|string}] = this._cache[${name|string}] || [];", - " var cachedResult = this._cache[${name|string}][this._pos];", + " var cacheKey = ${name|string} + '@' + this._pos;", + " var cachedResult = this._cache[cacheKey];", " if (cachedResult !== undefined) {", " this._pos = cachedResult.nextPos;", " return cachedResult.result;", @@ -408,7 +408,7 @@ PEG.Grammar.Rule.prototype.compile = function() { " ${restoreReportMatchFailuresCode}", " ${reportMatchFailureCode}", " ", - " this._cache[${name|string}][pos] = {", + " this._cache[cacheKey] = {", " nextPos: this._pos,", " result: ${resultVar}", " };",