Skip to content

Conversation

@Dunqing
Copy link
Contributor

@Dunqing Dunqing commented Jan 7, 2026

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:

prettierConfigCache.remember(filePath, async () => {
  return await prettier.resolveConfigFile(filePath)
})

This had two problems:

  1. Cache per file, not per directory — Every file got its own cache entry, even though all files in the same directory share the same config. Processing 100 files in /src/components/ created 100 cache entries all pointing to the same config.
  2. No parent directory caching — When resolving config for /repo/packages/ui/src/Button.tsx, only that exact path was cached. A subsequent file at /repo/packages/ui/src/Input.tsx would 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/.prettierrc

Now:

  • All files in /repo/packages/ui/src/ share one cache entry
  • Files in /repo/packages/ui/utils/ hit the cache at /repo/packages/ui
  • Files in /repo/other/deep/path/ hit the cache at /repo

Benchmark

Benchmark environment

  • CPU: Apple M4 Max (16 cores)
  • Memory: 64 GB
  • OS: macOS 26.1
  • Node: v24.10.0

Benchmark on dub codebase (~3000 files, deep nesting):

Version Time (mean ± σ)
Before 15.547s ± 0.581s
After 12.499s ± 0.688s

Result: 1.24× faster (~24% improvement)

Benchmark on headlessui codebase (~600 files, flatter structure):

Version Time (mean ± σ)
Before 4.140s ± 0.044s
After 3.879s ± 0.031s

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

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.

1 participant