Files
magpie/internal/usage/hash_rows_reuse_test.go
T
yetone fc9ce34d86 usage: hashRows reuses its scratch, within sizes its rows exactly
Startup on a copy of the owner's real accounts (mote, GUI, network
blocked, acgui.sh) allocated ~3.5GB, and two usage functions were near
the top of it:

- hashRows built a fresh 32KB byte buffer and shape map for every chunk
  it hashed: 510MB of the startup's allocations. It now takes them from
  a sync.Pool and clears the map before use. The digest is the same
  byte for byte (checked against the old code on the same chunks).
- rowChunk.within appended into a nil slice, growing it step by step
  (292MB flat). It now counts the rows in range first, returns nil when
  there are none, and makes the slice at that size.

Measured, same real-account copy, t9 (main) against t10 (this):
  startup CPU      13.81s -> 9.77s
  allocated (3m)   3483MB -> 2768MB (hashRows 510MB -> gone,
                   within 427MB -> 244MB cumulative)
  footprint        179MB  -> 175MB

Tests: TestHashRowsReusesItsScratch hashes a chunk again after a bigger
one and must get the same digest, and must allocate under 16KB a call
(min of 5 batches, so other goroutines' allocations don't count). Old
heat_disk.go: red at 32768 bytes a chunk; dropping clear(shapes): red.
TestWithinSizesItsRows needs len = cap = 600 (old code: cap 646).

Run on mote at this tree: go test ./internal/usage -count=1 ok, -race
ok, gofmt clean, go vet darwin/linux/windows ok, gui tests ok earlier.
An earlier usage run went red on a test whose name was lost (the log was
cut by tail); four full runs since are green, and the likely cause, the
first byte check counting other goroutines, is what the min-of-5 fixes.
2026-11-09 23:56:13 +08:00

83 lines
2.5 KiB
Go

package usage
import (
"crypto/sha256"
"fmt"
"math"
"runtime"
"testing"
"time"
)
func hashedRows(c *rowChunk) [sha256.Size]byte {
h := sha256.New()
hashRows(h, c)
var sum [sha256.Size]byte
copy(sum[:], h.Sum(nil))
return sum
}
// hashRows gave each chunk a new 32 KB buffer and shape map: 522 MB of a
// real history's start, a digest per session file's chunk. It reuses
// them now, and what it writes for a chunk is the same whatever it hashed
// before: a shape another chunk saw first is not taken as seen.
func TestHashRowsReusesItsScratch(t *testing.T) {
now := time.Unix(1_800_000_000, 0)
chunk := func(agent string, n int) *rowChunk {
c := &rowChunk{}
for i := range n {
r := Row{Record: Record{Time: now.Add(time.Duration(i) * time.Second), Agent: agent, Model: fmt.Sprint("m", i%3), Input: i}}
c.add(r, fmt.Sprint("msg", i), int64(i), false)
}
return c
}
a, b := chunk("claude", 40), chunk("codex", 2000)
first := hashedRows(a)
t.Logf("digest %x", first)
hashedRows(b)
if again := hashedRows(a); again != first {
t.Fatal("a chunk's rows hashed differently after another chunk's")
}
// by bytes: a pool may drop what it keeps (-race drops some on
// purpose). The process's count takes in whatever else runs, so the
// least of five batches is the one judged.
h := sha256.New()
per := uint64(math.MaxUint64)
for range 5 {
var m0, m1 runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m0)
const n = 20
for range n {
h.Reset()
hashRows(h, b)
}
runtime.ReadMemStats(&m1)
per = min(per, (m1.TotalAlloc-m0.TotalAlloc)/n)
}
if per >= 16<<10 {
t.Fatalf("hashRows allocated %d bytes a chunk", per)
}
}
// within made its rows by append, growing them up to twice what they
// needed: 245 MB of a real history's start. It counts them first now.
func TestWithinSizesItsRows(t *testing.T) {
now := time.Unix(1_800_000_000, 0)
c := &rowChunk{}
for i := range 1000 {
r := Row{Record: Record{Time: now.Add(time.Duration(i) * time.Minute), Agent: "claude", Model: "m", Input: i}}
c.add(r, fmt.Sprint("msg", i), int64(i), false)
}
out := c.within(now.Add(100*time.Minute), now.Add(700*time.Minute))
if out == nil || len(out.Rows) != 600 || cap(out.Rows) != 600 {
t.Fatalf("within kept %d rows in room for %d", len(out.Rows), cap(out.Rows))
}
if r := out.row(0); r.Input != 100 || out.row(599).Input != 699 {
t.Fatalf("within kept rows %d to %d", r.Input, out.row(599).Input)
}
if c.within(now.Add(-time.Hour), now) != nil {
t.Fatal("rows outside the span were kept")
}
}