Skip to content

Commit ed9859f

Browse files
committed
perf(javascript): use binary search for attached comments
1 parent 1043489 commit ed9859f

3 files changed

Lines changed: 87 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Speed up attached comment lookup during JavaScript analysis.

‎lib/javascript/JavascriptParser.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const NormalModule = require("../NormalModule");
1010
const Parser = require("../Parser");
1111
const WebpackError = require("../WebpackError");
1212
const StackedMap = require("../util/StackedMap");
13+
const binarySearchBounds = require("../util/binarySearchBounds");
1314
const {
1415
CompilerHintNotationRegExp,
1516
createMagicCommentContext,
@@ -1112,6 +1113,14 @@ const collectBlockDeclarations = (statements) => {
11121113
return overflow === undefined ? mask : overflow;
11131114
};
11141115

1116+
/**
1117+
* @param {Comment} comment comment to compare
1118+
* @param {number} position source offset
1119+
* @returns {number} comparison of the comment end with the offset
1120+
*/
1121+
const compareCommentEnd = (comment, position) =>
1122+
/** @type {Range} */ (comment.range)[1] - position;
1123+
11151124
/**
11161125
* Whether the source between two offsets is whitespace only.
11171126
* @param {string} source the module source
@@ -6672,7 +6681,11 @@ class JavascriptParser extends Parser {
66726681
const comments = this.comments;
66736682
if (comments === undefined) return position;
66746683
const source = this._source;
6675-
for (let index = comments.length - 1; index >= 0; index--) {
6684+
for (
6685+
let index = binarySearchBounds.le(comments, position, compareCommentEnd);
6686+
index >= 0;
6687+
index--
6688+
) {
66766689
const range = /** @type {Range} */ (comments[index].range);
66776690
if (range[1] > position) continue;
66786691
// with no source to read, nothing can be ruled out, so the run is

‎test/unitCases/JavascriptParser.unittest.js‎

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,21 +1895,31 @@ class WithStatic { static { const inStaticBlock = 20; } }
18951895
/**
18961896
* @param {string} source what to parse
18971897
* @param {number} index which statement to ask about
1898+
* @param {boolean} sourceAvailable whether the original source is available
18981899
* @returns {string} the source the comment run covers
18991900
*/
1900-
const attachedTo = (source, index) => {
1901+
const attachedTo = (source, index, sourceAvailable = true) => {
19011902
const parser = new JavascriptParser("module");
19021903
const { ast, comments } = JavascriptParser._parse(source, {
19031904
sourceType: "module",
19041905
ranges: true,
19051906
comments: true
19061907
});
1907-
parser.comments = /** @type {EXPECTED_ANY} */ (comments);
1908-
parser._source = source;
1909-
const statement = /** @type {EXPECTED_ANY} */ (ast).body[index];
1908+
parser.comments = comments;
1909+
parser._source = sourceAvailable ? source : undefined;
1910+
const statement = ast.body[index];
1911+
const target =
1912+
(statement.type === "ExportNamedDeclaration" ||
1913+
statement.type === "ExportDefaultDeclaration") &&
1914+
statement.declaration
1915+
? statement.declaration
1916+
: statement;
1917+
if (target !== statement) {
1918+
parser.statementPath = [statement, target];
1919+
}
19101920
return source.slice(
1911-
parser.getAttachedCommentsStart(statement),
1912-
statement.range[0]
1921+
parser.getAttachedCommentsStart(target),
1922+
/** @type {[number, number]} */ (statement.range)[0]
19131923
);
19141924
};
19151925

@@ -1933,6 +1943,58 @@ class WithStatic { static { const inStaticBlock = 20; } }
19331943
it("reads the statement itself when nothing precedes it", () => {
19341944
expect(attachedTo("function f() {}", 0)).toBe("");
19351945
});
1946+
1947+
it("ignores comments inside and after the statement", () => {
1948+
expect(
1949+
attachedTo("/* before */function f() {/* inside */}\n/* after */", 0)
1950+
).toBe("/* before */");
1951+
expect(attachedTo("function f() {/* inside */}\n/* after */", 0)).toBe(
1952+
""
1953+
);
1954+
});
1955+
1956+
it("keeps the conservative comment run without source text", () => {
1957+
expect(
1958+
attachedTo(
1959+
"/* first */first();\n/* second */function f() {}/* after */",
1960+
1,
1961+
false
1962+
)
1963+
).toBe("/* first */first();\n/* second */");
1964+
});
1965+
1966+
it.each(["export function f() {}", "export default function() {}"])(
1967+
"finds comments before %s",
1968+
(declaration) => {
1969+
expect(attachedTo(`/* before */${declaration}/* after */`, 0)).toBe(
1970+
"/* before */"
1971+
);
1972+
}
1973+
);
1974+
1975+
it("does not read every later comment to find an attached run", () => {
1976+
const source = "/* before */function f() {}" + "/* after */".repeat(1024);
1977+
const parser = new JavascriptParser("module");
1978+
const { ast, comments } = JavascriptParser._parse(source, {
1979+
sourceType: "module",
1980+
ranges: true,
1981+
comments: true
1982+
});
1983+
let reads = 0;
1984+
for (const comment of comments) {
1985+
const range = comment.range;
1986+
Object.defineProperty(comment, "range", {
1987+
get() {
1988+
reads++;
1989+
return range;
1990+
}
1991+
});
1992+
}
1993+
parser.comments = comments;
1994+
parser._source = source;
1995+
expect(parser.getAttachedCommentsStart(ast.body[0])).toBe(0);
1996+
expect(reads).toBeLessThan(32);
1997+
});
19361998
});
19371999

19382000
describe("WebpackParser fast paths", () => {

0 commit comments

Comments
 (0)