From 99de8e1ad1d7934a052de67eb328929357fb3887 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 17:14:34 +0800 Subject: [PATCH] gui: the warm-up's whole-history reads hold Go to the memory it held when they began (#3493) A cold build of the request index (after any update that changes internal/usage) reads every session again and allocates ~1.2 GB to keep ~40 MB more. Even under GOGC 25 the heap overshot its goal into pages it kept, so a cold start peaked ~70 MB over a warm one. underTightGC now also sets a soft memory limit at what Go holds of the system's memory when its read begins (total mapped less released), and puts the old limit back after. A lower limit the user set is kept, and GOGC off gets none. Real data, 15.7k sessions (sbpairs: copies of the real cache/config, the real Claude Code and Codex session folders linked read-only), harness TestZZWarm running warmSteps under sessions.Warm, phys_footprint_peak, old = 9dc7d6eb8, alternated: cold (requests-v1.bin removed): old 313/308/320/323 MB, new 251/241/247/249 warm: old 247/255/250, new 211/225/228 at rest after: unchanged (old 186-200, new 174-190) user CPU, cold run: old 4.8-5.9 s, new 6.0-12.8 s (load 120-200 on the box, wall time the same: 2.8-4.9 s vs 3.0-5.9 s) Fixed 220/200 MB limits were tried first: 200 doubled CPU; a limit relative to what is held scales to other histories. Run: go test -count=1 ./internal/gui ok (132s); the first full run had one red, TestRestartHandsTheGatewayOver (handover, #3451, commented), green 3 of 3 alone. GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gui ok, gofmt clean. Broken by hand, TestUnderTightGCLimitsMemory red each time: no limit set; a limit under GOGC off; the user's lower limit replaced. Not tried: Windows with real sessions (the box has none); a history much larger than 15.7k sessions. --- internal/gui/sessions.go | 24 +++++++++++++++++++++++- internal/gui/warm_gc_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index e9a6863ff..ebaf5203a 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -9,6 +9,7 @@ import ( "os" "runtime" "runtime/debug" + "runtime/metrics" "slices" "strconv" "strings" @@ -576,15 +577,36 @@ func warmUp(steps ...func()) { // 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. +// +// It also holds the memory Go asks of the system to what it held when fn +// began, as a soft limit (a lower one the user set is kept, and GOGC off +// has none). A build of the request index after an update reads every +// session again and allocates ~1.2 GB to keep ~40 MB more, and the heap +// overshot its goal into pages it then kept: 308–323 MB in Activity +// Monitor for 15.7k sessions, where a start that reads the index peaked at +// 247–255 MB. Under the limit, 241–251 MB and 211–228 MB, for 1–7 s more +// CPU in that build. func underTightGC(fn func()) { - if was := debug.SetGCPercent(tightGCPercent); was < tightGCPercent { + was := debug.SetGCPercent(tightGCPercent) + if was < tightGCPercent { debug.SetGCPercent(was) } else { defer debug.SetGCPercent(was) } + if limit := heldMemory(); was >= 0 && limit < debug.SetMemoryLimit(-1) { + defer debug.SetMemoryLimit(debug.SetMemoryLimit(limit)) + } fn() } +// heldMemory is what Go holds of the system's memory: all it mapped, less +// the pages it gave back. +func heldMemory() int64 { + s := []metrics.Sample{{Name: "/memory/classes/total:bytes"}, {Name: "/memory/classes/heap/released:bytes"}} + metrics.Read(s) + return int64(s[0].Value.Uint64() - s[1].Value.Uint64()) +} + const tightGCPercent = 25 // openTerminal runs a command in the chosen Mac terminal through a .command diff --git a/internal/gui/warm_gc_test.go b/internal/gui/warm_gc_test.go index 4f6f65592..76e39b5c1 100644 --- a/internal/gui/warm_gc_test.go +++ b/internal/gui/warm_gc_test.go @@ -1,6 +1,7 @@ package gui import ( + "math" "runtime/debug" "testing" ) @@ -33,6 +34,38 @@ func TestUnderTightGC(t *testing.T) { } } +// Under it the memory Go holds is limited to what it held when the read +// began, and the limit set before is put back; a lower limit the user set +// is kept, and GOGC off gets none. +func TestUnderTightGCLimitsMemory(t *testing.T) { + defer debug.SetGCPercent(debug.SetGCPercent(100)) + defer debug.SetMemoryLimit(debug.SetMemoryLimit(math.MaxInt64)) + for _, c := range []struct { + name string + gogc int + before int64 + low bool + }{ + {"default", 100, math.MaxInt64, true}, + {"user limit lower", 100, 1 << 20, false}, + {"GOGC off", -1, math.MaxInt64, false}, + } { + debug.SetGCPercent(c.gogc) + debug.SetMemoryLimit(c.before) + var during int64 + underTightGC(func() { during = debug.SetMemoryLimit(-1) }) + if c.low && during >= c.before { + t.Errorf("%s: read under limit %d, want what Go held", c.name, during) + } + if !c.low && during != c.before { + t.Errorf("%s: read under limit %d, want %d kept", c.name, during, c.before) + } + if after := debug.SetMemoryLimit(-1); after != c.before { + t.Errorf("%s: after the read the limit is %d, want %d", c.name, after, c.before) + } + } +} + // 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) {