sessions: a call shard's frames are read into a buffer kept between shards (#3493)

A cold Requests build (the first start after an update that changed the
usage package's source, so the request chunks are made again) reads
every call shard, ~15.7k on the real data copy. Each frame was read
into a new byte slice and decoded into a new callFrame, so gob made a
new Patches and Strings slice for every one. loadCalls now takes a
frameBuffer from a pool. gob decodes a slice that has room in place,
and leaves a field of the zero value as the field was, so each patch
and string of the last frame is cleared before the next is decoded in.
Nothing read out points into the buffer: gob copies strings and byte
slices, and a patch's call is copied into the file's calls.

Measured on the real data copy (sbpairs), warm-up harness TestZZWarm,
requests-v1.bin removed before each run so every run is a cold build,
three runs each:
  allocated through step 3   2139/2214/2182 MB -> 2001/2041/2044 MB
  loadCalls (cum, step 2)    781 MB -> 583 MB
  peak footprint             374-441 MB -> 371-419 MB, within noise
Warm starts unchanged (step 2 ~690 MB either way).

Run at this head: go test ./internal/sessions ./internal/usage ok;
-race -count=2 of the LoadedCalls, CallCache, Shard and Frame tests ok;
GOOS=linux and windows go vet -tags nogui ./internal/sessions ok; gofmt
clean. TestLoadedCallsKeepNothingOfTheLastShard reads a shard with
tokens, a session and a compaction, then one with none, three times;
broken by hand (the patches not cleared) it fails.

Not tried: Linux and Windows real-data runs.
This commit is contained in:
yetone
2026-10-25 15:18:40 +08:00
parent ba301e98e2
commit b686a8dfec
2 changed files with 75 additions and 3 deletions
+37 -3
View File
@@ -515,12 +515,41 @@ func callText(c *Call) [13]*string {
return [13]*string{&c.Agent, &c.Session, &c.ThreadID, &c.Model, &c.Requested, &c.Effort, &c.Cwd, &c.Upstream, &c.AccountID, &c.UserID, &c.Error, &c.ErrorText, &c.File}
}
// frameBuffer is what loadCalls reads a shard's frames into, kept between
// shards: a cold Requests build reads every one of ~15.6k, and a new
// buffer, slice of patches and slice of strings for each frame came to
// ~400 MB of it (#3493). Nothing read out of a frame points into these:
// gob copies every string and byte slice out of its input, and a patch's
// call is copied into the file's calls.
type frameBuffer struct {
data []byte
f callFrame
}
var frameBuffers = sync.Pool{New: func() any { return new(frameBuffer) }}
// frame is k's frame emptied for the next to be decoded into it. gob
// leaves out a field of the zero value and leaves the field it decodes
// into as it was, so a patch or string is cleared from the last frame,
// not only cut off: a reused slice is decoded into in place.
func (k *frameBuffer) frame() *callFrame {
clear(k.f.Patches)
clear(k.f.Strings)
k.f = callFrame{Strings: k.f.Strings[:0], Patches: k.f.Patches[:0]}
return &k.f
}
func loadCalls(f file) *callFile {
in, err := os.Open(callCachePath(f.path))
if err != nil {
return nil
}
defer in.Close()
k := frameBuffers.Get().(*frameBuffer)
defer func() {
k.frame() // the pool holds no file's strings
frameBuffers.Put(k)
}()
var st *callFile
var calls []Call
msgs := map[string]int{}
@@ -538,12 +567,17 @@ func loadCalls(f file) *callFile {
if size == 0 || size > 256<<20 {
return nil
}
data := make([]byte, int(size))
data := k.data[:0]
if cap(data) < int(size) {
data = make([]byte, int(size))
}
data = data[:size]
k.data = data
if _, err = io.ReadFull(in, data); err != nil || crc32.ChecksumIEEE(data) != binary.LittleEndian.Uint32(header[4:]) {
return nil
}
var frame callFrame
if decodeFrame(data, &frame) != nil || frame.State.Path != f.path || frame.State.Agent != f.agent {
frame := k.frame()
if decodeFrame(data, frame) != nil || frame.State.Path != f.path || frame.State.Agent != f.agent {
return nil
}
if st != nil && frame.State.Off < st.Off {
+38
View File
@@ -39,3 +39,41 @@ func TestLoadedCallsAreSizedOnce(t *testing.T) {
t.Fatal("appended calls not read back")
}
}
// Shards are read one after another into one kept frame, and gob leaves a
// field of the zero value out: a call read after one that had a value
// there has none, not the last shard's.
func TestLoadedCallsKeepNothingOfTheLastShard(t *testing.T) {
d := setupCalls(t)
write := func(name string, c Call, began time.Time) string {
path := filepath.Join(d.claude, "projects", "p", name+".jsonl")
c.Msg = "msg-" + name
st := &callFile{Path: path, Agent: "claude", Calls: []Call{c}, Began: map[string]time.Time{}}
if !began.IsZero() {
st.Began[c.Msg] = began
}
writeCalls(st, 0, false)
return path
}
full := write("full", Call{Time: time.Unix(100, 0), Agent: "claude", Session: "s1", Model: "claude-opus-5-5",
Tokens: Tokens{Input: 50, Output: 7, CacheRead: 9}, Compaction: true}, time.Unix(90, 0))
bareCall := Call{Time: time.Unix(200, 0), Agent: "claude"}
empty := write("empty", bareCall, time.Time{})
for range 3 {
if got := loadCalls(file{path: full, agent: "claude"}); got == nil || got.Calls[0].Input != 50 || !got.Calls[0].Compaction {
t.Fatal("the full shard not read back")
}
got := loadCalls(file{path: empty, agent: "claude"})
if got == nil || len(got.Calls) != 1 {
t.Fatal("the bare shard not read back")
}
want := bareCall
want.Msg, want.File = "msg-empty", empty
if c := got.Calls[0]; c.Input != 0 || c.Output != 0 || c.CacheRead != 0 || c.Compaction || c.Session != "" || c.Model != "" || c.Msg != want.Msg || !c.Time.Equal(want.Time) {
t.Fatalf("read after the full shard: %+v", c)
}
if b, ok := got.Began["msg-empty"]; ok {
t.Fatalf("began %v kept from the last shard", b)
}
}
}