Cache StartLine and NewLinesCount for O(1) access#25
Open
dreed-sd wants to merge 1 commit into
Open
Conversation
StartLine was computed on every access by recursively walking parent tokens to count newlines — O(depth × siblings) per call. Similarly, NewLinesCount on GDNode iterated all child tokens via LINQ Sum each time. Both are now cached on first access. The AST is immutable after parsing so the cached values never go stale. Measured on a project with 1,358 GDScript files: transpilation dropped from ~10s to ~1s (9.4x improvement). The test suite also benefits — 694 tests in 811ms vs ~5s previously.
Owner
|
The caching should work only if the tokens form is frozen. Otherwise, the functionality will not work correctly if the code tree changes dynamically |
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.
StartLine was computed on every access by recursively walking parent tokens to count newlines — O(depth × siblings) per call. Similarly, NewLinesCount on GDNode iterated all child tokens via LINQ Sum each time.
Both are now cached on first access. The AST is immutable after parsing so the cached values never go stale.
Resulted in a 9.4x improvement in perf in real-world usage that transpiles GDScript code.