diff --git a/internal/sessions/scan_buffer_test.go b/internal/sessions/scan_buffer_test.go index 87fb90e5e..a18d97fe4 100644 --- a/internal/sessions/scan_buffer_test.go +++ b/internal/sessions/scan_buffer_test.go @@ -214,3 +214,71 @@ func TestALineKeptPastItsScanIsGarbageInTests(t *testing.T) { t.Fatalf("a line kept past its scan reads %q", b) } } + +// A packed rollout's long line (a pasted screenshot in a prompt) has no +// length to read ahead, so it was grown a read buffer at a time and +// allocated ~3.8 times its size; a cold read of 120 real packed rollouts +// allocated 1.6 GB of 2.2 GB there. It is now gathered once, at its own +// size, and handed over whole; a line its head says no to is not gathered +// at all. +func TestScanGathersAPackedLongLineOnce(t *testing.T) { + p := filepath.Join(t.TempDir(), "rollout.jsonl.zst") + wanted := `{"type":"want","data":"` + strings.Repeat("x", 20<<20) + `"}` + unwanted := `{"type":"skip","data":"` + strings.Repeat("y", 20<<20) + `"}` + enc, err := zstd.NewWriter(nil) + if err != nil { + t.Fatal(err) + } + body := enc.EncodeAll([]byte(wanted+"\n{}\n"+unwanted+"\n{}\n"), nil) + enc.Close() + if err := os.WriteFile(p, body, 0o600); err != nil { + t.Fatal(err) + } + skip := func(b []byte) bool { return !bytes.Contains(b[:min(len(b), 64)], []byte(`"skip"`)) } + none := func([]byte) bool { return false } // sets up the decoder a packed scan keeps + scan := func(head func([]byte) bool) (uint64, []int) { + var got []int + var before, after runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&before) + if _, err := scanAt(p, 0, head, func(b []byte, _, _ int64) bool { + if len(b) > 2 && !bytes.Equal(b, []byte(wanted)) { + t.Fatalf("a %d-byte line handed over is not the one written", len(b)) + } + got = append(got, len(b)) + return true + }); err != nil { + t.Fatal(err) + } + runtime.ReadMemStats(&after) + return after.TotalAlloc - before.TotalAlloc, got + } + if _, got := scan(none); len(got) != 0 { + t.Fatalf("lines %v handed over, want none", got) + } + alloc, got := scan(skip) + if len(got) != 3 || got[0] != len(wanted) || got[1] != 2 || got[2] != 2 { + t.Fatalf("lines %v, want [%d 2 2]", got, len(wanted)) + } + // the line once at its size, and at worst once more into blocks the + // pool let go, with 16 MiB for the decoder's window; grown a chunk at + // a time it was ~107 MB past the window + if most := uint64(len(wanted))*5/2 + 16<<20; alloc > most { + t.Fatalf("a scan of a packed %d-byte line allocated %d bytes, want at most %d", len(wanted), alloc, most) + } +} + +// Blocks give back what they were given in order, across a block's end. +func TestBlocksKeepALineInOrder(t *testing.T) { + var g blocks + defer g.release() + var want []byte + for i, n := range []int{700 << 10, 900 << 10, 1, 1 << 20, 3} { + p := bytes.Repeat([]byte{byte('a' + i)}, n) + g.add(p) + want = append(want, p...) + } + if got := g.appendTo([]byte("x")); !bytes.Equal(got, append([]byte("x"), want...)) || g.n != len(want) { + t.Fatalf("blocks gave back %d bytes (n %d), not the %d given", len(got)-1, g.n, len(want)) + } +} diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index ab9bce45e..a010805bf 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -1408,7 +1408,12 @@ func scanAt(path string, off int64, head func([]byte) bool, fn func(b []byte, st return off, err } } + pf, plain := f.(*os.File) var long []byte + var kept *[]byte // long's buffer, from longLines + defer func() { giveLong(kept, long) }() + var parts blocks // a packed file's long line so far + defer parts.release() var n int64 // bytes of the line so far skip, seen := false, false for { @@ -1419,19 +1424,28 @@ 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 { + if plain && !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) + kept, long = takeLong(l) } } - if !skip && len(long)+len(chunk) <= maxLine { - long = append(long, chunk...) - } else { + switch { + case skip || len(long)+parts.n+len(chunk) > maxLine: skip, long = true, long[:0] + parts.release() + case plain: + long = append(long, chunk...) + default: + // a packed file's line has no length to read ahead: it is + // kept in pooled blocks until its end, then gathered once + // at its own size. Grown by append it allocated ~3.8 times + // the line, most of what a cold read of packed rollouts + // with pasted images allocated. + parts.add(chunk) } continue } @@ -1441,6 +1455,13 @@ func scanAt(path string, off int64, head func([]byte) bool, fn func(b []byte, st } return off, err } + if parts.n > 0 { + if l := parts.n + len(chunk); l > cap(long) { + kept, long = takeLong(l) + } + long = parts.appendTo(long[:0]) + parts.release() + } b := chunk if len(long) > 0 { long = append(long, chunk...) @@ -1458,6 +1479,77 @@ func scanAt(path string, off int64, head func([]byte) bool, fn func(b []byte, st } } +// longLines keep the buffers scanAt gathers long lines in between scans: +// a line handed out from one is only good until the scan ends, as one +// from its read buffer is. +var longLines sync.Pool + +// takeLong is an empty buffer of at least l bytes, from the pool when it +// has one that large (the one a scan had, too small, is let go). +func takeLong(l int) (*[]byte, []byte) { + if p, _ := longLines.Get().(*[]byte); p != nil { + if cap(*p) >= l { + return p, (*p)[:0] + } + longLines.Put(p) + } + b := make([]byte, 0, l) + return &b, b +} + +// giveLong gives the buffer long was gathered in back to the pool. +func giveLong(p *[]byte, long []byte) { + if p == nil { + return + } + *p = long[:cap(long)] + if poisonReleased { + for i := range *p { + (*p)[i] = 0xFF + } + } + *p = (*p)[:0] + longLines.Put(p) +} + +// blocks hold a long line read from a packed file until its end is found: +// copied into 1 MiB blocks kept between scans, so the line is allocated +// only once, at its own length. +type blocks struct { + b []*[]byte + n int // bytes held +} + +var gatherBlocks = sync.Pool{New: func() any { b := make([]byte, 0, 1<<20); return &b }} + +func (g *blocks) add(p []byte) { + g.n += len(p) + for len(p) > 0 { + if len(g.b) == 0 || len(*g.b[len(g.b)-1]) == cap(*g.b[len(g.b)-1]) { + g.b = append(g.b, gatherBlocks.Get().(*[]byte)) + } + last := g.b[len(g.b)-1] + k := min(len(p), cap(*last)-len(*last)) + *last = append(*last, p[:k]...) + p = p[k:] + } +} + +func (g *blocks) appendTo(dst []byte) []byte { + for _, b := range g.b { + dst = append(dst, *b...) + } + return dst +} + +func (g *blocks) release() { + for _, b := range g.b { + *b = (*b)[:0] + gatherBlocks.Put(b) + } + g.b, g.n = g.b[:0], 0 +} + // 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.