Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gui: a Sessions or Usage poll's reads are collected under GOGC 25, and two reads under it at once put back what was set before
yetone353519f55760
diff --git a/internal/gui/api.go b/internal/gui/api.goindex c67006274..d1c826e68 100644--- a/internal/gui/api.go+++ b/internal/gui/api.go@@ -2401,6 +2401,13 @@ func held(h http.Handler) http.Handler {defer sessions.Ask()()// and what the read threw away goes back once the page restsdefer readingPage()()+ // and collected under GOGC 25 while it runs: a poll's reads+ // allocated ~330 MB to keep ~105 MB, and at the default 100 the+ // heap grew to twice what it kept before a collection, ~320 MB+ // in Activity Monitor during a poll. The memory limit the+ // warm-up's read takes too isn't taken here: held as close as+ // heldMemory, every poll collected over and over, 5x the CPU.+ defer tightenGC(false)()}if !(api && r.Method == http.MethodGet) {// anything that may have written drops what was held, once itdiff --git a/internal/gui/sessions.go b/internal/gui/sessions.goindex f3415c086..8d205244f 100644--- a/internal/gui/sessions.go+++ b/internal/gui/sessions.go@@ -599,16 +599,56 @@ func warmUp(steps ...func()) {// 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()) {- was := debug.SetGCPercent(tightGCPercent)- if was < tightGCPercent {- debug.SetGCPercent(was)- } else {- defer debug.SetGCPercent(was)+ defer tightenGC(true)()+ fn()+}++// tightGC is what the reads under a tight GC hold: the first to begin sets+// it, and the last to end puts back what was set before it. Each read+// putting back what it found let two at once leave GOGC at 25 for good,+// when the first to begin ended before the second.+var tightGC struct {+ sync.Mutex+ n, limits int // reads under it, and those of them limited+ was int // GOGC before the first+ limit int64 // the memory limit before the first limited one+ limited bool // whether that one set one+}++// tightenGC begins a read under a tight GC, with the memory limit too when+// limit is set; undo ends it.+func tightenGC(limit bool) (undo func()) {+ tightGC.Lock()+ defer tightGC.Unlock()+ if tightGC.n == 0 {+ was := debug.SetGCPercent(tightGCPercent)+ if was < tightGCPercent {+ debug.SetGCPercent(was)+ }+ tightGC.was = was}- if limit := heldMemory(); was >= 0 && limit < debug.SetMemoryLimit(-1) {- defer debug.SetMemoryLimit(debug.SetMemoryLimit(limit))+ tightGC.n+++ if limit {+ if tightGC.limits == 0 {+ tightGC.limited = false+ if held := heldMemory(); tightGC.was >= 0 && held < debug.SetMemoryLimit(-1) {+ tightGC.limit, tightGC.limited = debug.SetMemoryLimit(held), true+ }+ }+ tightGC.limits++}- fn()+ return sync.OnceFunc(func() {+ tightGC.Lock()+ defer tightGC.Unlock()+ if limit {+ if tightGC.limits--; tightGC.limits == 0 && tightGC.limited {+ debug.SetMemoryLimit(tightGC.limit)+ }+ }+ if tightGC.n--; tightGC.n == 0 {+ debug.SetGCPercent(tightGC.was)+ }+ })}// heldMemory is what Go holds of the system's memory: all it mapped, lessdiff --git a/internal/gui/warm_gc_test.go b/internal/gui/warm_gc_test.goindex 76e39b5c1..f9b866e39 100644--- a/internal/gui/warm_gc_test.go+++ b/internal/gui/warm_gc_test.go@@ -66,6 +66,37 @@ func TestUnderTightGCLimitsMemory(t *testing.T) {}}+// Two reads under it at once put back what was set before the first, in+// either order they end: each putting back what it found left the second's+// 25 when the first ended before it.+func TestTightGCOverlappingReads(t *testing.T) {+ defer debug.SetGCPercent(debug.SetGCPercent(100))+ defer debug.SetMemoryLimit(debug.SetMemoryLimit(math.MaxInt64))+ for _, firstEndsFirst := range []bool{true, false} {+ first := tightenGC(true)+ second := tightenGC(false)+ if p := gcPercent(); p != tightGCPercent {+ t.Fatalf("two reads under GOGC=%d, want %d", p, tightGCPercent)+ }+ if firstEndsFirst {+ first()+ } else {+ second()+ }+ if p := gcPercent(); p != tightGCPercent {+ t.Errorf("one read still in flight under GOGC=%d, want %d", p, tightGCPercent)+ }+ first()+ second()+ if p := gcPercent(); p != 100 {+ t.Errorf("first ends first %v: after both GOGC=%d, want 100", firstEndsFirst, p)+ }+ if l := debug.SetMemoryLimit(-1); l != math.MaxInt64 {+ t.Errorf("first ends first %v: after both the limit is %d", firstEndsFirst, l)+ }+ }+}+// 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) {