Improve config resolution caching with directory-based cache #432
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.
This PR improves the performance of config resolution by caching results at the directory level instead of per-file. When a config is resolved for a file, we cache the result for all parent directories up to (and including) the config's directory.
Why the previous caching wasn't optimal
The previous implementation cached config lookups by file path:
This had two problems:
/src/components/created 100 cache entries all pointing to the same config./repo/packages/ui/src/Button.tsx, only that exact path was cached. A subsequent file at/repo/packages/ui/src/Input.tsxwould miss the cache (different path) and re-resolve, even though the answer is identical.The new approach
Cache by directory and populate all parent directories up to the config location:
For file at '/repo/packages/ui/src/Button.tsx' with config at '/repo/.prettierrc'
Caches:
/repo/packages/ui/src->/repo/.prettierrc/repo/packages/ui->/repo/.prettierrc/repo/packages->/repo/.prettierrc/repo->/repo/.prettierrcNow:
/repo/packages/ui/src/share one cache entry/repo/packages/ui/utils/hit the cache at/repo/packages/ui/repo/other/deep/path/hit the cache at/repoBenchmark
Benchmark environment
Benchmark on dub codebase (~3000 files, deep nesting):
Result: 1.24× faster (~24% improvement)
Benchmark on headlessui codebase (~600 files, flatter structure):
Result: 1.07× faster (~7% improvement)
The improvement scales with directory depth and file count.
Note: This PR was completed with the assistance of Claude Code