Skip to content

perf(config): mtime-signature fast path for load_config - #410

Merged
ChuckBuilds merged 1 commit into
mainfrom
perf/config-mtime-cache
Jul 14, 2026
Merged

perf(config): mtime-signature fast path for load_config#410
ChuckBuilds merged 1 commit into
mainfrom
perf/config-mtime-cache

Conversation

@ChuckBuilds

@ChuckBuilds ChuckBuilds commented Jul 13, 2026

Copy link
Copy Markdown
Owner

Summary

PR 6 of the performance series. load_config() did full work on every call — 3 file reads + 3 JSON parses + a recursive template-migration diff — and it's called from ~30 web request handlers, several 2–3× per request.

Now: 3 os.stat calls; if the (mtime_ns, size) signature of config/secrets/template is unchanged since the last successful load, return the already-parsed self.config (identical aliasing semantics to the full path). Signature taken after load+migration so migration write-backs don't retrigger; both save paths refresh it; rollback and raw-file writes self-invalidate through the mtime they change.

Cross-process freshness preserved by construction — the web process's save bumps the mtime, so the display process's next call re-reads (dedicated test). Same-second edits caught by mtime_ns + size.

Verification

7 new tests: zero file opens on the fast path (counted via monkeypatched open), reload on each of the three files changing, same-mtime/different-size edit detection, save-then-load coherence with secrets preserved, and the cross-process pickup. Full config-related suites green (the 2 failures are the pre-existing order-dependent TestConfigAPI double-sided ones on main).

🤖 Generated with Claude Code

https://claude.ai/code/session_01FqzC1nzTWL4kaqgMaQZFam

Summary by CodeRabbit

  • Performance

    • Improved configuration loading to avoid unnecessary file reads when configuration files are unchanged.
    • Configuration changes are detected reliably, including edits made within the same timestamp interval.
  • Bug Fixes

    • Ensured saved settings remain immediately available without redundant reloads.
    • External updates to configuration files are now detected and reflected correctly.
  • Tests

    • Added coverage for caching, file changes, template updates, and save/reload behavior.

load_config re-read and re-parsed config.json, the secrets file, AND the
template (running the recursive migration diff) on every call — with
~30 call sites in web request handlers, some hit 2-3x per request.

Fast path: stat all three files (mtime_ns + size); when unchanged since
the last successful load, return the already-parsed self.config (same
aliasing semantics as before). The signature is taken AFTER load +
migration so a migration write-back doesn't retrigger, and both save
paths refresh it. Cross-process freshness is preserved by construction:
a save from the other process bumps the file mtime, so the next load
here re-reads — verified by a dedicated test. Same-second edits are
caught by mtime_ns plus a size check.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FqzC1nzTWL4kaqgMaQZFam
@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 94d9724a-b147-479d-a286-b9813d356994

📥 Commits

Reviewing files that changed from the base of the PR and between 6edd80d and 373fe2e.

📒 Files selected for processing (2)
  • src/config_manager.py
  • test/test_config_load_cache.py

📝 Walkthrough

Walkthrough

ConfigManager now caches loaded configuration using (mtime_ns, size) signatures for config, secrets, and template files. Saves refresh the cache signature, while tests cover unchanged loads, file changes, same-mtime edits, migrations, and external updates.

Changes

Config Cache

Layer / File(s) Summary
Signature cache and save coherence
src/config_manager.py
Adds file-signature tracking, a cached-load fast path, post-load signature updates, and signature refreshes after atomic and non-atomic saves.
Cache invalidation and save tests
test/test_config_load_cache.py
Tests unchanged-file caching, config/secrets/template changes, same-mtime edits, template migration, save coherence, and cross-process updates.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ConfigManager
  participant ConfigFiles
  ConfigManager->>ConfigFiles: Compute mtime_ns and size signature
  ConfigFiles-->>ConfigManager: Return current signature
  ConfigManager->>ConfigManager: Compare with _loaded_sig
  ConfigManager-->>ConfigManager: Return cached config or load files
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding an mtime-signature fast path to load_config for config caching.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/config-mtime-cache

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 4 complexity · 0 duplication

Metric Results
Complexity 4
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@ChuckBuilds
ChuckBuilds merged commit bff1312 into main Jul 14, 2026
8 checks passed
@ChuckBuilds
ChuckBuilds deleted the perf/config-mtime-cache branch July 14, 2026 12:49
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