sessions: a Claude session's calls list grows to the rate its file was read at
claudeCall appended each call to st.Calls, and append grows a long list by about a quarter at a time, so a session's calls were copied through lists holding ~5.2 times them on the way to its length: 616 MB of a 120s cold start's allocations on mote over real history copies, the largest single line in it. When the list is full and past 256 calls, growCalls makes room for the calls the rest of the file is likely to hold at the rate read so far (calls / bytes read, times the file's size, an eighth more), at most twice the calls so far, so a misjudged rate costs no more than a short list's doubling. A file read from its cache's offset counts from there the same way; a packed file, whose offsets run past its size, is left to append. TestClaudeCallsListGrownToTheFile reads 20k replies through claudeCallLine: the lists on the way hold 2.84 times the calls (bar 3.5), the last 1.14 times; by hand, growCalls returning at once, 5.2 times, red. A file whose second half is ten times as long per call ends at 1.44 times, under the bar of twice. Measured on mote, 120s cold, rc5 (the parent) against rc6 (this change) under load 67-112: heap peak 244 MB and 243 MB, the same. The list's growth over the Sessions read of the same lines (claudeLine) 616/1234 = 0.50 -> (535 + growCalls' 192)/1778 = 0.41: most of a real history's sessions stay under 256 calls, which append still grows. Run at the rebased head on mote: gofmt; go vet ./internal/sessions for darwin, linux and windows; go test -count=1 ./internal/sessions ok. Not run: the full suite (last at v0.1.3152's change, 85 packages ok).
This commit is contained in:
@@ -72,6 +72,9 @@ type callFile struct {
|
||||
Claude *claudeUsageState
|
||||
ContentHash string
|
||||
dirty map[int]bool
|
||||
// readSize is the size of the file being read (prepareCalls), for
|
||||
// growCalls
|
||||
readSize int64
|
||||
// bigFrame: read from a frame of more than framePatches (loadCalls)
|
||||
bigFrame bool
|
||||
Size int64
|
||||
@@ -409,9 +412,28 @@ func prepareCalls(f file, old *callFile, owned bool) *callFile {
|
||||
}
|
||||
st.dirty = map[int]bool{}
|
||||
st.Head, st.HeadSize, st.Path = hashHead(head), len(head), f.path
|
||||
st.readSize = f.size
|
||||
return st
|
||||
}
|
||||
|
||||
// growCalls makes room in st.Calls, when it is full, for the calls the
|
||||
// rest of the file holds at the rate read so far: append grows a long
|
||||
// list by a quarter at a time, and a Claude session's calls were copied
|
||||
// into ~5 lists their size on the way (616 MB of a cold start's reading
|
||||
// on mote, ~13% of all it allocated). The room is at most twice the
|
||||
// calls so far, as append's own doubling of a short list.
|
||||
func growCalls(st *callFile) {
|
||||
n := len(st.Calls)
|
||||
if n < cap(st.Calls) || n < 256 || st.End <= 0 || st.readSize <= st.End {
|
||||
return
|
||||
}
|
||||
// what the rest is likely to hold, and an eighth more
|
||||
want := min(int(int64(n)*st.readSize/st.End)*9/8, 2*n)
|
||||
if want > n+n/4+1 {
|
||||
st.Calls = slices.Grow(st.Calls, want-n)
|
||||
}
|
||||
}
|
||||
|
||||
func (st *callFile) clone() *callFile {
|
||||
c := st.cloneState()
|
||||
c.Calls = append([]Call(nil), st.Calls...)
|
||||
@@ -967,6 +989,7 @@ func claudeCall(st *callFile, b []byte, l *ccCall) {
|
||||
if c.Error == "" {
|
||||
c.Compact, st.CompactMs = st.CompactMs, 0
|
||||
}
|
||||
growCalls(st)
|
||||
st.Calls = append(st.Calls, c)
|
||||
}
|
||||
|
||||
|
||||
@@ -143,3 +143,49 @@ func TestClaudeMessageLookupAllocations(t *testing.T) {
|
||||
t.Fatalf("a message with only its request: %q", q.key)
|
||||
}
|
||||
}
|
||||
|
||||
// TestClaudeCallsListGrownToTheFile: a Claude session's calls list grows
|
||||
// to the rate the file was read at so far, not by append's quarters: the
|
||||
// lists made on the way to 20k calls hold ~2.8 times them, ~5.2 times by
|
||||
// append, and the last ends within an eighth or so of its length. A file
|
||||
// whose second half holds fewer calls ends at no more than twice them.
|
||||
func TestClaudeCallsListGrownToTheFile(t *testing.T) {
|
||||
at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC)
|
||||
read := func(n int, sparse func(i int) int) (made int, st *callFile) {
|
||||
// made: the calls the lists past 256 had room for
|
||||
var lines [][]byte
|
||||
size := int64(0)
|
||||
for i := range n {
|
||||
b := append(reuseLine(i, at), bytes.Repeat([]byte(" "), sparse(i))...)
|
||||
lines = append(lines, b)
|
||||
size += int64(len(b)) + 1
|
||||
}
|
||||
st = &callFile{Agent: "claude", readSize: size, Msgs: map[string]int{}, dirty: map[int]bool{}}
|
||||
end, last := int64(0), -1
|
||||
for _, b := range lines {
|
||||
st.At, end = end, end+int64(len(b))+1
|
||||
st.End = end
|
||||
claudeCallLine(st, b)
|
||||
if c := cap(st.Calls); c != last && len(st.Calls) > 256 {
|
||||
made += c
|
||||
}
|
||||
last = cap(st.Calls)
|
||||
}
|
||||
return made, st
|
||||
}
|
||||
n := 20000
|
||||
made, st := read(n, func(int) int { return 0 })
|
||||
if len(st.Calls) != n {
|
||||
t.Fatalf("%d calls of %d", len(st.Calls), n)
|
||||
}
|
||||
t.Logf("even: lists of %d calls past 256, cap %d for %d", made, cap(st.Calls), n)
|
||||
if made > 7*n/2 || cap(st.Calls) > n*5/4 {
|
||||
t.Fatalf("lists of %d calls were made, the last of %d, for %d calls", made, cap(st.Calls), n)
|
||||
}
|
||||
// the second half's lines ten times as long
|
||||
made, st = read(n, func(i int) int { return 9 * 600 * (i * 2 / n) })
|
||||
t.Logf("sparse end: lists of %d calls, cap %d for %d", made, cap(st.Calls), n)
|
||||
if len(st.Calls) != n || cap(st.Calls) > 2*n {
|
||||
t.Fatalf("%d calls in a list of %d", len(st.Calls), cap(st.Calls))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user