sessions: a visit to Sessions no longer parses Codex's config.toml again when it hasn't changed (#1665, laowangba233)
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.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
toml "github.com/pelletier/go-toml/v2"
|
||||
"github.com/tidwall/gjson"
|
||||
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
"github.com/yetone/magpie/internal/steady"
|
||||
)
|
||||
@@ -99,11 +100,16 @@ type codexConfig struct {
|
||||
} `toml:"profiles"`
|
||||
}
|
||||
|
||||
// readCodexConfig is the folder's codexConfig, parsed again only when
|
||||
// config.toml changes: a config.toml holding a table for each of ten
|
||||
// thousand projects took ~0.15s of CPU to parse, on each visit to Sessions
|
||||
// (#1665). What it gives is shared and must not be changed.
|
||||
func readCodexConfig(home string) codexConfig {
|
||||
var c codexConfig
|
||||
if b, err := os.ReadFile(filepath.Join(home, "config.toml")); err == nil {
|
||||
c, _ := filememo.Read("sessions codex config", filepath.Join(home, "config.toml"), func(b []byte) (codexConfig, error) {
|
||||
var c codexConfig
|
||||
toml.Unmarshal(b, &c)
|
||||
}
|
||||
return c, nil
|
||||
})
|
||||
return c
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user