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) + } +}