Files
magpie/internal/usage/refset.go
T
yetone fddd7ff08f usage: pairing keeps its rows as bits and its groups by sums, and lets its pairs go (#2278)
A cold start pairs the whole history, and pairing held its row sets as
map[rowRef]bool, ~40 bytes a row, several sets of most rows at once.
They are now refSet: a bitset a chunk, a bit a row. matchScratch's
groupOf was keyed by the whole matchKey (a session string and its
tokens); it is keyed by a 128-bit maphash sum of them now, and the
sessions map is filled when a group is made. pairedRows' pairs and
compactions maps were filled and never read; they are gone.

Measured on mote, a copy of the real full history (52G Codex, 13G
Claude), GODEBUG=gctrace=1, 120s of /api polls, 3 rounds each,
alternating, v0.1.2143 (807048470) against this change:
  peak live heap  459 -> 439 MB (512/426/440 vs 470/433/413)
  heap at GC      543 -> 517 MB
  footprint peak  677 -> 641 MB
The inuse profile at the peak shows groupOf 28.7 -> 11.8 MB and the
matched set gone from matchedBlocks. This is a small part of the peak;
the rest is the full re-pair itself, which is next.

Run at this tree (rebased onto dca3e2d71 from 807048470), on mote:
go vet ./internal/usage; GOOS=linux|windows|darwin go vet -tags nogui
./... all ok; go test -count=1 ./internal/usage ok (30.1s), with the
new TestRefSet and TestMatchKeySum (no allocation).
Not run: the full suite, the GUI suite.
2026-10-30 13:42:11 +08:00

59 lines
1.2 KiB
Go

package usage
import (
"iter"
"math/bits"
)
// refSet is a set of rows, a bit a row of each chunk it holds. As a
// map[rowRef]bool a row was ~40 bytes, and a cold start pairing the
// whole history holds a set of most of its rows several times over; as
// bits a million rows are 125 KB (#2278).
type refSet map[*rowChunk][]uint64
func (s refSet) has(r rowRef) bool {
b := s[r.Chunk]
w := r.Index >> 6
return w < len(b) && b[w]&(1<<(r.Index&63)) != 0
}
func (s refSet) add(r rowRef) {
b := s[r.Chunk]
w := r.Index >> 6
if w >= len(b) {
grown := make([]uint64, max(w+1, 2*len(b), (r.Chunk.Count+63)>>6, (len(r.Chunk.Rows)+63)>>6))
copy(grown, b)
b = grown
s[r.Chunk] = b
}
b[w] |= 1 << (r.Index & 63)
}
// count is how many rows s holds.
func (s refSet) count() int {
n := 0
for _, b := range s {
for _, w := range b {
n += bits.OnesCount64(w)
}
}
return n
}
// all is each row s holds, in no order but each chunk's.
func (s refSet) all() iter.Seq[rowRef] {
return func(yield func(rowRef) bool) {
for c, b := range s {
for w, word := range b {
for word != 0 {
i := bits.TrailingZeros64(word)
word &= word - 1
if !yield(rowRef{c, w<<6 + i}) {
return
}
}
}
}
}
}