Token leadingTrivia handling and reduce memory retention in long-lived compiler/LSP caches#1705
Conversation
There was a problem hiding this comment.
Pull request overview
This PR reduces memory retention in long-lived BrighterScript sessions by placing bounds on a frequently-used path normalization cache and by more aggressively releasing cached data during disposal of core objects (scopes and files).
Changes:
- Add a size limit to
Util.standardizePath’s internal cache to prevent unbounded growth (clears the cache when the limit is reached). - Ensure
Scope.dispose()releases cached data and unlinks symbol table relationships. - Ensure
BrsFile.dispose()invalidates_cachedLookupsso cached AST-derived collections don’t remain retained after disposal, with accompanying tests.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/util.ts |
Adds a bounded standardizePath cache with a helper method to enforce the limit. |
src/util.spec.ts |
Adds test coverage to verify cache bounding behavior. |
src/Scope.ts |
Clears scope cache and unlinks symbol tables during dispose() to reduce retention. |
src/Scope.spec.ts |
Adds a test ensuring scope cache is cleared on dispose. |
src/files/BrsFile.ts |
Invalidates _cachedLookups during file disposal to release cached lookup data. |
src/files/BrsFile.spec.ts |
Adds a test ensuring cached lookups are cleared on dispose. |
TwitchBronBron
left a comment
There was a problem hiding this comment.
Hmm. So this path caching was added to improve performance. I benchmarked it to verify that it was significantly faster to cache these lookups.
Adding a function call in the middle of this hot function might negate a significant amount the benefit of caching in the first place.
Just curious, did you do any profiling to see how much memory this cache actually gets? Is it actually significant or just a guess?
Yeah.. That was just AI.. I'll revert that change. |
|
Hey there! I just built a new temporary npm package based on 39c1aae. You can download it here or install it by running the following command: npm install https://github.com/rokucommunity/brighterscript/releases/download/v0.0.0-packages/brighterscript-1.0.0-alpha.50-memory-performance.20260512220521.tgz |
Summary
This PR tightens memory behavior and trivia handling across lexer/parser/transpile paths.
It does two main things:
leadingTriviais optional when empty.What changed
leadingTriviawhen there is no triviaToken.leadingTriviais now optionalisPostionInComment->isPositionInComment)Why
In editor-driven and long-running language server sessions, small retained references and unbounded cache growth can accumulate over time.
These changes keep behavior intact while lowering memory pressure and making trivia handling more explicit and robust.
Testing
Results
This code was run on an app with these metrics: