diff --git a/internal/sessions/calls_agy.go b/internal/sessions/calls_agy.go index f5d21ca80..7cd92a0b2 100644 --- a/internal/sessions/calls_agy.go +++ b/internal/sessions/calls_agy.go @@ -82,7 +82,7 @@ func readAgyCalls(f file) (*callFile, bool) { defer r.Close() st := &callFile{Agent: "agy", Path: f.path, Session: strings.TrimPrefix(f.key, "agy:")} at := map[int]int{} // a step's index → its call in st.Calls - br := bufio.NewReaderSize(r, 1<<20) + br := bufio.NewReaderSize(r, scanBuffer(r, 0)) for { b, err := br.ReadBytes('\n') if err != nil && (!errors.Is(err, io.EOF) || len(bytes.TrimSpace(b)) == 0) { diff --git a/internal/sessions/calls_minimax.go b/internal/sessions/calls_minimax.go index 7a7061205..b48383518 100644 --- a/internal/sessions/calls_minimax.go +++ b/internal/sessions/calls_minimax.go @@ -106,7 +106,7 @@ func readMiniMaxCalls(f file) (*callFile, bool) { } defer r.Close() st := &callFile{Agent: "minimax-code", Path: f.path, Session: strings.TrimPrefix(f.key, "minimax-code:")} - br := bufio.NewReaderSize(r, 1<<20) + br := bufio.NewReaderSize(r, scanBuffer(r, 0)) for { b, err := br.ReadBytes('\n') if err != nil && (!errors.Is(err, io.EOF) || len(bytes.TrimSpace(b)) == 0) { diff --git a/internal/sessions/calls_workbuddy.go b/internal/sessions/calls_workbuddy.go index 00de930e8..2301eced4 100644 --- a/internal/sessions/calls_workbuddy.go +++ b/internal/sessions/calls_workbuddy.go @@ -167,7 +167,7 @@ func readWorkBuddyCalls(f file) (*callFile, bool) { defer r.Close() st := &callFile{Agent: f.agent, Path: f.path, Session: strings.TrimPrefix(f.key, f.agent+":")} dir := buddyDir(f.agent) - br := bufio.NewReaderSize(r, 1<<20) + br := bufio.NewReaderSize(r, scanBuffer(r, 0)) last := "" // the messageId the call before this line carried for { b, err := br.ReadBytes('\n') diff --git a/internal/sessions/frames.go b/internal/sessions/frames.go index 520a12674..a661e834e 100644 --- a/internal/sessions/frames.go +++ b/internal/sessions/frames.go @@ -136,7 +136,7 @@ func scanFrames(path string, off, skip int64, fn func([]byte) bool) (next, nextS return } defer dec.Close() - r := bufio.NewReaderSize(f, 1<<20) + r := bufio.NewReaderSize(f, scanBuffer(f, off)) var frame, out, pending []byte at := off first := true diff --git a/internal/sessions/scan_buffer_test.go b/internal/sessions/scan_buffer_test.go index 8844a66f1..a81278fb3 100644 --- a/internal/sessions/scan_buffer_test.go +++ b/internal/sessions/scan_buffer_test.go @@ -1,11 +1,14 @@ package sessions import ( + "context" "os" "path/filepath" "runtime" "strings" "testing" + + "github.com/klauspost/compress/zstd" ) // A scan of a small session file takes a buffer the size of the file, not @@ -61,3 +64,51 @@ func TestScanReadsALineLongerThanItsBuffer(t *testing.T) { t.Fatalf("lines %v, want [%d 2]", got, len(long)) } } + +// Every other reader of a session file sizes its buffer as scanAt does: +// the calls readers of MiniMax Code, agy and WorkBuddy, dsh's packed +// frames and a search's first look each took 1 MiB per file read (#2776). +func TestReadersOfASmallFileTakeASmallBuffer(t *testing.T) { + dir := t.TempDir() + write := func(name, body string) string { + t.Helper() + p := filepath.Join(dir, name) + if err := os.WriteFile(p, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + return p + } + line := `{"type":"user","n":1}` + "\n" + plain := write("s.jsonl", line+line) + enc, err := zstd.NewWriter(nil) + if err != nil { + t.Fatal(err) + } + framed := write("s.v3.jsonl.zstd", string(enc.EncodeAll([]byte(line+line), nil))) + enc.Close() + for _, c := range []struct { + name string + read func() + }{ + {"minimax", func() { readMiniMaxCalls(file{path: plain, key: "minimax-code:s"}) }}, + {"agy", func() { readAgyCalls(file{path: plain, key: "agy:s"}) }}, + {"workbuddy", func() { readWorkBuddyCalls(file{agent: "workbuddy", path: plain, key: "workbuddy:s"}) }}, + {"frames", func() { scanFrames(framed, 0, 0, func([]byte) bool { return true }) }}, + {"search", func() { mayHold(context.Background(), plain, "absent") }}, + } { + t.Run(c.name, func(t *testing.T) { + const reads = 50 + c.read() // anything a first read sets up once + var before, after runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&before) + for range reads { + c.read() + } + runtime.ReadMemStats(&after) + if per := (after.TotalAlloc - before.TotalAlloc) / reads; per > 256<<10 { + t.Fatalf("a read of a two-line file allocated %d bytes", per) + } + }) + } +} diff --git a/internal/sessions/search.go b/internal/sessions/search.go index f09b87dd4..00f8d4250 100644 --- a/internal/sessions/search.go +++ b/internal/sessions/search.go @@ -157,7 +157,7 @@ func mayHold(ctx context.Context, path, needle string) bool { defer f.Close() nb := []byte(needle) keep := len(nb) - 1 - buf := make([]byte, 1<<20+keep) + buf := make([]byte, scanBuffer(f, 0)+keep) have := 0 for { if ctx.Err() != nil { diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 3fd94942b..f689dd23a 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -1413,8 +1413,9 @@ func scanAt(path string, off int64, head func([]byte) bool, fn func(b []byte, st } } -// scanBuffer is the read buffer scanAt needs for what is left of f from off: -// 1 MiB at most, and no more than that rest of a plain file. Every scan +// scanBuffer is the read buffer a reader of f needs for what is left of it +// from off: 1 MiB at most, and no more than that rest of a plain file. +// Every line reader of a session file sizes its buffer by it. Every scan // took 1 MiB, and a start's read of ~4,600 mostly small session files // (each read again from where it was left) allocated ~9 GB of them, which // the GC's heap target followed up (#2776). A line longer than the buffer