Profiled for laowangba233's report of CPU jumping on page switches: in a sandbox HOME with a copy of a real Codex config.toml (10,927 [projects."<dir>"] tables), 67% of GET /api/sessions/manage's CPU was go-toml parsing config.toml in readCodexConfig, for the provider Codex uses now (codexProviders -> CodexProviderIn). go-toml checks each table name against every sibling before it, so a file with that many projects takes ~0.2 s to parse, and it was parsed on every visit. readCodexConfig now keeps the parse through filememo, keyed by the file's size and time (and its bytes while it is less than two seconds old). Its callers only read the struct. Measured (sandbox HOME, 8.9 GB of copied sessions, macOS, 10 warm GET /api/sessions/manage after start): 3.00 s of CPU before, 0.94 s after (~0.30 s -> ~0.09 s a visit). TestCodexConfigParsedOncePerChange rewrites config.toml with the same size and time and expects the first parse; a real change is read. It fails with the memo undone. go test -tags nogui ./internal/sessions passes.
34 lines
920 B
Go
34 lines
920 B
Go
package sessions
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Sessions read Codex's config.toml on every visit, and one with a table for
|
|
// each of thousands of projects took ~0.15s of CPU to parse (#1665): it is
|
|
// parsed again only when it changes.
|
|
func TestCodexConfigParsedOncePerChange(t *testing.T) {
|
|
d := t.TempDir()
|
|
p := filepath.Join(d, "config.toml")
|
|
old := time.Now().Add(-time.Hour)
|
|
write := func(s string) {
|
|
os.WriteFile(p, []byte(s), 0o600)
|
|
os.Chtimes(p, old, old)
|
|
}
|
|
write("model_provider = \"aaaa\"\n")
|
|
if got := CodexProviderIn(d); got != "aaaa" {
|
|
t.Fatal(got)
|
|
}
|
|
write("model_provider = \"bbbb\"\n")
|
|
if got := CodexProviderIn(d); got != "aaaa" {
|
|
t.Fatalf("parsed again with the file unchanged by size and time: %q", got)
|
|
}
|
|
os.WriteFile(p, []byte("model_provider = \"cc\"\n"), 0o600)
|
|
if got := CodexProviderIn(d); got != "cc" {
|
|
t.Fatalf("after a change: %q", got)
|
|
}
|
|
}
|