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.
This commit is contained in:
yetone
2026-10-22 00:04:28 +08:00
committed by yetone
parent b3f5953495
commit b1a2eec2c1
2 changed files with 14 additions and 4 deletions
+5 -3
View File
@@ -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)
+9 -1
View File
@@ -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)