diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go index 7b12a8dec..96105f869 100644 --- a/internal/sessions/call_cache.go +++ b/internal/sessions/call_cache.go @@ -14,6 +14,7 @@ import ( "io" "os" "path/filepath" + "slices" "strings" "sync" "time" @@ -518,6 +519,10 @@ func loadCalls(f file) *callFile { if st != nil && frame.State.Off < st.Off { return nil } + // a frame's patches are mostly new calls: room for them at once, + // rather than append's doublings, which a warm start paid ~500 MB + // for across every cached file + calls = slices.Grow(calls, len(frame.Patches)) for _, p := range frame.Patches { if p.Index < 0 || p.Index > len(calls) { return nil diff --git a/internal/sessions/call_cache_load_test.go b/internal/sessions/call_cache_load_test.go new file mode 100644 index 000000000..a6c9336b1 --- /dev/null +++ b/internal/sessions/call_cache_load_test.go @@ -0,0 +1,41 @@ +package sessions + +import ( + "fmt" + "path/filepath" + "testing" + "time" +) + +// A cached file's calls are read into a slice made for them, not grown +// call by call: append's doublings cost a warm start ~500 MB of garbage +// across every cached file, all of it in its first seconds. +func TestLoadedCallsAreSizedOnce(t *testing.T) { + d := setupCalls(t) + path := filepath.Join(d.claude, "projects", "p", "s.jsonl") + st := &callFile{Path: path, Agent: "claude", Began: map[string]time.Time{}} + add := func(n int) { + for range n { + i := len(st.Calls) + st.Calls = append(st.Calls, Call{Time: time.Unix(int64(i), 0), Agent: "claude", Session: "s", Model: "claude-opus-5-5", Msg: fmt.Sprint("msg", i)}) + } + } + add(3000) + writeCalls(st, 0, false) + got := loadCalls(file{path: path, agent: "claude"}) + if got == nil || len(got.Calls) != 3000 || got.Calls[2999].Msg != "msg2999" || got.Calls[2999].File != path || got.Msgs["msg2999"] != 2999 { + t.Fatal("cached calls not read back") + } + // made once, to the allocator's size class; grown by append it ends + // a growth step past + if cap(got.Calls)-len(got.Calls) > len(got.Calls)/100 { + t.Fatalf("3000 calls read into a slice of %d", cap(got.Calls)) + } + // a later frame appended to the file is read after the first, in order + add(500) + writeCalls(st, 3000, true) + got = loadCalls(file{path: path, agent: "claude"}) + if got == nil || len(got.Calls) != 3500 || got.Calls[3499].Msg != "msg3499" || got.Calls[2999].Msg != "msg2999" { + t.Fatal("appended calls not read back") + } +}