Skip to content

Speed up TSFASTImporter (from 7sec to 115ms for a python file of 900 LoC)#33

Merged
alesshosry merged 1 commit intoEvref-BL:mainfrom
jecisc:speed-up-start-end
Feb 25, 2026
Merged

Speed up TSFASTImporter (from 7sec to 115ms for a python file of 900 LoC)#33
alesshosry merged 1 commit intoEvref-BL:mainfrom
jecisc:speed-up-start-end

Conversation

@jecisc
Copy link
Contributor

@jecisc jecisc commented Feb 19, 2026

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

@jecisc
Copy link
Contributor Author

jecisc commented Feb 19, 2026

I updated the comment of the PR because I added a commit that changes a lot the intent behind this PR :)

@jecisc jecisc changed the title Speed up some methods when the parameter is a ByteString Speed up TSFASTImporter (from 7sec to 115ms for a python file of 900 LoC) Feb 19, 2026
@jecisc jecisc force-pushed the speed-up-start-end branch from 4dbc614 to 262e239 Compare February 19, 2026 23:56
@jecisc jecisc force-pushed the speed-up-start-end branch from 262e239 to 4684e03 Compare February 20, 2026 00:19
@jecisc
Copy link
Contributor Author

jecisc commented Feb 20, 2026

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 :)

@alesshosry alesshosry merged commit eb97a81 into Evref-BL:main Feb 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Idea to improve performances

2 participants