usage: the Requests page's seen message ids are hashes, not copies
visibleLocal leaves out a Claude message written again in another file
(a resumed or forked session's copy). It kept every id it had seen as a
strings.Clone of the decoded chunk's string, so a long history's ~400k
ids were copied on each page build: ~7MB held at the start's peak and
27MB more allocated per run. It now keeps two maphash values of each id
(seeds made per process); two ids alike in both are ~1e-28 likely.
Measured with the real ~/.codex and ~/.claude histories (sandbox HOME,
fp3 harness), base bbb4fd478 vs this change, two alternating runs:
visibleLocal allocated 81MB -> 55MB per run (the clones gone)
footprint at the end 170 / 174MB -> 166 / 170MB
footprint peak 275 / 278MB -> 252 / 277MB (noisy)
No test guarded the dedupe: breaking it by hand left ./internal/usage
green. TestVisibleLocalLeavesOutRepeatedMessages is new; with the
seen check turned off by hand it went red ("left out map[]").
Run at this head: gofmt clean; GOOS=linux and GOOS=windows go vet
-tags nogui ./internal/usage; go test -timeout 45m -count=1
./internal/usage ./internal/gui ok.
Not run: the full ./... suite (only internal/usage changed).
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"hash/maphash"
|
||||
"io"
|
||||
"maps"
|
||||
"os"
|
||||
@@ -1175,8 +1176,12 @@ func (idx *requestIndex) trim(bound int64) {
|
||||
// references for matches, never a full hydrated row for every call.
|
||||
func visibleLocal(chunks []*rowChunk) map[rowRef]bool {
|
||||
duplicates := map[rowRef]bool{}
|
||||
seen := map[string]bool{}
|
||||
rows := lentRows() // a kept message id is cloned
|
||||
// a message id seen is kept as two 64-bit hashes of it, not a copy of
|
||||
// the decoded chunk's string: a long history's ~400k ids held ~7MB of
|
||||
// copies at the start's peak, and a key of these is a string header's
|
||||
// size. Two ids alike in both are ~1e-28 likely.
|
||||
seen := map[[2]uint64]struct{}{}
|
||||
rows := lentRows()
|
||||
defer rows.release()
|
||||
for _, c := range chunks {
|
||||
open := rows.of(c)
|
||||
@@ -1186,17 +1191,19 @@ func visibleLocal(chunks []*rowChunk) map[rowRef]bool {
|
||||
if msg == "" {
|
||||
continue
|
||||
}
|
||||
if seen[msg] {
|
||||
k := [2]uint64{maphash.String(msgSeeds[0], msg), maphash.String(msgSeeds[1], msg)}
|
||||
if _, ok := seen[k]; ok {
|
||||
duplicates[rowRef{c, i}] = true
|
||||
} else {
|
||||
// a copy: a decoded chunk's strings are one allocation
|
||||
seen[strings.Clone(msg)] = true
|
||||
seen[k] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
return duplicates
|
||||
}
|
||||
|
||||
var msgSeeds = [2]maphash.Seed{maphash.MakeSeed(), maphash.MakeSeed()}
|
||||
|
||||
// matchKey narrows candidates before comparing their end times. Request IDs
|
||||
// are consumed first; fallback matching still requires uniqueness both ways.
|
||||
// The agent isn't in it: a session's id names the one conversation, and a
|
||||
|
||||
@@ -311,3 +311,21 @@ func TestRequestPageRanksModelAtEachProvider(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A Claude message written again in another file (a resumed or forked
|
||||
// session's copy) is counted once, at its first file, and only it: a
|
||||
// message id seen once, or none, is never left out.
|
||||
func TestVisibleLocalLeavesOutRepeatedMessages(t *testing.T) {
|
||||
a, b := &rowChunk{}, &rowChunk{}
|
||||
a.add(Row{}, "msg_1", 0, false)
|
||||
a.add(Row{}, "msg_2", 1, false)
|
||||
b.add(Row{}, "msg_1", 0, false)
|
||||
b.add(Row{}, "", 1, false)
|
||||
b.add(Row{}, "msg_3", 2, false)
|
||||
b.add(Row{}, "msg_2", 3, false)
|
||||
a, b = a.pack(), b.pack()
|
||||
got := visibleLocal([]*rowChunk{a, b})
|
||||
if len(got) != 2 || !got[rowRef{b, 0}] || !got[rowRef{b, 3}] {
|
||||
t.Fatalf("left out %v, want b's rows 0 and 3 only", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user