Files
magpie/internal/library/mcp_entries_memo_test.go
T
yetone 9300b2a126 library: a visit to the Library no longer parses Codex's config.toml again when it hasn't changed (#1665, laowangba233)
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.
2026-10-12 02:56:01 +08:00

57 lines
1.6 KiB
Go

package library
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// A Codex config.toml with a table for each of thousands of projects took
// ~0.2s of CPU to parse, and the Library parsed it on every visit (#1665):
// the file is parsed once until it changes, and a change to what entries
// gave doesn't reach the next caller.
func TestCodexEntriesParsedOncePerChange(t *testing.T) {
p := filepath.Join(t.TempDir(), "config.toml")
var b strings.Builder
b.WriteString("[mcp_servers.a]\ncommand = \"x\"\nargs = [\"1\"]\n")
for i := range 2000 {
fmt.Fprintf(&b, "[projects.\"/p/%04d\"]\ntrust_level = \"trusted\"\n", i)
}
os.WriteFile(p, []byte(b.String()), 0o600)
old := time.Now().Add(-time.Hour)
os.Chtimes(p, old, old)
f := &mcpFile{Path: p, Format: fmtCodex}
es, err := f.entries()
if err != nil || es["a"]["command"] != "x" {
t.Fatal(es, err)
}
es["a"]["command"] = "changed"
es["a"]["args"].([]any)[0] = "changed"
delete(es, "a")
// asked again with the file unchanged: not parsed again
n := entriesParsed.Load()
for range 3 {
es, _ = f.entries()
}
if d := entriesParsed.Load() - n; d != 0 {
t.Fatalf("parsed %d more times with the file unchanged", d)
}
if es["a"]["command"] != "x" || es["a"]["args"].([]any)[0] != "1" {
t.Fatalf("a caller's change reached the next one: %v", es["a"])
}
// a change is read
os.WriteFile(p, []byte("[mcp_servers.b]\ncommand = \"z\"\n"), 0o600)
if es, _ = f.entries(); es["b"]["command"] != "z" || es["a"] != nil {
t.Fatalf("after a change: %v", es)
}
os.Remove(p)
if es, err = f.entries(); err != nil || len(es) != 0 {
t.Fatalf("no file: %v %v", es, err)
}
}