usage: a row's Codex thread is kept as a 4-byte place, not a string (#2278)

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.
This commit is contained in:
yetone
2026-10-28 20:43:10 +08:00
parent 7e58591dcf
commit 0dc4cec85d
10 changed files with 159 additions and 54 deletions
+91
View File
@@ -0,0 +1,91 @@
package usage
import (
"slices"
"unsafe"
)
// codexActors is the Codex thread of each row of a chunk, by row, "" for
// none. A row keeps its thread's place in names, not the string: the
// rows of a day are one thread's turns after another, so a []string held
// a 16-byte header a row for a few hundred names, 24.5 MB of the 184 MB
// live after 34k sessions' start (#2278). A nil ids is no row's.
type codexActors struct {
ids []uint32
// names are the threads, names[0] "" when any. A clone shares them
// clipped, so adding to one never writes into the other's.
names []string
}
func (a *codexActors) len() int { return len(a.ids) }
// at is row i's thread, "" past the rows it has
func (a *codexActors) at(i int) string {
if i < len(a.ids) {
return a.names[a.ids[i]]
}
return ""
}
func (a *codexActors) id(actor string) uint32 {
if len(a.names) == 0 {
a.names = []string{""}
}
if actor == "" {
return 0
}
if n := len(a.ids); n > 0 && a.names[a.ids[n-1]] == actor {
return a.ids[n-1] // the row before's thread, as most are
}
if i := slices.Index(a.names, actor); i >= 0 {
return uint32(i)
}
a.names = append(a.names, actor)
return uint32(len(a.names) - 1)
}
func (a *codexActors) add(actor string) { a.ids = append(a.ids, a.id(actor)) }
// set makes row i's thread actor, with n rows held
func (a *codexActors) set(n, i int, actor string) {
if len(a.ids) != n {
ids := make([]uint32, n)
copy(ids, a.ids)
a.ids = ids
}
a.ids[i] = a.id(actor)
}
func (a codexActors) clone() codexActors {
return codexActors{ids: slices.Clone(a.ids), names: slices.Clip(a.names)}
}
// some says some row has a thread
func (a *codexActors) some() bool {
for _, id := range a.ids {
if id != 0 {
return true
}
}
return false
}
func (a codexActors) equal(b codexActors) bool {
if len(a.ids) != len(b.ids) {
return false
}
for i := range a.ids {
if a.at(i) != b.at(i) {
return false
}
}
return true
}
func (a *codexActors) bytes() int64 {
n := int64(cap(a.ids))*4 + int64(cap(a.names))*int64(unsafe.Sizeof(""))
for _, name := range a.names {
n += int64(len(name))
}
return n
}
+40
View File
@@ -0,0 +1,40 @@
package usage
import (
"fmt"
"testing"
"time"
)
// A day's rows are one thread's turns after another: each keeps its
// thread in 4 bytes, not a string's 16, and reads it back (#2278).
func TestCodexActorsTakeFourBytesARow(t *testing.T) {
const rows, threads = 100_000, 200
c := &rowChunk{}
at := time.Date(2026, 10, 28, 0, 0, 0, 0, time.UTC)
actor := func(i int) string {
if i%7 == 0 {
return "" // a row of no Codex thread
}
return fmt.Sprintf("019a%028d", i*threads/rows)
}
for i := range rows {
c.add(Row{Record: Record{Time: at, Agent: "codex"}, codexActor: actor(i)}, "", int64(i), false)
}
for i := range rows {
if got := c.codexActors.at(i); got != actor(i) {
t.Fatalf("row %d: actor %q, want %q", i, got, actor(i))
}
}
if got, most := c.codexActors.bytes(), int64(rows*4*5/4+threads*64); got > most {
t.Fatalf("%d rows of %d threads hold %d bytes of actors, want at most %d", rows, threads, got, most)
}
clone := c.codexActors.clone()
setCodexActor(c, 3, "another")
if clone.at(3) != actor(3) || c.codexActors.at(3) != "another" || clone.equal(c.codexActors) {
t.Fatalf("a clone saw its original's change: %q, %q", clone.at(3), c.codexActors.at(3))
}
if len(clone.names) != threads+1 {
t.Fatalf("clone names = %d, want %d", len(clone.names), threads+1)
}
}
+3 -19
View File
@@ -6,7 +6,6 @@ import (
"strings"
"sync"
"time"
"unsafe"
"github.com/yetone/magpie/internal/sessions"
)
@@ -172,7 +171,7 @@ func cloneCodexChunks(chunks []*rowChunk) []*rowChunk {
out := make([]*rowChunk, len(chunks))
for i, chunk := range chunks {
copy := *chunk
copy.codexActors = slices.Clone(chunk.codexActors)
copy.codexActors = chunk.codexActors.clone()
copy.codexCompactions = nil
copy.codexIdentities = nil
out[i] = &copy
@@ -426,9 +425,7 @@ func foldCodexCall(refs []rowRef, read map[rowRef]Row, skip map[rowRef]bool) {
actorConflict := false
for i, ref := range refs {
rows[i] = read[ref]
if ref.Index < len(ref.Chunk.codexActors) {
actors[i] = ref.Chunk.codexActors[ref.Index]
}
actors[i] = ref.Chunk.codexActors.at(ref.Index)
if i == 0 {
actor = actors[i]
} else {
@@ -460,20 +457,7 @@ func foldCodexCall(refs []rowRef, read map[rowRef]Row, skip map[rowRef]bool) {
}
func setCodexActor(chunk *rowChunk, index int, actor string) {
if n := rowsIn(chunk); len(chunk.codexActors) != n {
actors := make([]string, n)
copy(actors, chunk.codexActors)
chunk.codexActors = actors
}
chunk.codexActors[index] = actor
}
func codexActorBytes(actors []string) int64 {
bytes := int64(cap(actors)) * int64(unsafe.Sizeof(""))
for _, actor := range actors {
bytes += int64(len(actor))
}
return bytes
chunk.codexActors.set(rowsIn(chunk), index, actor)
}
// codexEffort keeps CodexModelEffort's last answer for each period: the
+1 -1
View File
@@ -448,7 +448,7 @@ func TestCodexActorSurvivesPackedRequestChunk(t *testing.T) {
if row.SessionRole != "subagent" || row.Session != "main" || row.Subagent != "child" {
t.Fatalf("packed Codex actor identity was lost: %+v", row)
}
if len(chunk.codexActors) != len(chunk.Rows) || chunk.codexActors[0] != "child" {
if chunk.codexActors.len() != len(chunk.Rows) || chunk.codexActors.at(0) != "child" {
t.Fatal("query identity changed the source chunk")
}
}
+2 -2
View File
@@ -73,8 +73,8 @@ func directRows(p Period, now time.Time) []Row {
matched[ref.Index] = true
}
for i, call := range logs {
if i < len(local.codexActors) && local.codexActors[i] != call.ThreadID {
actors[i] = local.codexActors[i]
if i < local.codexActors.len() && local.codexActors.at(i) != call.ThreadID {
actors[i] = local.codexActors.at(i)
}
}
}
+2 -2
View File
@@ -287,8 +287,8 @@ func (ps *pairedRows) sigs(identities map[string]sessions.CodexIdentity) map[*ro
if c.codexIdentities != nil {
h += mix(3)
}
for i, a := range c.codexActors {
if a != "" {
for i := range c.codexActors.len() {
if a := c.codexActors.at(i); a != "" {
h += mix(4, uint64(i), actor(a))
}
}
+5 -5
View File
@@ -132,9 +132,9 @@ func diskEntry(c *rowChunk) []byte {
b = appendDiskString(b, name)
put(uint64(int64(offset)))
}
put(uint64(len(c.codexActors)))
for _, actor := range c.codexActors {
b = appendDiskString(b, actor)
put(uint64(c.codexActors.len()))
for i := range c.codexActors.len() {
b = appendDiskString(b, c.codexActors.at(i))
}
put(uint64(len(archive)))
if !readMapped(c.mapped, func() { b = append(b, archive...) }) {
@@ -213,14 +213,14 @@ func readDiskEntry(b []byte) (*rowChunk, error) {
if !good {
return nil, errDiskEntry
}
c.codexActors = append(c.codexActors, actor)
c.codexActors.add(actor)
}
n = take()
if b == nil || n != uint64(len(b)) || n == 0 {
return nil, errDiskEntry
}
c.Archive = b
c.Bytes = int64(len(b)+len(c.Locations)*8) + codexActorBytes(c.codexActors)
c.Bytes = int64(len(b)+len(c.Locations)*8) + c.codexActors.bytes()
return c, nil
}
+10 -20
View File
@@ -115,7 +115,7 @@ type rowChunk struct {
// row of the period at once. 364k calls had 7300 shapes.
Shapes [][rowTexts]uint32
shapeIDs map[[rowTexts]uint32]uint32
codexActors []string
codexActors codexActors
codexCompactions map[int]*bool
codexIdentities map[string]sessions.CodexIdentity
dict map[string]uint32
@@ -275,21 +275,19 @@ func (c *rowChunk) add(r Row, msg string, order int64, failed bool) {
}
}
c.Rows = append(c.Rows, p)
if c.codexActors != nil || r.codexActor != "" {
if c.codexActors == nil {
c.codexActors = make([]string, len(c.Rows)-1)
c.Bytes += int64(len(c.codexActors)) * int64(unsafe.Sizeof(""))
if c.codexActors.ids != nil || r.codexActor != "" {
before := c.codexActors.bytes()
if c.codexActors.ids == nil {
c.codexActors.ids = make([]uint32, len(c.Rows)-1)
}
c.codexActors = append(c.codexActors, r.codexActor)
c.codexActors.add(r.codexActor)
c.Bytes += c.codexActors.bytes() - before
}
c.Count = len(c.Rows)
if r.Time.After(c.Latest) {
c.Latest = r.Time
}
c.Bytes += int64(unsafe.Sizeof(packedRow{}))
if c.codexActors != nil {
c.Bytes += int64(unsafe.Sizeof("")) + int64(len(r.codexActor))
}
}
func (c *rowChunk) row(i int) Row {
p := &c.Rows[i]
@@ -331,11 +329,7 @@ func (c *rowChunk) row(i int) Row {
}
r.Computer = c.Computer
if c.codexIdentities != nil {
actor := ""
if i < len(c.codexActors) {
actor = c.codexActors[i]
}
r = codexIdentityRow(r, actor, c.codexIdentities)
r = codexIdentityRow(r, c.codexActors.at(i), c.codexIdentities)
}
return r
}
@@ -1554,11 +1548,7 @@ func pairRows(p Period, now time.Time, gateways, chunks, others []*rowChunk, ide
since, until := p.Since(now), p.Until(now)
matched := matchedBlocks(gateways, chunks, skip, pairs, since, true)
for gateway, local := range pairs {
actor := ""
if local.Index < len(local.Chunk.codexActors) {
actor = local.Chunk.codexActors[local.Index]
}
setCodexActor(gateway.Chunk, gateway.Index, actor)
setCodexActor(gateway.Chunk, gateway.Index, local.Chunk.codexActors.at(local.Index))
gateway.Chunk.setCodexCompaction(gateway.Index, compactions[local])
}
all := append(append(slices.Clone(gateways), chunks...), others...)
@@ -1588,7 +1578,7 @@ func (ps *pairedRows) visitActors(fn func(*Row)) {
defer rows.release()
var r Row
for _, c := range ps.all {
if slices.ContainsFunc(c.codexActors, func(a string) bool { return a != "" }) {
if c.codexActors.some() {
if visitingActors != nil {
visitingActors(c)
}
+3 -3
View File
@@ -18,7 +18,7 @@ type pairMemo struct {
// a chunk without a serial (the probes') is named by its rows instead
serials []uint64
skip, matched []serialRef
actors map[uint64][]string
actors map[uint64]codexActors
compactions map[uint64]map[int]*bool
}
@@ -85,9 +85,9 @@ func memoOf(ps *pairedRows, serials []uint64, originals []*rowChunk) *pairMemo {
}
named[c] = serials[k]
k++
if !slices.Equal(c.codexActors, originals[i].codexActors) {
if !c.codexActors.equal(originals[i].codexActors) {
if m.actors == nil {
m.actors = map[uint64][]string{}
m.actors = map[uint64]codexActors{}
}
m.actors[serials[k-1]] = c.codexActors
}
+2 -2
View File
@@ -44,7 +44,7 @@ func (c *rowChunk) pack() *rowChunk {
next.Count = len(c.Rows) // what rowsIn says of it
next.Rows, next.Strings, next.Shapes, next.dict, next.shapeIDs = nil, nil, nil, nil, nil
next.Archive, next.Locations = encoded, locations
next.Bytes = int64(len(next.Archive)+len(locations)*8) + codexActorBytes(next.codexActors)
next.Bytes = int64(len(next.Archive)+len(locations)*8) + next.codexActors.bytes()
return &next
}
@@ -202,7 +202,7 @@ func (c *rowChunk) unpackReusing(dst *rowChunk, into []packedRow, strs []string,
} else {
next.Strings = make([]string, int(count))
}
next.Bytes = int64(len(next.Rows))*int64(unsafe.Sizeof(packedRow{})) + codexActorBytes(next.codexActors)
next.Bytes = int64(len(next.Rows))*int64(unsafe.Sizeof(packedRow{})) + next.codexActors.bytes()
// The strings are copied out in one piece: one allocation a block, not
// one a string. The section is walked once to find its end, and its
// copy again to cut the strings from it.