sessions: every other reader of a session file sizes its buffer by what is left of it, as scanAt does (#2776)
d7e02ae08sized scanAt's buffer by the rest of the file. The other line readers of a session file still took 1 MiB per file read: - readMiniMaxCalls, readAgyCalls and readWorkBuddyCalls (WorkBuddy and CodeBuddy Code), which read a changed file whole; - scanFrames, dsh's packed (.zstd) sessions, read on from a frame; - mayHold, a search's first look at each file. (calls_pi, calls_kimi, calls_dsh and dsh.go no longer hold one since the incremental readers before a77f6ae16.) Each now takes scanBuffer(f, off): the rest of a plain file plus one byte, 16 KiB to 1 MiB. ReadBytes and readFrame read a line or frame longer than the buffer whole, as before; a packed WorkBuddy file (a zstd stream, not an *os.File) keeps 1 MiB. TestReadersOfASmallFileTakeASmallBuffer reads a two-line file 50 times through each of the five and allows 256 KiB per read. With the five readers put back (the test kept): red in all five, 1,049,161 (minimax), 1,049,271 (agy), 1,049,256 (workbuddy), 1,068,296 (frames) and 1,056,984 (search) bytes per read. Not measured on the 600k sandbox: it holds no MiniMax, agy, WorkBuddy or dsh sessions, so the saving there is the test's: ~1 MiB per file each time one of these is read again. Full suite on this series' final tree (aa678b482 before this note was added), rebased on origin/mainf76201a84: go test -p 4 ./... -timeout 45m, exit 0, 64 packages ok and none failed (gateway 1783 s, gui 361 s), 41 minutes at load ~190-200. gofmt -l: no files. go vet: darwin, linux (-tags nogui) and windows exit 0. The GUI suite wasn't run: no file under internal/gui/assets changed.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user