usage: visibleLocal keeps its set of ids seen for the next pairing, not made again each time (#2278)

visibleLocal made a map of every Claude message id (~400k on the bench
HOME) to size for every pairing: 212 MB of the ~2 GB 90 s of polls
allocated, the largest single site left after the decode buffers were
kept (70d9996c5).

The set is now kept empty between calls as the matcher's scratch is
(keptSeen, an atomic pointer): a call takes it, a cleared map keeps its
buckets, and it goes with the scratch on ReleaseScratch or scratchLinger
after the last pairing. A call while another has it makes its own.

Run on mote at this tree, rebased on origin/main:
- go test -count=1 ./internal/usage/ ./internal/usageapi/: ok, at df09470d0 on 99ad38b7c
- GOOS=linux|windows|darwin go vet -tags nogui ./internal/usage/
  ./internal/usageapi/ ./internal/gui/: ok
- TestVisibleLocalSeenKeptEmpty (new): the set is kept empty, a call on
  other chunks after leaves out what a first call does, ReleaseScratch
  drops it. Broken by hand, red each time: the set not cleared before
  it is kept; ReleaseScratch not dropping it.

Bench (hmw.sh, 90 s), second runs: visibleLocal 266 MB -> 71 MB
(msgKeysOf and the duplicates left), fp_peak 347 -> 348 MB. The run's
total (2030 -> 2702 MB) moved with the sessions list, which this run
built again (sessionRoutes +782 MB, QueryPage -277 MB); that list is
the next thing to look at.
This commit is contained in:
yetone
2026-10-30 18:36:13 +08:00
parent 99ad38b7c8
commit 3bce283df4
2 changed files with 54 additions and 3 deletions
+22 -3
View File
@@ -1432,19 +1432,36 @@ func visibleLocal(chunks []*rowChunk) refSet {
for _, k := range keys {
n += len(k.keys)
}
seen := make(map[[2]uint64]struct{}, n)
seen := keptSeen.Swap(nil)
if seen == nil {
m := make(map[[2]uint64]struct{}, n)
seen = &m
}
defer keepSeen(seen)
for i, c := range chunks {
for _, k := range keys[i].keys {
if _, ok := seen[k.k]; ok {
if _, ok := (*seen)[k.k]; ok {
duplicates.add(rowRef{c, int(k.at)})
} else {
seen[k.k] = struct{}{}
(*seen)[k.k] = struct{}{}
}
}
}
return duplicates
}
// keptSeen is visibleLocal's set of ids seen, kept empty for the next
// call as matchScratch is: made to size for every pairing it was ~210 MB
// of the ~2 GB 90 s of polls allocated (#2278). A cleared map keeps its
// buckets.
var keptSeen atomic.Pointer[map[[2]uint64]struct{}]
func keepSeen(seen *map[[2]uint64]struct{}) {
clear(*seen)
keptSeen.Store(seen)
lingerScratch()
}
// msgKeys are a chunk's message ids as visibleLocal hashes them, each
// with its row, kept by the chunk's serial as matchHints are: a pairing
// decoded every session file of the period to read them, ~840 MB of the
@@ -1667,6 +1684,7 @@ func mapFor[K comparable, V any](m map[K]V, n int) map[K]V {
func ReleaseScratch() {
keptMatch.Store(nil)
keptPicks.Store(nil)
keptSeen.Store(nil)
releaseRows()
releasePriced()
}
@@ -1693,6 +1711,7 @@ func lingerScratch() {
scratchSweep.t = time.AfterFunc(scratchLinger, func() {
keptMatch.Store(nil)
keptPicks.Store(nil)
keptSeen.Store(nil)
releaseRows()
})
return
+32
View File
@@ -366,6 +366,38 @@ func TestVisibleLocalKeptBySerial(t *testing.T) {
}
}
// The ids one call saw are kept as an empty set for the next (#2278): a
// call after another, on other chunks, leaves out what a first call does,
// and ReleaseScratch lets the set go.
func TestVisibleLocalSeenKeptEmpty(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_2", 0, false)
b.add(Row{}, "msg_3", 1, false)
a, b = a.pack(), b.pack()
t.Cleanup(ReleaseScratch)
ReleaseScratch()
if got := refMap(visibleLocal([]*rowChunk{a, b})); len(got) != 1 || !got[rowRef{b, 0}] {
t.Fatalf("left out %v, want b's row 0", got)
}
kept := keptSeen.Load()
if kept == nil || len(*kept) != 0 {
t.Fatal("the ids seen weren't kept empty")
}
// b alone repeats nothing; the last call's msg_2 doesn't stand
if got := refMap(visibleLocal([]*rowChunk{b})); len(got) != 0 {
t.Fatalf("after another call left out %v", got)
}
if keptSeen.Load() != kept {
t.Fatal("a call right after made a set of its own")
}
ReleaseScratch()
if keptSeen.Load() != nil {
t.Fatal("ReleaseScratch kept the ids seen")
}
}
// A page read again after a call takes each chunk's newest rows from its
// part, refs and rows: decoding the newest chunks for every page was
// ~390 MB of 90s while an agent worked, a long session's file among them,