Skip to content

Commit

Permalink
perf(parser): binary search for comments in range
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Somogyvari <peter.metz@unarin.com>
  • Loading branch information
petermetz committed Oct 28, 2020
1 parent a846b66 commit bae319a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
14 changes: 11 additions & 3 deletions lib/javascript/JavascriptParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { SyncBailHook, HookMap } = require("tapable");
const vm = require("vm");
const Parser = require("../Parser");
const StackedMap = require("../util/StackedMap");
const binarySearchBounds = require("../util/binarySearchBounds");
const memorize = require("../util/memorize");
const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");

Expand Down Expand Up @@ -3327,9 +3328,16 @@ class JavascriptParser extends Parser {
}

getComments(range) {
return this.comments.filter(
comment => comment.range[0] >= range[0] && comment.range[1] <= range[1]
);
const [rangeStart, rangeEnd] = range;
const compare = (comment, needle) => comment.range[0] - needle;
let idx = binarySearchBounds.ge(this.comments, rangeStart, compare);
let commentsInRange = [];
while (this.comments[idx] && this.comments[idx].range[1] <= rangeEnd) {
commentsInRange.push(this.comments[idx]);
idx++;
}

return commentsInRange;
}

/**
Expand Down
86 changes: 86 additions & 0 deletions lib/util/binarySearchBounds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Mikola Lysenko @mikolalysenko
*/

"use strict";

/* cspell:disable-next-line */
// Refactor: Peter Somogyvari @petermetz

const compileSearch = (funcName, predicate, reversed, extraArgs, earlyOut) => {
const code = [
"function ",
funcName,
"(a,l,h,",
extraArgs.join(","),
"){",
earlyOut ? "" : "var i=",
reversed ? "l-1" : "h+1",
";while(l<=h){var m=(l+h)>>>1,x=a[m]"
];

if (earlyOut) {
if (predicate.indexOf("c") < 0) {
code.push(";if(x===y){return m}else if(x<=y){");
} else {
code.push(";var p=c(x,y);if(p===0){return m}else if(p<=0){");
}
} else {
code.push(";if(", predicate, "){i=m;");
}
if (reversed) {
code.push("l=m+1}else{h=m-1}");
} else {
code.push("h=m-1}else{l=m+1}");
}
code.push("}");
if (earlyOut) {
code.push("return -1};");
} else {
code.push("return i};");
}
return code.join("");
};

const compileBoundsSearch = (predicate, reversed, suffix, earlyOut) => {
const arg1 = compileSearch(
"A",
"x" + predicate + "y",
reversed,
["y"],
earlyOut
);

const arg2 = compileSearch(
"P",
"c(x,y)" + predicate + "0",
reversed,
["y", "c"],
earlyOut
);

const fnHeader = "function dispatchBinarySearch";

const fnBody =
"(a,y,c,l,h){\
if(typeof(c)==='function'){\
return P(a,(l===void 0)?0:l|0,(h===void 0)?a.length-1:h|0,y,c)\
}else{\
return A(a,(c===void 0)?0:c|0,(l===void 0)?a.length-1:l|0,y)\
}}\
return dispatchBinarySearch";

const fnArgList = [arg1, arg2, fnHeader, suffix, fnBody, suffix];
const fnSource = fnArgList.join("");
const result = new Function(fnSource);
return result();
};

module.exports = {
ge: compileBoundsSearch(">=", false, "GE"),
gt: compileBoundsSearch(">", false, "GT"),
lt: compileBoundsSearch("<", true, "LT"),
le: compileBoundsSearch("<=", true, "LE"),
eq: compileBoundsSearch("-", true, "EQ", true)
};
2 changes: 1 addition & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4319,7 +4319,7 @@ declare class JavascriptParser extends Parser {
commentsStartPos: number,
args?: any
): boolean;
getComments(range?: any): any;
getComments(range?: any): any[];
isAsiPosition(pos: number): boolean;
isStatementLevelExpression(expr?: any): boolean;
getTagData(name?: any, tag?: any): any;
Expand Down

0 comments on commit bae319a

Please sign in to comment.