From fcbbb8e479c8fa240db3942606a6ebec436d9b42 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 28 Oct 2026 13:51:33 +0800 Subject: [PATCH] gui: the start's warm-up and read-ahead look at each file once a second (#1665) The warm-up after a start (sessions, usage, Codex effort) and the usage read-ahead asked for settings.json, logins.json and the rest for each session and row they read: a Windows sandbox's second start stat'ed settings.json 677 times in 25s, 286 of them settings.load and AccountDiscoveryOff under the warm-up. Both now hold what was read (filememo.Hold), as the GUI's requests and a catalog sync's round do. A write still reads the disk (edit.Read, settings' writes, logins.json under loginsMu), so nothing is written from a held read; a file another app changes is seen up to a second late while they run. Measured on xiaomi-gpu (Windows 11), an empty sandbox HOME, magpie web --gateway, second start, 27s, with a build counting each look at the disk (instrumentation not committed): before 1113 looks, settings.json 677 after 483 looks, settings.json 114 The catalog sync's round stays at ~100ms. Run: - TestWarmUpHoldsWhatItReads: red with the warm-up's Hold taken out by hand (the second read saw the other app's change), green with it. - mote: gofmt -l internal/gui clean; GOOS=windows and darwin go vet ./internal/gui and GOOS=linux go vet -tags nogui ./internal/gui clean; go test -count=1 ./internal/gui ./internal/usage: usage ok, gui red only on TestMainWindowCorners (#3949, mote's macOS 27, red on main before this). --- internal/gui/backend.go | 7 ++++- internal/gui/sessions.go | 5 ++++ internal/gui/warm_hold_test.go | 47 ++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 internal/gui/warm_hold_test.go diff --git a/internal/gui/backend.go b/internal/gui/backend.go index cb2d76d01..1107f6f6e 100644 --- a/internal/gui/backend.go +++ b/internal/gui/backend.go @@ -9,6 +9,7 @@ import ( "github.com/yetone/magpie/internal/agent" "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/filememo" "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/update" @@ -43,7 +44,11 @@ func startBackend() (gw *gateway.Server) { go watchGateway(backendCtx, gatewayWatch) // the usage log parsed now, not when the tray panel first asks for // today's calls (CyberCaius) - go usage.ReadAhead() + go func() { + // its files looked at once a second, as the warm-up's (#1665) + defer filememo.Hold()() + usage.ReadAhead() + }() // Codex's background app-server, restarted when it has the list from // before a change and no codex session is on it (luci) go agent.KeepCodexDaemonCurrent(backendCtx) diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 81ef991ee..08c3f262f 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -18,6 +18,7 @@ import ( "time" "github.com/yetone/magpie/internal/agent" + "github.com/yetone/magpie/internal/filememo" "github.com/yetone/magpie/internal/proc" "github.com/yetone/magpie/internal/sessions" "github.com/yetone/magpie/internal/settings" @@ -586,6 +587,10 @@ var warmCodexEffort = func() { usage.CodexModelEffort(usage.All) } // the next minutes: a magpie that had just started read ~470 MB for its // first two minutes at rest. func warmUp(steps ...func()) { + // each file read looked at once a second, not for each session or row + // that asks: an empty sandbox's warm-up looked at settings.json ~300 + // times on Windows (#1665); a write still reads the disk (edit.Read) + defer filememo.Hold()() for _, step := range steps { step() } diff --git a/internal/gui/warm_hold_test.go b/internal/gui/warm_hold_test.go new file mode 100644 index 000000000..0c563e4ef --- /dev/null +++ b/internal/gui/warm_hold_test.go @@ -0,0 +1,47 @@ +package gui + +import ( + "os" + "path/filepath" + "testing" + "time" + + "github.com/yetone/magpie/internal/filememo" +) + +// The warm-up's reads look at a file once a second, not for each session +// or row that asks (#1665: settings.json ~300 times on Windows): a file +// another app changed meanwhile is read as it was, inside that second. +func TestWarmUpHoldsWhatItReads(t *testing.T) { + p := filepath.Join(t.TempDir(), "f.json") + old := time.Now().Add(-time.Hour) + write := func(s string, at time.Time) { + if err := os.WriteFile(p, []byte(s), 0o600); err != nil { + t.Fatal(err) + } + if err := os.Chtimes(p, at, at); err != nil { + t.Fatal(err) + } + } + read := func() string { + b, err := filememo.Read("warm-hold-test", p, func(b []byte) (string, error) { return string(b), nil }) + if err != nil { + t.Fatal(err) + } + return string(b) + } + write("one", old) + var got string + warmUp(func() { + read() + write("two", old.Add(time.Minute)) + got = read() + }) + if got != "one" { + t.Fatalf("the warm-up looked at the file again for its second read: got %q", got) + } + // and nothing is held once it ends + if got := read(); got != "two" { + t.Fatalf("after the warm-up the file reads %q, not what is on the disk", got) + } +}