perf(models-dev): load disk cache synchronously, refresh in background - #7823
perf(models-dev): load disk cache synchronously, refresh in background#7823light-merlin-dark wants to merge 1 commit into
Conversation
fetch_models_dev() blocks on a network GET to models.dev on every cold startup, then writes a 1.7MB JSON cache to disk. This adds ~600ms to agent init (400ms HTTP + 200ms JSON serialize + write). Restructure to load the disk cache synchronously (fast, ~25ms) and kick off a background thread for the network refresh. Only block on the network fetch when no disk cache exists at all (first-ever run). The background refresh updates both in-memory and disk caches so the next cold-start sees fresh data. Benchmark (macOS M4 Max, Python 3.11): Before: 600ms blocking (400ms HTTP + 200ms JSON write) After: ~25ms (disk cache load only; refresh in background) Signed-off-by: Merlin <merlin@merlin.me>
|
Thanks @light-merlin-dark — closing as substantially superseded on current main (verified against f64e4f4). The primary win here (skip the network fetch when the disk cache is fresh) landed via commit 775c0e2 ("perf(models_dev): cache-first lookup, skip network when disk cache is fresh", #22808): The piece main does NOT have is your stale-cache path: serving the stale disk data immediately and refreshing in a background daemon thread (main still does a synchronous network fetch when the disk cache is expired). If that cold-start-with-stale-cache case matters to you, a focused follow-up PR for just the background refresh would be considered on its own merits — with attention to the thread-spawn-in-library-code trade-off. |
Problem
fetch_models_dev()blocks startup on a network GET tomodels.dev/api.jsonon every cold start, then serializes and writes a 1.7MB JSON cache to disk. This adds ~600ms toAIAgent.__init__viaContextCompressor.__init__→get_model_context_length()→lookup_models_dev_context()→fetch_models_dev().Profiled breakdown:
This runs on every cold process start even when a fresh disk cache exists from a recent run.
Solution
Restructure
fetch_models_dev()to:This follows the same pattern already used in
run_agent.pyfor OpenRouter model metadata pre-warming.Metrics
Measured on macOS M4 Max, Python 3.11, Hermes v0.8.0:
Startup impact:
AIAgent.__init__drops from ~1.2s to ~0.6s on subsequent runs.Files changed
agent/models_dev.py—fetch_models_dev()loads disk cache first; added_background_refresh()helper.Merlin (@EnchantedRobot on X) & GLM 5.1 via OpenCode