POC: cache file entries to reduce heap allocation#548
Open
no-yan wants to merge 2 commits intooxc-project:mainfrom
Open
POC: cache file entries to reduce heap allocation#548no-yan wants to merge 2 commits intooxc-project:mainfrom
no-yan wants to merge 2 commits intooxc-project:mainfrom
Conversation
no-yan
commented
Dec 31, 2025
Comment on lines
+242
to
+259
| +// tryExtensionBits checks whether `extensionless + <extension>` exists, using pre-scanned cache. | ||
| +// If the file exists, it constructs and returns `resolved`; otherwise it returns continueSearching(). | ||
| +// | ||
| +// Why this exists: | ||
| +// Module resolution is a hot path. A straightforward implementation tries multiple extensions by | ||
| +// concatenating strings (`extensionless + ext`) and then hitting the filesystem. | ||
| +// In Go, each concatenation allocates a new string, so doing this for *missing* candidates increases GC pressure. | ||
| +// | ||
| +// Optimization strategy: | ||
| +// - During a directory scan, we record "which extensions exist" for each extensionless path: | ||
| +// pathCache[extensionless] = bitset of possible extensions | ||
| +// - At lookup time, we first test the bitset; we only build the final path string when the extension is possible. | ||
| +// | ||
| +// Result: | ||
| +// Avoids allocations for non-existing extension candidates; we allocate only for candidates that may exist. | ||
| +// | ||
| +// See: https://www.typescriptlang.org/docs/handbook/modules/reference.html#the-moduleresolution-compiler-option | ||
| +func (r *resolutionState) tryExtensionBits(extension extensionBits, extensionless string, resolvedUsingTsExtension bool, _onlyrecordFailure bool) *resolved { |
Collaborator
Author
There was a problem hiding this comment.
This is the main logic.
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.
PoC for #295
This change reduces GC time by ~30%, by eliminating string concatenation in missing files check.
In the large codebase(kibana), program execution is 10% faster than before.
I'm not considering this should be merged, but I believe it could serve as a starting point for proposing a new approach to the tsgo team.
Note
Disclosure: I used some AI tools: ChatGPT and Gemini to help creating hypotheses and improve my English, and Claude to review my code.
Object allocation
Some execution paths still fall back to the original implementation, but overall allocations are significantly reduced.
GC time
Details
before:
after: