From efd1b697e401ca9ea771a02720291a0e53ac414b Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 21 Oct 2026 09:23:50 +0800 Subject: [PATCH] sessions: a cached file's calls are read into a slice made for them loadCalls appended each decoded patch to a nil slice, so every cached file's calls grew through append's steps. In a warm start's 30s alloc_space profile (sandbox HOME, ~380k calls) that append was 509 MB, the largest single site. Each frame now grows the slice once by its patch count (slices.Grow), which a frame of new calls fills exactly. TestLoadedCallsAreSizedOnce: 3000 cached calls came back in a slice of 3246 on the old code (red, run with the change stashed); 3006 now, the allocator's size class. It also reads back a second appended frame. go test -tags nogui ./internal/sessions/ ./internal/usage/: ok. At 8b48dc5d6 (this commit and the two beside it, on 5c557c793): gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ./... clean; go test -race -tags nogui ./internal/usage/ ./internal/sessions/ ok; go test -tags nogui -timeout 45m ./... with pipefail: 68 packages ok, no FAIL. Warm start in a sandbox HOME, magpie serve --web (ps RSS / footprint / heapSys at 30s): origin/main 650 / 213 / 649 MB, all three 606 / 208 / 598 MB. Allocated in the first 30s: 2557 MB before, 1878 MB after. --- internal/sessions/call_cache.go | 5 +++ internal/sessions/call_cache_load_test.go | 41 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 internal/sessions/call_cache_load_test.go 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") + } +}