Skip to content

perf(memory): take the cache load off startup and session hooks - #28

Merged
ipiton merged 3 commits into
mainfrom
bugfix/t175-lazy-cache-load
Nov 18, 2026
Merged

ipiton merged 3 commits into
mainfrom
bugfix/t175-lazy-cache-load

Conversation

@ipiton

@ipiton ipiton commented Nov 18, 2026

Copy link
Copy Markdown
Owner

What does this PR do?

Takes the memory cache load off the startup path and off the session hooks.

  • The cache is rebuilt aside and swapped in with a short assignment, so reads and writes no longer wait for a rebuild. Writes made during a rebuild are preserved.
  • auto-capture, checkpoint and close-session read their context from the database and do not load the whole bank.
  • The server loads the cache in the background, so document search is available sooner.

This does not make the first memory call faster. It still needs the full cache, so on a starved machine memory tools return starting for minutes.

Behavior changes

  • Memory tools wait up to 5 seconds for the cache, then return the retryable starting error.
  • If the cache cannot be loaded, memory tools stay listed and return an error. The next call retries the load. Before, the store counted as unavailable and the tools were not listed.
  • /health adds memory_ready. memory_available now means the store is open.

Why?

Loading the cache costs about 0.9 s of CPU and 679 MB of allocations on a 33,580-record bank. Every server start and every hook paid for it, and rebuilds after a foreign write held the store locks.

Measurements

Real binary on a copy of the bank, before → after. The 2% CPU column limits only the measured process.

No limit 2% CPU
First search 1.27–3.29 s → 0.21–0.83 s 182.6–281.0 s → 30.2–38.1 s
First memory call 1.30–3.31 s → 1.38–2.06 s 184.6–283.0 s → 188.8–212.8 s
checkpoint, largest context 0.74–0.76 s → 0.52–0.74 s 78.3 s → 28.1 s
checkpoint, second context 0.74–0.77 s → 0.05–0.10 s 114.4 s → 2.0 s

Peak memory of checkpoint fell from 371–393 MB to 200–210 MB in the largest context, and from 371–405 MB to 33–36 MB in the second.

Machine load varied between runs: unthrottled checkpoint ran at load about 5 before and about 16 after. At 2% CPU, checkpoint ran once per binary and MCP twice.

Testing

  • gofmt, make vet and go test -race ./... pass.
  • New tests cover context reads without a load, writes and foreign commits during the load, retry after a failed load, and Close during a load.
  • The whole suite was also run with deferred loading forced into NewStore. Only 4 tests failed, all of which assert on an already loaded cache or on the cache_load startup log line.

Not verified

  • Live Claude Code and Codex clients.
  • A cold file cache.
  • Real load above 100 with swapping.

@ipiton
ipiton merged commit cba46cf into main Nov 18, 2026
5 checks passed
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