sessions: a line longer than the read buffer is gathered once, at its own size (#2515)
scanAt gathered a line longer than its read buffer by appending one
buffer's worth at a time. Past 256 elements append grows a slice by
1.25x, so an 8 MiB line allocated ~37 MB, and a line past maxLine was
gathered to 32 MiB before being dropped (~175 MB allocated). The session
list and the call readers both read every session file this way at a
start, so a history whose prompts carry pasted screenshots (each a
multi-MB base64 line) allocated GBs in scanAt alone.
On the first overflow of a plain file's line, scanAt now reads ahead
(ReadAt, which moves nothing) to the line's end and gathers it into one
buffer of exactly that size. A line longer than maxLine plus the buffer
is stepped over at once, as before, but without being gathered first. A
packed (zstd) file, or a line with no end yet, keeps the old path.
Tests (internal/sessions/scan_buffer_test.go):
- TestScanGathersALongLineOnce: an 8 MiB line and a short one are both
handed over whole, and the scan allocates at most 1.5x the line.
Fixed: 9,451,632 bytes for an 8,388,633-byte line (bound 12,582,949).
With the read-ahead turned off by hand: red, "allocated 36856408
bytes".
- TestScanStepsOverATooLongLineUngathered: a maxLine+4 MiB line is
stepped over and the next line read, with at most 8 MiB allocated.
Fixed: 1,049,032 bytes. Turned off by hand: red, "allocated 175179080
bytes".
Measured: a sandbox `magpie web` on a fixture HOME (150 Claude Code
sessions, each with six 4 MiB pasted-image prompts, 3.5 GB), main and
panel pages loaded in Playwright Chromium, heap profile at ~2.5 min,
macOS at a load average of ~290, two runs each:
allocated in scanAt peak RSS
base 5b267fbd8 3.97 GB 3.31 GB 394 MB
base 5b267fbd8 4.05 GB 3.37 GB 277 MB
this commit 1.52 GB 1.14 GB 109 MB (pages failed to load)
this commit 1.89 GB 1.20 GB 348 MB
What allocation shows is halved; peak RSS on this loaded machine is
noise (277-394 MB on the base) and does not show a change. The #2515
report's 3.2 GiB peak (v0.1.1265, Windows) was not reproduced: on a
5,501-file fixture v0.1.1265 peaked at 250 MB RSS and the base at 288
MB, and on 950 MB of large sessions the base peaked at 256 MB. This
commit is not claimed to be its cause; it is the largest start
allocation left on these fixtures.
Run in a sandbox HOME, Go caches pinned:
- go test -count=1 -timeout 45m ./internal/sessions/ ./internal/usage/:
both ok (959s, 272s), set -o pipefail.
- gofmt -l internal/sessions: none. go vet ./internal/sessions/,
GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet -tags
nogui ./...: all exit 0.
- Not run: the full suite, the GUI suite (no asset changed), the
Windows box.
This commit is contained in:
@@ -112,3 +112,57 @@ func TestReadersOfASmallFileTakeASmallBuffer(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// scanAllocated is what one scan of the file at p allocated, and the
|
||||
// lengths of the lines it handed over.
|
||||
func scanAllocated(t *testing.T, p string) (uint64, []int) {
|
||||
t.Helper()
|
||||
var got []int
|
||||
var before, after runtime.MemStats
|
||||
runtime.GC()
|
||||
runtime.ReadMemStats(&before)
|
||||
if _, err := scanAt(p, 0, nil, func(b []byte, _, _ int64) bool { got = append(got, len(b)); return true }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime.ReadMemStats(&after)
|
||||
return after.TotalAlloc - before.TotalAlloc, got
|
||||
}
|
||||
|
||||
// A line longer than the read buffer (a pasted screenshot's base64) is
|
||||
// gathered into one buffer of its own size. Grown a buffer's worth at a
|
||||
// time it allocated ~5 times its length, for each reader of each file, and
|
||||
// a start of sessions holding images allocated GBs of it (#2515).
|
||||
func TestScanGathersALongLineOnce(t *testing.T) {
|
||||
p := filepath.Join(t.TempDir(), "s.jsonl")
|
||||
long := `{"type":"user","data":"` + strings.Repeat("x", 8<<20) + `"}`
|
||||
if err := os.WriteFile(p, []byte(long+"\n{}\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
alloc, got := scanAllocated(t, p)
|
||||
if len(got) != 2 || got[0] != len(long) || got[1] != 2 {
|
||||
t.Fatalf("lines %v, want [%d 2]", got, len(long))
|
||||
}
|
||||
// the line once and the 1 MiB read buffer, with room for both; grown
|
||||
// a chunk at a time it was ~38 MiB
|
||||
if want := uint64(len(long)) * 3 / 2; alloc > want {
|
||||
t.Fatalf("a scan of an %d-byte line allocated %d bytes, want at most %d", len(long), alloc, want)
|
||||
}
|
||||
}
|
||||
|
||||
// A line past maxLine is stepped over without being gathered first.
|
||||
func TestScanStepsOverATooLongLineUngathered(t *testing.T) {
|
||||
p := filepath.Join(t.TempDir(), "s.jsonl")
|
||||
long := `{"type":"user","data":"` + strings.Repeat("x", maxLine+(4<<20)) + `"}`
|
||||
if err := os.WriteFile(p, []byte(long+"\n{}\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
alloc, got := scanAllocated(t, p)
|
||||
if len(got) != 1 || got[0] != 2 {
|
||||
t.Fatalf("lines %v, want [2]", got)
|
||||
}
|
||||
// the 1 MiB read buffer and the look ahead, with room; gathered first
|
||||
// it was ~150 MiB
|
||||
if alloc > 8<<20 {
|
||||
t.Fatalf("a scan stepping over a %d-byte line allocated %d bytes", len(long), alloc)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1383,6 +1383,15 @@ func scanAt(path string, off int64, head func([]byte) bool, fn func(b []byte, st
|
||||
seen = true
|
||||
skip = skip || !head(chunk)
|
||||
}
|
||||
if pf, ok := f.(*os.File); ok && !skip && len(long) == 0 {
|
||||
// gathered in one buffer of the line's own size, not grown a
|
||||
// chunk at a time (~5 times the line allocated, #2515)
|
||||
if l := lineLen(pf, off, maxLine+r.Size()); l > maxLine+r.Size() {
|
||||
skip = true
|
||||
} else if l > cap(long) {
|
||||
long = make([]byte, 0, l)
|
||||
}
|
||||
}
|
||||
if !skip && len(long)+len(chunk) <= maxLine {
|
||||
long = append(long, chunk...)
|
||||
} else {
|
||||
@@ -1413,6 +1422,25 @@ func scanAt(path string, off int64, head func([]byte) bool, fn func(b []byte, st
|
||||
}
|
||||
}
|
||||
|
||||
// lineLen is the length, its newline included, of the line at off in f,
|
||||
// read ahead without moving f: 0 when it has no end yet, and past most
|
||||
// (but no further) for a longer one.
|
||||
func lineLen(f *os.File, off int64, most int) int {
|
||||
buf := make([]byte, 64<<10)
|
||||
n := 0
|
||||
for n <= most {
|
||||
k, err := f.ReadAt(buf, off+int64(n))
|
||||
if i := bytes.IndexByte(buf[:k], '\n'); i >= 0 {
|
||||
return n + i + 1
|
||||
}
|
||||
n += k
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
Reference in New Issue
Block a user