Skip to content

Add text-based fastpaths into LS token matching #41924

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

Merged
merged 6 commits into from
Dec 15, 2020
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
27 changes: 25 additions & 2 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,23 @@ namespace ts {
return isInsideJsxElementTraversal(getTokenAtPosition(sourceFile, position));
}

export function findPrecedingMatchingToken(token: Node, matchingTokenKind: SyntaxKind, sourceFile: SourceFile) {
export function findPrecedingMatchingToken(token: Node, matchingTokenKind: SyntaxKind.OpenBraceToken | SyntaxKind.OpenParenToken | SyntaxKind.OpenBracketToken, sourceFile: SourceFile) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes to this function were able to bring the test runtime from ~36s to ~10s on my machine. Just looking for the likely token position using lastIndexOf gives a great improvement,

const closeTokenText = tokenToString(token.kind)!;
const matchingTokenText = tokenToString(matchingTokenKind)!;
const tokenFullStart = token.getFullStart();
// Text-scan based fast path - can be bamboozled by comments and other trivia, but often provides
// a good, fast approximation without too much extra work in the cases where it fails.
const bestGuessIndex = sourceFile.text.lastIndexOf(matchingTokenText, tokenFullStart);
if (bestGuessIndex === -1) {
return undefined; // if the token text doesn't appear in the file, there can't be a match - super fast bail
}
// we can only use the textual result directly if we didn't have to count any close tokens within the range
if (sourceFile.text.lastIndexOf(closeTokenText, tokenFullStart - 1) < bestGuessIndex) {
const nodeAtGuess = findPrecedingToken(bestGuessIndex + 1, sourceFile);
if (nodeAtGuess && nodeAtGuess.kind === matchingTokenKind) {
return nodeAtGuess;
}
}
const tokenKind = token.kind;
let remainingMatchingTokens = 0;
while (true) {
Expand Down Expand Up @@ -1447,7 +1463,14 @@ namespace ts {
}

// Get info for an expression like `f <` that may be the start of type arguments.
export function getPossibleTypeArgumentsInfo(tokenIn: Node, sourceFile: SourceFile): PossibleTypeArgumentInfo | undefined {
export function getPossibleTypeArgumentsInfo(tokenIn: Node | undefined, sourceFile: SourceFile): PossibleTypeArgumentInfo | undefined {
// This is a rare case, but one that saves on a _lot_ of work if true - if the source file has _no_ `<` character,
// then there obviously can't be any type arguments - no expensive brace-matching backwards scanning required

if (sourceFile.text.lastIndexOf("<", tokenIn ? tokenIn.pos : sourceFile.text.length) === -1) {
return undefined;
}

let token: Node | undefined = tokenIn;
// This function determines if the node could be type argument position
// Since during editing, when type argument list is not complete,
Expand Down