Every row of a chunk had a 16-byte string header for its Codex
thread. A day's rows are a few hundred threads, one after another. At
the scale of #2278 (34k session files, 1M Codex rows), these headers
were the largest single holder in the heap, 24.5 MB of 184 MB live.
codexActors now keeps a []uint32 into the chunk's thread names. The
row before's thread is checked first, then the names. A clone shares
the names clipped, so adding a name to one never writes into the
other. The disk format is unchanged: each row's thread is still
written as a string.
Measured on mote, at 7e58591dc's parent with this change. The history
is synthetic: 25,000 Codex rollouts and 9,000 Claude Code sessions, 40
turns each (13 GB), plus a 115 MB usage.jsonl of 240k gateway calls on
the newest 3,000 of each. Cold `magpie web --no-open` in a sandbox
HOME, with `magpie heap` taken at 130 s:
- main: live heap 195.2 MB (both runs), physical footprint peak 389 to
417 MB.
- this change: live heap 175.6 MB, physical footprint peak 366 MB.
The reporter's 0.1.1318 peaked at 2.85 GB RSS on such a history.
Run at the pushed head (rebased onto 7e58591dc, rerun after):
- GOOS=linux, windows and darwin go vet -tags nogui ./internal/usage/...
./internal/sessions/... ./internal/gateway ./internal/cli: ok.
- go test ./internal/usage/... ./internal/sessions/...: ok.
TestCodexActorsTakeFourBytesARow: 100k rows of 200 threads hold at
most 5 bytes a row, read back, and a clone keeps its own rows. Broken
by hand (no lookup of a name already held) it goes red.
Not run before the push: the full suite. It runs next.
140 lines
4.2 KiB
Go
140 lines
4.2 KiB
Go
package usage
|
|
|
|
import (
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// pairMemo is what pairRows found over one list of chunks, named by their
|
|
// serials: which rows another one stands for, and the Codex actors and
|
|
// compactions it gave each chunk. A page turned, or asked for again
|
|
// before anything changed, unpacked every session file three times over to
|
|
// pair the same rows again: about half of a warm page (#3493).
|
|
type pairMemo struct {
|
|
since, until time.Time
|
|
// serials are the chunks' in pairRows' order, each set ended by a 0;
|
|
// a chunk without a serial (the probes') is named by its rows instead
|
|
serials []uint64
|
|
skip, matched []serialRef
|
|
actors map[uint64]codexActors
|
|
compactions map[uint64]map[int]*bool
|
|
}
|
|
|
|
type serialRef struct {
|
|
serial uint64
|
|
index int
|
|
}
|
|
|
|
// pairSerials names the chunks of a pairing, or is false when two have
|
|
// no serial and so can't be told apart.
|
|
func pairSerials(sets ...[]*rowChunk) ([]uint64, bool) {
|
|
var out []uint64
|
|
unnamed := false
|
|
for _, set := range sets {
|
|
for _, c := range set {
|
|
s := c.serial
|
|
if s == 0 {
|
|
if unnamed || len(c.Archive) != 0 {
|
|
return nil, false
|
|
}
|
|
unnamed = true
|
|
s = unnamedSerial(c)
|
|
}
|
|
out = append(out, s)
|
|
}
|
|
out = append(out, 0)
|
|
}
|
|
return out, true
|
|
}
|
|
|
|
// unnamedSerial stands for a chunk made again for each query, the probes':
|
|
// its rows are named by their place in the log and their time.
|
|
func unnamedSerial(c *rowChunk) uint64 {
|
|
h := mix(6, uint64(len(c.Rows)))
|
|
for i := range c.Rows {
|
|
h = mix(h, uint64(c.Rows[i].Order), uint64(c.Rows[i].Time.UnixNano()))
|
|
}
|
|
return h | 1
|
|
}
|
|
|
|
// pairRowsMemo is pairRows, through old when it paired these chunks over
|
|
// the same window. The memo it returns is for the next call (nil: none).
|
|
func pairRowsMemo(p Period, now time.Time, gateways, chunks, others []*rowChunk, identities map[string]sessions.CodexIdentity, old *pairMemo) (*pairedRows, *pairMemo) {
|
|
since, until := p.Since(now), p.Until(now)
|
|
serials, ok := pairSerials(gateways, chunks, others)
|
|
if !ok {
|
|
return pairRows(p, now, gateways, chunks, others, identities), nil
|
|
}
|
|
if old != nil && old.since.Equal(since) && old.until.Equal(until) && slices.Equal(old.serials, serials) {
|
|
return old.apply(serials, identities, gateways, chunks, others), old
|
|
}
|
|
ps := pairRows(p, now, gateways, chunks, others, identities)
|
|
return ps, memoOf(ps, serials, append(append(slices.Clone(gateways), chunks...), others...))
|
|
}
|
|
|
|
// memoOf is ps as a memo; originals are the chunks ps.all was cloned from.
|
|
func memoOf(ps *pairedRows, serials []uint64, originals []*rowChunk) *pairMemo {
|
|
m := &pairMemo{since: ps.since, until: ps.until, serials: serials}
|
|
named := make(map[*rowChunk]uint64, len(ps.all))
|
|
k := 0
|
|
for i, c := range ps.all {
|
|
for serials[k] == 0 {
|
|
k++
|
|
}
|
|
named[c] = serials[k]
|
|
k++
|
|
if !c.codexActors.equal(originals[i].codexActors) {
|
|
if m.actors == nil {
|
|
m.actors = map[uint64]codexActors{}
|
|
}
|
|
m.actors[serials[k-1]] = c.codexActors
|
|
}
|
|
if c.codexCompactions != nil {
|
|
if m.compactions == nil {
|
|
m.compactions = map[uint64]map[int]*bool{}
|
|
}
|
|
m.compactions[serials[k-1]] = c.codexCompactions
|
|
}
|
|
}
|
|
refs := func(set map[rowRef]bool) []serialRef {
|
|
out := make([]serialRef, 0, len(set))
|
|
for ref := range set {
|
|
out = append(out, serialRef{named[ref.Chunk], ref.Index})
|
|
}
|
|
return out
|
|
}
|
|
m.skip, m.matched = refs(ps.skip), refs(ps.matched)
|
|
return m
|
|
}
|
|
|
|
// apply is the pairing m remembers, on clones of these chunks.
|
|
func (m *pairMemo) apply(serials []uint64, identities map[string]sessions.CodexIdentity, sets ...[]*rowChunk) *pairedRows {
|
|
var all []*rowChunk
|
|
by := make(map[uint64]*rowChunk, len(serials))
|
|
k := 0
|
|
for _, set := range sets {
|
|
for _, c := range cloneCodexChunks(set) {
|
|
s := serials[k]
|
|
k++
|
|
if a, ok := m.actors[s]; ok {
|
|
c.codexActors = a
|
|
}
|
|
c.codexCompactions = m.compactions[s]
|
|
c.codexIdentities = identities
|
|
by[s] = c
|
|
all = append(all, c)
|
|
}
|
|
k++
|
|
}
|
|
refs := func(list []serialRef) map[rowRef]bool {
|
|
out := make(map[rowRef]bool, len(list))
|
|
for _, ref := range list {
|
|
out[rowRef{by[ref.serial], ref.index}] = true
|
|
}
|
|
return out
|
|
}
|
|
return &pairedRows{all: all, skip: refs(m.skip), matched: refs(m.matched), since: m.since, until: m.until}
|
|
}
|