From b1a2eec2c143021494b2274f2311da9bccab5740 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 21 Oct 2026 23:42:09 +0800 Subject: [PATCH] gui: the warm-up reads Codex's effort under the tight GC too The warm-up's last step reads Codex's per-thread effort from the whole ledger right after the usage page's read, while that page still holds every row (~300 MB live on the owner's history). It ran at GOGC 100, so the heap was let grow to about twice that: a heap profile every 2 s of a cold start showed heapSys going 342 -> 622 MB while live stayed ~300, in the seconds after QueryPage returned. It now runs under underTightGC like the usage read. Footprint (phys_footprint, what Activity Monitor shows), cold start on a clone of the real history (15,176 files), alternating, two runs each: 4042aa4d1 peak 611 / 575 MB end RSS 659 / 651 MB CPU 4:20.6 / 3:54.9 this peak 521 / 498 MB end RSS 595 / 568 MB CPU 4:11.7 / 4:00.7 Tests: TestWarmUpReadsCodexEffort now also checks the read runs at GOGC 25; with the step put back to warmCodexEffort() by hand it is red. go test ./... -timeout 45m on 083b1c8e7: 68 packages ok; rebased onto b3f595349 (routing CSS/JS only), go test ./internal/gui ok again. GOOS=linux and GOOS=windows go vet -tags nogui ./... clean; gofmt clean. --- internal/gui/sessions.go | 8 +++++--- internal/gui/sessions_warm_test.go | 10 +++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index fa5db8ce2..add8cbe66 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -509,7 +509,7 @@ func warmSteps() []func() { func() { underTightGC(func() { usage.QueryPage(usage.All, usage.Filter{}, 0, 50) }) }, func() { if slices.ContainsFunc(statsFor(0).Keys, func(k string) bool { return strings.HasPrefix(k, "codex:") }) { - warmCodexEffort() + underTightGC(warmCodexEffort) } }, } @@ -536,8 +536,10 @@ func warmUp(steps ...func()) { // what was set; a GOGC the user set lower, or off, is kept. The usage // page's read of the whole history holds every row of it at once (~270 MB // live for 390k calls), and a cold start that rests at ~200 MB peaked at -// 555–587 MB in Activity Monitor there; under it, 410–438 MB. Only that -// read runs under it: the warm-up as a whole did too for ~11 s more CPU. +// 555–587 MB in Activity Monitor there; under it, 410–438 MB. That read +// and Codex's effort, which reads the whole ledger again while the page +// still holds its rows, run under it: the warm-up as a whole did too for +// ~11 s more CPU. func underTightGC(fn func()) { if was := debug.SetGCPercent(tightGCPercent); was < tightGCPercent { debug.SetGCPercent(was) diff --git a/internal/gui/sessions_warm_test.go b/internal/gui/sessions_warm_test.go index ebf3464ba..91fa74742 100644 --- a/internal/gui/sessions_warm_test.go +++ b/internal/gui/sessions_warm_test.go @@ -2,6 +2,7 @@ package gui import ( "runtime" + "runtime/debug" "testing" "time" @@ -33,7 +34,9 @@ func TestWarmUpReturnsItsGarbage(t *testing.T) { // With Codex sessions on this computer the warm-up also reads Codex's // per-thread effort, which a first Codex page after a start otherwise read // from the whole ledger while it waited (#2776); without any it doesn't. +// It reads under the tight GC: the usage page's rows are still held then. func TestWarmUpReadsCodexEffort(t *testing.T) { + defer debug.SetGCPercent(debug.SetGCPercent(100)) statsMemo.Lock() saved := statsMemo.m statsMemo.Unlock() @@ -55,7 +58,12 @@ func TestWarmUpReadsCodexEffort(t *testing.T) { } statsMemo.Unlock() read := 0 - warmCodexEffort = func() { read++ } + warmCodexEffort = func() { + read++ + if p := gcPercent(); p != tightGCPercent { + t.Errorf("Codex's effort was read under GOGC=%d, want %d", p, tightGCPercent) + } + } warmUp(warmSteps()...) if read != c.want { t.Fatalf("with sessions %v Codex's effort was read %d times, want %d", c.keys, read, c.want)