laowangba233 saw magpie's CPU jump on each switch to a Library page.
Profiled in a sandbox HOME with a copy of a real Codex config.toml (1.5 MB,
10,927 [projects."<dir>"] tables, one per folder Codex was ever opened
in): 69% of GET /api/library's CPU was go-toml parsing that file through
foundServersOf -> mcpFile.entries. go-toml's SeenTracker looks each table
name up among every sibling before it, so the parse is quadratic in the
number of projects, ~0.2 s of CPU each time, and the Library parsed it on
every visit (edit.Read already kept the bytes, not the parse).
mcpFile.entries now keeps what it parsed through filememo, keyed by the
file's size and time (and, written in the last two seconds, its bytes),
and hands each caller a deep copy of the servers it found, so a caller
changing an entry doesn't change the next caller's. The RTK page's
codexSandbox (Windows, Codex with rtk on) parsed the same file the same
way on each visit and is kept the same way.
Measured (sandbox HOME, the copied config and library, macOS, 10 warm
GET /api/library after start): 2.67 s of CPU before, 0.55 s after
(~0.27 s -> ~0.055 s a visit).
TestCodexEntriesParsedOncePerChange: a config with 2000 projects read
four times parses once, a caller's change to the entries doesn't reach
the next, a change to the file is read, a file gone is no servers. It
fails with the memo undone (entries parsing edit.Read's bytes each time)
and with the copy undone. go test -tags nogui ./internal/library
./internal/filememo pass; TestCodexSandbox passes. The RTK memo has no
test of its own (Windows only); filememo's own tests cover the keep.