-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 text-based fastpaths into LS token matching #41924
Add text-based fastpaths into LS token matching #41924
Conversation
@@ -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) { |
There was a problem hiding this comment.
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,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏
Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com>
Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com>
Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com>
This takes the runtime of the
excessivelyLargeArrayLiteralCompletions.ts
fourslash
test down from ~36s on my machine (near the timeout limit) to 1s.A bit of history: The test in question was added recently, in #40953, as a perf-monitoring test. The initial fix did make the perf much better (going from "spin forever" to 11s is pretty good), however its perf has been creeping back up over the last two months, to the point where on @andrewbranch 's machine it started timing out. So I took a look again to see what was going on. The low-level operation that we spend all the time doing is matching tokens, and that's what I'd algorithmically optimized in #40953. In this PR, I took a look from the top down - the reason we're matching tokens, at all, is to determine if the location we're looking for completions at is a type argument position (which in turn filters the completion list) - to do so, we match all braces backwards from the cursor until we run into a
<
which isn't contained within a set of matched braces. Unfortunately, in the test given, there are thousands of braces to match, so even though the token finding within the source file is pretty efficient(tm), we have a lot of work to do, still, since we essentially are forced to find almost every other token in the file. And that's where this PR comes in: in two phases, we use textual heuristics to quickly stop or speed up that token matching process.