Files
magpie/internal/sessions/call_cache_load_test.go
T
yetone efd1b697e4 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.
2026-10-21 09:41:29 +08:00

42 lines
1.5 KiB
Go

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")
}
}