Speed up TSFASTImporter (from 7sec to 115ms for a python file of 900 LoC)#33
Merged
alesshosry merged 1 commit intoEvref-BL:mainfrom Feb 25, 2026
Merged
Conversation
Contributor
Author
|
I updated the comment of the PR because I added a commit that changes a lot the intent behind this PR :) |
4dbc614 to
262e239
Compare
262e239 to
4684e03
Compare
Contributor
Author
|
I did some benchmark before and after this PR on this file: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/types/react/index.d.ts I had some errors so I added a try/catch in the visit node to not fail. Before the PR it was taking 124sec and after the PR it takes 400ms :) I also tried to import this file: https://github.com/microsoft/TypeScript/blob/main/src/compiler/parser.ts Before the PR I stopped the import after 5minutes. After the PR it takes 3seconds :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I tried to import some files with TSFASTImporter without doing any customizations to the FAST model and I got disappointed because it was already taking 7sec for a file of 900LoC and this is slow :(
I am proposing a big speed up in this PR.
Previously, for each node we were using either the TSPoint or the #start/endByte to compute the positions in number of characters. But the more nodes we have, the slower it is getting.
Since all nodes are computing from the same origin string, I propose to compute only once a dictionary associating the index of each leading bytes to the index of the corresponding characters. This takes 4ms for a file a 900 lines (~23k bytes) containing characters encoded on multiple bytes and then getting the positions in characters from the position in bytes becomes almost instant.
The change brings the import of the file from 7sec to 115ms on my machine.
Fixes #32 by making it obsolete