Skip to content

Commit

Permalink
Add a memoize to makeLineColumnIndex for large input scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
matthemsteger committed Jul 4, 2020
1 parent ce035ee commit fd08950
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/parsimmon.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ function mergeReplies(result, last) {
};
}

// Hold a simple memoize for the last value
var lastLineColumnIndex = {};
function makeLineColumnIndex(input, i) {
if (isBuffer(input)) {
return {
Expand All @@ -363,16 +365,25 @@ function makeLineColumnIndex(input, i) {
column: -1
};
}
// if we are calling this function with the same arguments as last time
// return the memoized value to prevent expensive processing below
if (lastLineColumnIndex.input === input && lastLineColumnIndex.i === i) {
return lastLineColumnIndex.value;
}
var lines = input.slice(0, i).split("\n");
// Note that unlike the character offset, the line and column offsets are
// 1-based.
var lineWeAreUpTo = lines.length;
var columnWeAreUpTo = lines[lines.length - 1].length + 1;
return {
var value = {
offset: i,
line: lineWeAreUpTo,
column: columnWeAreUpTo
};
lastLineColumnIndex.input = input;
lastLineColumnIndex.i = i;
lastLineColumnIndex.value = value;
return value;
}

// Returns the sorted set union of two arrays of strings
Expand Down

0 comments on commit fd08950

Please sign in to comment.