usage: a chunk's Claude message ids are read once per serial, not on every pairing (#2278)
visibleLocal leaves out a Claude message written again in another file. To do it, every pairing decoded every local chunk of the period and hashed each row's message id. While an agent worked (a call appended every 3s, the GUI's endpoints polled every 5s), that was 1.1 GB of the 4.5 GB allocated in 90s; 840 MB of it was decoding. Each chunk's message keys (the two hashes and the row) are now kept by the chunk's serial, as matchedBlocks' hints are, with the same pruning (hintsKept turns). Only a chunk with no serial, or none kept, is decoded. The keys are read in the order the chunks are passed, so what is left out is the same as before. Run at this head, on mote: - go vet ./internal/usage/; GOOS=linux, windows, darwin go vet -tags nogui ./internal/usage/ ./internal/usageapi/: ok - go test -count=1 ./internal/usage/ ./internal/usageapi/: ok - TestVisibleLocalKeptBySerial: three chunks, two with serials. The calls decode 3, then 1, then 1 chunk, with the same rows left out each time, and in reverse order too. - Broken by hand: ignoring the kept keys made it red (call 1 decoded 3 chunks); shifting each kept row index made it and TestVisibleLocalLeavesOutRepeatedMessages red. - Writer bench (hmw.sh: the real 109 MB usage.jsonl history, calls appended every 3s, 10 endpoints polled), at the second start, heap profile at 90s, against v0.1.2188's code: - visibleLocal: 1111 MB -> 259 MB (msgKeysOf 53 MB); - pairRowsIn: 1394 MB -> 331 MB; - total allocated: 4537 MB -> 3908 MB; - fp_peak: 544 MB -> 315 MB. mote was at load ~26 from other work, so the times are noisy. Not changed: the seen map is still built on every pairing (206 MB of the 259 MB left).
This commit is contained in:
@@ -1394,34 +1394,103 @@ func visibleLocal(chunks []*rowChunk) refSet {
|
||||
// made to size, as many as the rows that can have an id (a Codex call
|
||||
// has none): grown an id at a time, it was ~250 MB of a minute and a
|
||||
// third of polls (#2278)
|
||||
keys := msgKeysOf(chunks)
|
||||
n := 0
|
||||
for _, c := range chunks {
|
||||
if c.Source.Agent != "codex" {
|
||||
n += c.Count
|
||||
}
|
||||
for _, k := range keys {
|
||||
n += len(k.keys)
|
||||
}
|
||||
seen := make(map[[2]uint64]struct{}, n)
|
||||
rows := lentRows()
|
||||
defer rows.release()
|
||||
for _, c := range chunks {
|
||||
open := rows.of(c)
|
||||
for i := range open.Rows {
|
||||
p := &open.Rows[i]
|
||||
msg := open.Strings[open.text(p, rowMsg)]
|
||||
if msg == "" {
|
||||
continue
|
||||
}
|
||||
k := [2]uint64{maphash.String(msgSeeds[0], msg), maphash.String(msgSeeds[1], msg)}
|
||||
if _, ok := seen[k]; ok {
|
||||
duplicates.add(rowRef{c, i})
|
||||
for i, c := range chunks {
|
||||
for _, k := range keys[i].keys {
|
||||
if _, ok := seen[k.k]; ok {
|
||||
duplicates.add(rowRef{c, int(k.at)})
|
||||
} else {
|
||||
seen[k] = struct{}{}
|
||||
seen[k.k] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
return duplicates
|
||||
}
|
||||
|
||||
// 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
|
||||
// 1.1 GB visibleLocal allocated in 90s while an agent worked (#2278).
|
||||
var msgKeys struct {
|
||||
sync.Mutex
|
||||
by map[uint64]*chunkMsgKeys
|
||||
turn uint64
|
||||
}
|
||||
|
||||
type chunkMsgKeys struct {
|
||||
keys []msgKey
|
||||
turn uint64
|
||||
}
|
||||
|
||||
type msgKey struct {
|
||||
k [2]uint64
|
||||
at int32
|
||||
}
|
||||
|
||||
// msgKeysOpening, in tests, is told each chunk msgKeysOf decodes.
|
||||
var msgKeysOpening func(*rowChunk)
|
||||
|
||||
// msgKeysOf are chunks' message keys, read from a chunk only when none
|
||||
// are kept for its serial.
|
||||
func msgKeysOf(chunks []*rowChunk) []*chunkMsgKeys {
|
||||
msgKeys.Lock()
|
||||
msgKeys.turn++
|
||||
turn := msgKeys.turn
|
||||
out := make([]*chunkMsgKeys, len(chunks))
|
||||
for i, c := range chunks {
|
||||
if k := msgKeys.by[c.serial]; c.serial != 0 && k != nil {
|
||||
k.turn = turn
|
||||
out[i] = k
|
||||
}
|
||||
}
|
||||
msgKeys.Unlock()
|
||||
rows := lentRows()
|
||||
defer rows.release()
|
||||
made := map[uint64]*chunkMsgKeys{}
|
||||
for i, c := range chunks {
|
||||
if out[i] != nil {
|
||||
continue
|
||||
}
|
||||
if msgKeysOpening != nil {
|
||||
msgKeysOpening(c)
|
||||
}
|
||||
k := &chunkMsgKeys{turn: turn}
|
||||
open := rows.of(c)
|
||||
for j := range open.Rows {
|
||||
p := &open.Rows[j]
|
||||
msg := open.Strings[open.text(p, rowMsg)]
|
||||
if msg == "" {
|
||||
continue
|
||||
}
|
||||
k.keys = append(k.keys, msgKey{[2]uint64{maphash.String(msgSeeds[0], msg), maphash.String(msgSeeds[1], msg)}, int32(j)})
|
||||
}
|
||||
k.keys = slices.Clip(k.keys)
|
||||
out[i] = k
|
||||
if c.serial != 0 {
|
||||
made[c.serial] = k
|
||||
}
|
||||
}
|
||||
msgKeys.Lock()
|
||||
if msgKeys.by == nil {
|
||||
msgKeys.by = map[uint64]*chunkMsgKeys{}
|
||||
}
|
||||
for serial, k := range made {
|
||||
msgKeys.by[serial] = k
|
||||
}
|
||||
for serial, k := range msgKeys.by {
|
||||
if k.turn+hintsKept < msgKeys.turn {
|
||||
delete(msgKeys.by, serial)
|
||||
}
|
||||
}
|
||||
msgKeys.Unlock()
|
||||
return out
|
||||
}
|
||||
|
||||
var msgSeeds = [2]maphash.Seed{maphash.MakeSeed(), maphash.MakeSeed()}
|
||||
|
||||
// idCalls are the gateway's calls of one request id, in the gateway's
|
||||
|
||||
@@ -3,6 +3,7 @@ package usage
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -330,6 +331,41 @@ func TestVisibleLocalLeavesOutRepeatedMessages(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A chunk's message ids are read once while its serial stands: a pairing
|
||||
// decoded every session file of the period for them on every poll
|
||||
// (#2278), and what is left out is the same either way.
|
||||
func TestVisibleLocalKeptBySerial(t *testing.T) {
|
||||
a, b, c := &rowChunk{}, &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)
|
||||
c.add(Row{}, "msg_3", 0, false)
|
||||
c.add(Row{}, "msg_1", 1, false)
|
||||
a, b, c = a.pack(), b.pack(), c.pack()
|
||||
a.serial, b.serial = nextChunkSerial(), nextChunkSerial()
|
||||
opened := 0
|
||||
msgKeysOpening = func(*rowChunk) { opened++ }
|
||||
t.Cleanup(func() { msgKeysOpening = nil })
|
||||
want := map[rowRef]bool{{b, 0}: true, {c, 0}: true, {c, 1}: true}
|
||||
for i, open := range []int{3, 1, 1} {
|
||||
opened = 0
|
||||
got := refMap(visibleLocal([]*rowChunk{a, b, c}))
|
||||
if !maps.Equal(got, want) {
|
||||
t.Fatalf("call %d left out %v, want %v", i, got, want)
|
||||
}
|
||||
if opened != open {
|
||||
t.Fatalf("call %d decoded %d chunks, want %d (c has no serial)", i, opened, open)
|
||||
}
|
||||
}
|
||||
// in another order, the kept keys are read in that order
|
||||
opened = 0
|
||||
got := refMap(visibleLocal([]*rowChunk{c, b, a}))
|
||||
if want := map[rowRef]bool{{b, 1}: true, {a, 0}: true, {a, 1}: true}; !maps.Equal(got, want) || opened != 1 {
|
||||
t.Fatalf("reversed: left out %v (decoded %d), want %v", got, opened, want)
|
||||
}
|
||||
}
|
||||
|
||||
// A read keeps one chunk a source, the map's and the page's both: a copy
|
||||
// for each was 13.5 MB after 34k sessions' start (#2278). A chunk an
|
||||
// earlier read published is taken as it is, its use counted on it: a copy
|
||||
|
||||
Reference in New Issue
Block a user