Skip to content

Cache StartLine and NewLinesCount for O(1) access#25

Open
dreed-sd wants to merge 1 commit into
elamaunt:LSPfrom
dreed-sd:startline-perf
Open

Cache StartLine and NewLinesCount for O(1) access#25
dreed-sd wants to merge 1 commit into
elamaunt:LSPfrom
dreed-sd:startline-perf

Conversation

@dreed-sd
Copy link
Copy Markdown

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.

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.
@elamaunt
Copy link
Copy Markdown
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

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.

2 participants