sessions: the warm-up lists the Codex rollouts once for all its reads (#3493)

The warm-up at start ran three reads within about two seconds: the
stats of every day, the stats of 30 days and the Requests page. Each
listed every Codex rollout again through codexFiles, about 12k lstat()s
and ~47 MB allocated a time. codexFiles now keeps its listing while
only the warm-up reads (quiet: inside Warm, no page asking) and for 10s
at most, the age statsFor's memo already answers with. Each caller gets
its own copy. A page asking meanwhile, and every read outside the
warm-up, lists fresh, and Warm drops the kept listing when it ends.

Measured on the owner's real sessions (sbpairs: Claude Code and Codex
history, warm index), the gui warm-up harness inside sessions.Warm, run
alternately with the parent b686a8dfe, three runs each:
  allocated through step 3: 990/826/811 MB -> 751/732/734 MB
  step 1 (stats of 30 days): 436/134/114 ms -> 65/57/57 ms
  peak live heap: 170/163/160 MB -> 157/153/153 MB
  phys_footprint_peak: 259/242/234 MB -> 233/250/250 MB (no change)
Step 2 and the whole warm-up time are within noise.

Run at this tree (b686a8dfe + this change):
  go test -count=1 -timeout 45m ./internal/sessions/  ok
  go test -count=1 -timeout 45m ./internal/usage/  one red,
    TestUsageReadAfterAppendHashesNothingOnAFineClock/whole-second_stamp,
    which is #3511/#3551; alone -count=5 it passes. That path never
    enters Warm, so it never takes the kept listing.
  go test -count=1 -run 'Warm|Session' ./internal/gui/  ok
  GOOS=linux and GOOS=windows go vet -tags nogui ./internal/sessions/, gofmt -l: clean
Broken by hand: codexFiles always listing fresh makes
TestWarmUpListsRolloutsOnce red ("warm-up listed again"); handing back the
kept slice itself instead of a copy makes it red ("a caller's change
reached the kept listing").
Not tried: Windows (the box is unreachable over ssh).
This commit is contained in:
yetone
2026-10-25 15:29:58 +08:00
parent ee715c2e0e
commit 04439c8592
3 changed files with 78 additions and 1 deletions
+4 -1
View File
@@ -35,7 +35,10 @@ func scanReaders() int { return max(2, runtime.NumCPU()/2) }
// Warm runs fn, the reads nobody waits on yet, with few readers.
func Warm(fn func()) {
warming.Add(1)
defer warming.Add(-1)
defer func() {
warming.Add(-1)
forgetWarmListing()
}()
fn()
}
+35
View File
@@ -616,6 +616,41 @@ const zstSuffix = ".zst"
// reading CodexDir alone left out every other account's sessions and
// calls (phantom3792 on X).
func codexFiles() []file {
if !quiet() {
return listCodexFiles()
}
warmListing.Lock()
defer warmListing.Unlock()
if warmListing.files != nil && time.Since(warmListing.at) < warmListingFor {
return slices.Clone(warmListing.files)
}
files := listCodexFiles()
warmListing.at, warmListing.files = time.Now(), slices.Clone(files)
return files
}
// warmListing is codexFiles' listing kept through the warm-up: its reads
// (the stats of every day and of 30, the Requests page) each listed every
// rollout again within a few seconds, ~12k lstat()s and ~45 MB a time
// (#3493). It is kept only while nobody but the warm-up reads (quiet) and
// for warmListingFor at most, the age statsFor's memo already answers
// with; Warm lets it go when it ends.
var warmListing struct {
sync.Mutex
at time.Time
files []file
}
const warmListingFor = 10 * time.Second
func forgetWarmListing() {
warmListing.Lock()
warmListing.files = nil
warmListing.Unlock()
}
// listCodexFiles is codexFiles listed now.
func listCodexFiles() []file {
var out []file
for _, dir := range CodexDirs() {
out = append(out, codexFilesIn(dir)...)
+39
View File
@@ -0,0 +1,39 @@
package sessions
import (
"path/filepath"
"testing"
)
// The warm-up's reads list the rollouts once between them (#3493), and only
// they do: a page asking meanwhile, and every read after the warm-up, sees
// a rollout written since.
func TestWarmUpListsRolloutsOnce(t *testing.T) {
d := setupCalls(t)
day := filepath.Join(d.codex, "sessions", "2026", "09", "20")
first := filepath.Join(day, "rollout-2026-09-20T10-00-00-0190aaaa-1111-7222-8333-444455556666.jsonl")
second := filepath.Join(day, "rollout-2026-09-20T11-00-00-0190bbbb-1111-7222-8333-444455556666.jsonl")
writeLines(t, first, cxTurnLines(1, "turn-1", "gpt-6-astra", "low")...)
Warm(func() {
if n := len(codexFiles()); n != 1 {
t.Fatalf("warm-up's first listing: %d rollouts", n)
}
writeLines(t, second, cxTurnLines(1, "turn-1", "gpt-6-astra", "low")...)
fs := codexFiles()
if len(fs) != 1 {
t.Fatalf("warm-up listed again: %d rollouts", len(fs))
}
fs[0] = file{}
if again := codexFiles(); len(again) != 1 || again[0].path != first {
t.Fatalf("a caller's change reached the kept listing: %+v", again)
}
done := Ask()
if n := len(codexFiles()); n != 2 {
t.Fatalf("a page asking got the warm-up's listing: %d rollouts", n)
}
done()
})
if n := len(codexFiles()); n != 2 {
t.Fatalf("after the warm-up: %d rollouts", n)
}
}