Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a memoize to makeLineColumnIndex for large input scenarios #297

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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