Speed up deduplication of expectations

The expectation deduplication algorithm called |Array.prototype.splice|
to eliminate each individual duplication, which was slow. This caused
problems with grammar/input combinations that generated a lot of
expecations (see #377 for an example).

This commit replaces the algorithm with much faster one, eliminating the
problem.
redux
David Majda 9 years ago
parent 972cafbee0
commit d34faba59e

@ -1056,7 +1056,7 @@ function generateJS(ast, options) {
'',
' function peg$buildException(message, expected, location) {',
' function cleanupExpected(expected) {',
' var i = 1;',
' var i, j;',
'',
' expected.sort(function(a, b) {',
' if (a.description < b.description) {',
@ -1073,12 +1073,14 @@ function generateJS(ast, options) {
* expectation object exists only once, so it's enough to use |===| instead
* of deeper structural comparison.
*/
' while (i < expected.length) {',
' if (expected[i - 1] === expected[i]) {',
' expected.splice(i, 1);',
' } else {',
' i++;',
' if (expected.length > 0) {',
' for (i = 1, j = 1; i < expected.length; i++) {',
' if (expected[i - 1] !== expected[i]) {',
' expected[j] = expected[i];',
' j++;',
' }',
' }',
' expected.length = j;',
' }',
' }',
'',

@ -492,7 +492,7 @@ module.exports = (function() {
function peg$buildException(message, expected, location) {
function cleanupExpected(expected) {
var i = 1;
var i, j;
expected.sort(function(a, b) {
if (a.description < b.description) {
@ -504,12 +504,14 @@ module.exports = (function() {
}
});
while (i < expected.length) {
if (expected[i - 1] === expected[i]) {
expected.splice(i, 1);
} else {
i++;
if (expected.length > 0) {
for (i = 1, j = 1; i < expected.length; i++) {
if (expected[i - 1] !== expected[i]) {
expected[j] = expected[i];
j++;
}
}
expected.length = j;
}
}

Loading…
Cancel
Save