perf: cache loadConfig() and ensureConfigDir() results to eliminate repeated disk reads - #25
Open
mediumWellness wants to merge 1 commit into
Open
Conversation
…epeated disk reads Add module-level in-memory caches for both config loading and config-dir creation to avoid redundant synchronous fs operations on every CLI command invocation. - _configCache: VeniceConfig | null — populated on first loadConfig() call; returned directly on subsequent calls; updated by saveConfig() so it stays in sync after writes - _configDirEnsured: boolean — set to true after the first successful ensureConfigDir(); subsequent calls return immediately without an fs.existsSync() round-trip Before this change every output line triggered: getChalk() -> createChalk() -> isColorEnabled() -> loadConfig() -> fs.readFileSync() and each of getDefaultModel(), getDefaultImageModel(), getDefaultVoice(), getApiKey(), shouldShowUsage(), getOutputFormat() also called loadConfig() independently, causing N synchronous disk reads per invocation. After this change only the first call incurs any I/O. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR reduces repeated synchronous filesystem access in the CLI by introducing in-process caching for configuration loading and a one-time guard for ensuring the config directory exists.
Changes:
- Add a module-level
_configCachesoloadConfig()reads/parsesconfig.jsonat most once per process. - Update
saveConfig()to keep the in-memory cache in sync after writes. - Add a module-level
_configDirEnsuredflag soensureConfigDir()avoids repeatedexistsSync()checks.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
38
to
+42
| } catch { | ||
| // Return empty config on error | ||
| } | ||
| return {}; | ||
| _configCache = {}; | ||
| return _configCache; |
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.
Problem
Every CLI command caused multiple synchronous disk reads via \loadConfig(). The call chain for each output line alone was:
\
getChalk() → createChalk() → isColorEnabled() → loadConfig() → fs.readFileSync()
\\
Beyond that, \getDefaultModel(), \getDefaultImageModel(), \getDefaultVoice(), \getApiKey(), \shouldShowUsage(), and \getOutputFormat()\ each independently called \loadConfig(), meaning a single command could trigger 6+ synchronous
eadFileSync\ calls reading the same file each time.
Similarly, \ensureConfigDir()\ called \s.existsSync(CONFIG_DIR)\ on every config/history/usage operation with no early-exit.
Fix
Added two module-level caches in \src/lib/config.ts:
Config cache
\\ s
let _configCache: VeniceConfig | null = null;
\\
Config-dir flag
\\ s
let _configDirEnsured = false;
\\
Expected improvement
After this change, \loadConfig()\ incurs a single
eadFileSync\ per process lifetime instead of one per call site. For a typical command that calls 6+ config accessor functions, this reduces synchronous I/O from O(N) to O(1).
Testing
Existing test suite passes (
pm test) — one test covering \config show --format json\ with API key masking.