gui: a cold start's turn lets the matcher's scratch go when it ends (#2278)

Each turn of the warm-up and of a page's first ask (coldTurns) now ends
with usage.ReleaseMatchScratch: the request matcher's kept maps, picks
and row buffers, which sat for scratchLinger (30 s) under the next
turn's read of the history. The priced blocks stay, so the next turn's
queries don't price the history again.

On mote, a sandbox HOME with the real history (13k sessions, ~720k
gateway calls), 75 s with ten answers asked every 5 s, phys_footprint
peak:
- start with its caches: 260 and 263 MB, against 274 and 285 MB at
  v0.1.2284.
- first start, run alternately with v0.1.2284: 359, 359 and 361 MB
  against 360, 360 and 364. CPU 3:41-3:47 against 3:35-3:41. The
  slowest first answer was 31.5-32.4 s against 30.9-31.8 s.
- the heatmap and the requests answers are byte for byte the same.
Dropped: releasing the priced blocks too (ReleaseScratch). That gave
253-255 MB with caches, but 362-369 MB on a first start, which priced
the history again in each turn.

Run on mote at 03d21ab8e + this:
- go vet ./...: ok.
- go test ./internal/usage: ok.
- go test ./internal/gui: red only in ten real-window AppKit tests,
  killed at their deadlines under load. They are the same ten that fail
  on a clean a6951cc63 (#4125, #2645).
- TestMatchScratchGoesPricedBlocksStay goes red with
  ReleaseMatchScratch made a no-op, and red when it releases the priced
  blocks too.
- gofmt -l: clean.
Not run: Linux vet of internal/gui (no gtk4 on wsl-run). After this was
run, the rebase onto origin/main brought in 11fc687c0 (a GUI test's
wait), 06c2c35d1 (Claude's interactive start) and 6ba593de0 (the main
window's passing note). They touch none of these files.
This commit is contained in:
yetone
2026-10-31 08:31:15 +08:00
parent 6ba593de00
commit 86e19b9b74
3 changed files with 58 additions and 2 deletions
+10 -2
View File
@@ -709,7 +709,7 @@ func warmUp(steps ...func()) {
for _, step := range steps {
func() {
coldTurns.turn <- struct{}{}
defer func() { <-coldTurns.turn }()
defer coldTurnDone()
step()
}()
}
@@ -728,6 +728,14 @@ var coldTurns = struct {
over atomic.Bool
}{turn: make(chan struct{}, 1)}
// coldTurnDone ends a turn. What the request matching kept for a next
// read goes with it: kept for scratchLinger, it sat under the next
// turn's read of the history.
func coldTurnDone() {
usage.ReleaseMatchScratch()
<-coldTurns.turn
}
// coldTurnMost is how long a page waits for its turn: a warm-up stuck on
// one read never keeps the pages from answering.
const coldTurnMost = 2 * time.Minute
@@ -744,7 +752,7 @@ func coldTurn(h http.HandlerFunc) http.HandlerFunc {
t := time.NewTimer(coldTurnMost)
select {
case coldTurns.turn <- struct{}{}:
defer func() { <-coldTurns.turn }()
defer coldTurnDone()
case <-t.C:
}
t.Stop()
+40
View File
@@ -116,3 +116,43 @@ func TestPricedBlocksGoWithTheScratch(t *testing.T) {
holding.release()
ReleaseScratch()
}
// A turn of a cold start lets go of the matcher's scratch, so the next
// turn's read doesn't sit on it, and keeps the priced blocks the next
// turn's queries read again (#2278).
func TestMatchScratchGoesPricedBlocksStay(t *testing.T) {
t.Cleanup(ReleaseScratch)
now := time.Date(2026, 10, 1, 13, 0, 0, 0, time.UTC)
g, l := pairFixture(12, 9)
ReleaseScratch()
pairRows(All, now, g, l, nil, nil)
if keptMatch.Load() == nil || keptPicks.Load() == nil {
t.Fatal("nothing kept for the next call")
}
keepRows(new(rowBuffer))
var raw rowChunk
for i := 0; i < logBlockRows; i++ {
raw.add(Row{Record: Record{Time: now, Provider: "openai", Model: "gpt-5", Input: 100 + i, Output: 10}}, "", int64(i), false)
}
packed := raw.pack()
meta := sha256.Sum256([]byte("turn"))
b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })
b.share(meta)
c := b.block(packed)
b.release()
ReleaseMatchScratch()
if keptMatch.Load() != nil || keptPicks.Load() != nil || keptSeen.Load() != nil {
t.Fatal("the matcher's scratch was kept")
}
for i := range keptRows {
if keptRows[i].Load() != nil {
t.Fatal("a row buffer was kept")
}
}
next := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })
next.share(meta)
defer next.release()
if next.block(packed) != c {
t.Fatal("the priced block went with the scratch")
}
}
+8
View File
@@ -1788,6 +1788,14 @@ func ReleaseScratch() {
releasePriced()
}
// ReleaseMatchScratch is ReleaseScratch keeping the priced blocks.
func ReleaseMatchScratch() {
keptMatch.Store(nil)
keptPicks.Store(nil)
keptSeen.Store(nil)
releaseRows()
}
// scratchLinger is how long the matcher's scratch is kept once the last
// pairing put it back. While an agent works every poll pairs again and
// finds it kept; with nothing new to pair, a poll is answered from the