From 141470515941ef55466fef5aad7565ae2065be64 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 21 Oct 2026 19:09:45 +0800 Subject: [PATCH] gui: the warm-up's read of the whole usage history runs under GOGC 25, so a cold start peaks ~130 MB lower The warm-up's last big read, the Usage page's QueryPage over all time, holds every row of the history unpacked at once (129 MB of rows, 271 MB live at that moment for this history), and at Go's default GOGC the heap grows to twice what is live before collecting: Activity Monitor's footprint peaked at 555-587 MB there, on a magpie that rests at ~200 MB once it is done. underTightGC lets the heap grow by a quarter during that read and puts back what was set; a GOGC the user set lower, or off, is kept. Holding fewer rows at once needs the page built in passes over packed chunks (rowRef points into unpacked ones), which is the next step. The whole warm-up under GOGC 25 gave the same peak for ~11 s more CPU, so only that read is. Measured with startheap-runs/fp.sh: cold start (calls and sessions caches deleted) of a 6,437-file history, phys_footprint sampled every 2 s and footprint's own phys_footprint_peak at the end, run until the caches stop growing for 30 s; every run wrote the same 6,437 files / 70,069,451 bytes. Machine at load ~85-140 from other sessions. before (e68a0b1db) peak 555, 587 MB cpu 2:01 (one run) whole warm-up at 25 peak 438, 410 MB cpu 2:12, 2:14 this commit peak 421, 456 MB cpu 2:05, 2:01 at rest after, all 183-215 MB (ps RSS reads 500-720 MB at rest in every build: macOS counts the pages Go already gave back with MADV_FREE until it takes them; heapReleased was 438-678 MB of that.) Run: - TestUnderTightGC (new): the read runs under 25 from GOGC 100 and 200, under 10 from 10, and with GC off from off; each is put back after. TestWarmUpTightensOnlyTheUsageRead: other warm-up steps run at 100. - go test -run 'TestWarmUp|TestUnderTightGC' ./internal/gui ok. - gofmt -l clean. - go test -tags nogui -count=1 ./internal/gui (the whole package) ok 77.2s; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gui ok. No other test sets or reads GOGC (grep SetGCPercent/FreeOSMemory in *_test.go). The full suite's last run (e58108c3b, the commit before) had every package ok but internal/agent at the 45m deadline under load ~120; alone it passed in 2213s (noted on #2544). --- internal/gui/sessions.go | 20 +++++++++++++++- internal/gui/warm_gc_test.go | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 internal/gui/warm_gc_test.go diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 545012860..fa5db8ce2 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -506,7 +506,7 @@ func warmSteps() []func() { return []func(){ func() { statsFor(0) }, func() { statsFor(30) }, - func() { usage.QueryPage(usage.All, usage.Filter{}, 0, 50) }, + 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() @@ -531,6 +531,24 @@ func warmUp(steps ...func()) { debug.FreeOSMemory() } +// underTightGC runs fn letting the heap grow by a quarter of what it holds +// before a collection, not Go's default of all of it, and then puts back +// 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. +func underTightGC(fn func()) { + if was := debug.SetGCPercent(tightGCPercent); was < tightGCPercent { + debug.SetGCPercent(was) + } else { + defer debug.SetGCPercent(was) + } + fn() +} + +const tightGCPercent = 25 + // openTerminal runs a command in the chosen Mac terminal through a .command // file, as it would open from Finder: no Automation consent. // The shell is left open when the agent quits. diff --git a/internal/gui/warm_gc_test.go b/internal/gui/warm_gc_test.go new file mode 100644 index 000000000..4f6f65592 --- /dev/null +++ b/internal/gui/warm_gc_test.go @@ -0,0 +1,45 @@ +package gui + +import ( + "runtime/debug" + "testing" +) + +func gcPercent() int { + p := debug.SetGCPercent(100) + debug.SetGCPercent(p) + return p +} + +// The usage page's read of the whole history runs under a tighter GOGC, so +// its heap (every row at once) isn't let grow to twice that, and puts back +// what was set when it ends; a lower GOGC, or off, is kept as it is. +func TestUnderTightGC(t *testing.T) { + defer debug.SetGCPercent(debug.SetGCPercent(100)) + for _, before := range []int{100, 200, 10, -1} { + debug.SetGCPercent(before) + var during int + underTightGC(func() { during = gcPercent() }) + want := min(before, tightGCPercent) + if before < 0 { + want = before + } + if during != want { + t.Errorf("GOGC=%d: read under %d, want %d", before, during, want) + } + if after := gcPercent(); after != before { + t.Errorf("GOGC=%d: after the read it is %d", before, after) + } + } +} + +// The warm-up's usage read is the one under it, and the warm-up's other +// reads are not. +func TestWarmUpTightensOnlyTheUsageRead(t *testing.T) { + defer debug.SetGCPercent(debug.SetGCPercent(100)) + var during int + warmUp(func() { during = gcPercent() }) + if during != 100 { + t.Fatalf("a warm-up step read under GOGC=%d, want 100", during) + } +}