usage: a row keeps its times in ms in 32 bits, 184 bytes where it was 216 (#2278)

A page holds every row of its period at once, and at a cold start over
the full history the rows held were most of what the footprint's peak
held: on mote's copy of the real history (52G Codex, 13G Claude Code),
the inuse heap 20s into a polled start had 48.5 MB of decoded session
rows (unpackReusing) and 42.9 MB of priced gateway rows (pricedRows),
both packedRow.

Millis, TTFT, FirstText, FirstReasoning, Sent, Flow, Own, Retried and
Queued are ms (Record's *_ms) and go in an int32, as Masking already
did. One past 24 days, which no call takes, is kept as the longest an
int32 holds (msOf), not wrapped. The snapshot's encoding is unchanged:
each is written as the int64 it was and read back through msOf.
unsafe.Sizeof(packedRow{}) goes from 216 to 184, 15% of every row held:
~14 MB of the 91 MB above.

Run at this head (rebased on 46aa7e6b8, which touches providers and
the GUI only), on mote:
- go build ./... ok
- go test -count=1 ./internal/usage ./internal/usageapi ok (and before
  the rebase, on eadeb89c4)
- GOOS=linux, windows, darwin go vet -tags nogui ./internal/usage ok;
  gofmt -l clean
- TestRowsOfOneSessionShareAShape now wants 184 bytes at most.

Broken by hand: msOf as a plain int32(ms) turns the new
TestRowTimesPastWhatARowKeeps red ("read back Millis -1702967296").

Not measured: a whole-app footprint A/B. Two rounds of peakheap.sh ran
at mote load ~220, with most polls timing out (3 to 12 of 80 answered)
and each run higher than the last whatever the binary (202, 248, 270,
318 MB), so they say nothing either way.
This commit is contained in:
yetone
2026-10-30 12:41:24 +08:00
parent 46aa7e6b89
commit 807048470e
3 changed files with 40 additions and 20 deletions
+20 -13
View File
@@ -70,19 +70,26 @@ type packedRow struct {
Time time.Time
// Varied are the strings of rowVaried, and Shape the chunk's Shapes
// entry that holds the row's other strings
Varied [len(rowVaried)]uint32
Shape uint32
Tokens [6]int64
Millis, TTFT, FirstText, Order int64
Sent, Flow, FirstReasoning int64
Own, Retried, Queued int64
RouteID int64
Cost, Credits float64
Status, Tries int32
Masking int32 // ms (Record.Masking)
Flags uint16
Varied [len(rowVaried)]uint32
Shape uint32
Tokens [6]int64
Order, RouteID int64
Cost, Credits float64
// the times of the call, in ms as in Record, kept in 32 bits (msOf):
// a page holds every row of its period, and they were 36 of a row's
// 216 bytes as int64s (#2278)
Millis, TTFT, FirstText int32
Sent, Flow, FirstReasoning int32
Own, Retried, Queued int32
Masking int32 // ms (Record.Masking)
Status, Tries int32
Flags uint16
}
// msOf is a time in ms as a packedRow keeps it: one past 24 days, which
// no call takes, is kept as the longest it can be.
func msOf(ms int64) int32 { return int32(max(min(ms, math.MaxInt32), math.MinInt32)) }
// rowHandedOn is the Flags bit of Record.HandedOn, rowReasoningHidden
// Record.ReasoningHidden's: 64 is block_price.go's swapKnown, 256
// rowOwnCost
@@ -231,7 +238,7 @@ func (c *rowChunk) add(r Row, msg string, order int64, failed bool) {
c.Bytes += int64(len(s) + 48)
return id
}
p := packedRow{Time: r.Time, Tokens: [6]int64{int64(r.Input), int64(r.Output), int64(r.CacheRead), int64(r.CacheWrite), int64(r.Reasoning), int64(r.CacheWrite1h)}, Millis: r.Millis, TTFT: r.TTFT, FirstText: r.FirstText, FirstReasoning: r.FirstReasoning, Sent: r.Sent, Flow: r.Flow, Own: r.Own, Masking: int32(min(r.Masking, math.MaxInt32)), Retried: r.Retried, Queued: r.Queued, Order: order, RouteID: r.RouteID, Cost: r.Cost, Status: int32(r.Status), Tries: int32(r.Tries)}
p := packedRow{Time: r.Time, Tokens: [6]int64{int64(r.Input), int64(r.Output), int64(r.CacheRead), int64(r.CacheWrite), int64(r.Reasoning), int64(r.CacheWrite1h)}, Millis: msOf(r.Millis), TTFT: msOf(r.TTFT), FirstText: msOf(r.FirstText), FirstReasoning: msOf(r.FirstReasoning), Sent: msOf(r.Sent), Flow: msOf(r.Flow), Own: msOf(r.Own), Masking: int32(min(r.Masking, math.MaxInt32)), Retried: msOf(r.Retried), Queued: msOf(r.Queued), Order: order, RouteID: r.RouteID, Cost: r.Cost, Status: int32(r.Status), Tries: int32(r.Tries)}
var t [rowTexts]uint32
for i, s := range rowText(&r) {
t[i] = intern(*s)
@@ -296,7 +303,7 @@ func (c *rowChunk) add(r Row, msg string, order int64, failed bool) {
}
func (c *rowChunk) row(i int) Row {
p := &c.Rows[i]
r := Row{Record: Record{RouteID: p.RouteID, Time: p.Time, Input: int(p.Tokens[0]), Output: int(p.Tokens[1]), CacheRead: int(p.Tokens[2]), CacheWrite: int(p.Tokens[3]), CacheWrite1h: int(p.Tokens[5]), Reasoning: int(p.Tokens[4]), Millis: p.Millis, TTFT: p.TTFT, FirstText: p.FirstText, FirstReasoning: p.FirstReasoning, ReasoningHidden: p.Flags&rowReasoningHidden != 0, CacheUnknown: p.Flags&rowCacheUnknown != 0, Sent: p.Sent, Flow: p.Flow, Own: p.Own, Masking: int64(p.Masking), Retried: p.Retried, Queued: p.Queued, Status: int(p.Status), Tries: int(p.Tries), HandedOn: p.Flags&rowHandedOn != 0, Rejected: p.Flags&4 != 0, SessionOfficialLogin: p.Flags&8 != 0}, Cost: p.Cost, Priced: p.Flags&1 != 0, Swapped: p.Flags&2 != 0, Routed: p.Flags&32 != 0}
r := Row{Record: Record{RouteID: p.RouteID, Time: p.Time, Input: int(p.Tokens[0]), Output: int(p.Tokens[1]), CacheRead: int(p.Tokens[2]), CacheWrite: int(p.Tokens[3]), CacheWrite1h: int(p.Tokens[5]), Reasoning: int(p.Tokens[4]), Millis: int64(p.Millis), TTFT: int64(p.TTFT), FirstText: int64(p.FirstText), FirstReasoning: int64(p.FirstReasoning), ReasoningHidden: p.Flags&rowReasoningHidden != 0, CacheUnknown: p.Flags&rowCacheUnknown != 0, Sent: int64(p.Sent), Flow: int64(p.Flow), Own: int64(p.Own), Masking: int64(p.Masking), Retried: int64(p.Retried), Queued: int64(p.Queued), Status: int(p.Status), Tries: int(p.Tries), HandedOn: p.Flags&rowHandedOn != 0, Rejected: p.Flags&4 != 0, SessionOfficialLogin: p.Flags&8 != 0}, Cost: p.Cost, Priced: p.Flags&1 != 0, Swapped: p.Flags&2 != 0, Routed: p.Flags&32 != 0}
if p.Flags&rowCredits != 0 {
credits := p.Credits
r.Credits = &credits
+5 -5
View File
@@ -89,7 +89,7 @@ func (c *rowChunk) encode(keep func([]byte) bool) ([]byte, []*time.Location) {
for _, n := range p.Tokens {
put(uint64(n))
}
for _, n := range []int64{p.Millis, p.TTFT, p.FirstText, p.Order, p.RouteID, p.Sent, p.Flow, p.Own, p.Retried, p.Queued, p.FirstReasoning} {
for _, n := range []int64{int64(p.Millis), int64(p.TTFT), int64(p.FirstText), p.Order, p.RouteID, int64(p.Sent), int64(p.Flow), int64(p.Own), int64(p.Retried), int64(p.Queued), int64(p.FirstReasoning)} {
put(uint64(n))
}
put(math.Float64bits(p.Cost))
@@ -279,11 +279,11 @@ func (c *rowChunk) unpackReusing(dst *rowChunk, into []packedRow, strs []string,
for j := range p.Tokens {
p.Tokens[j] = int64(take())
}
p.Millis, p.TTFT, p.FirstText, p.Order = int64(take()), int64(take()), int64(take()), int64(take())
p.Millis, p.TTFT, p.FirstText, p.Order = msOf(int64(take())), msOf(int64(take())), msOf(int64(take())), int64(take())
p.RouteID = int64(take())
p.Sent, p.Flow = int64(take()), int64(take())
p.Own, p.Retried, p.Queued = int64(take()), int64(take()), int64(take())
p.FirstReasoning = int64(take())
p.Sent, p.Flow = msOf(int64(take())), msOf(int64(take()))
p.Own, p.Retried, p.Queued = msOf(int64(take())), msOf(int64(take())), msOf(int64(take()))
p.FirstReasoning = msOf(int64(take()))
p.Cost, p.Status, p.Flags = math.Float64frombits(take()), int32(take()), uint16(take())
p.Tries = int32(take())
p.Masking = int32(take())
+15 -2
View File
@@ -2,6 +2,7 @@ package usage
import (
"fmt"
"math"
"reflect"
"testing"
"time"
@@ -13,8 +14,8 @@ import (
// and 140 of a row's 328 bytes were string ids most rows of a chunk repeat.
// Packed and read back, every row is the row it was.
func TestRowsOfOneSessionShareAShape(t *testing.T) {
if n := unsafe.Sizeof(packedRow{}); n > 216 {
t.Fatalf("a packed row is %d bytes, want 216 at most", n)
if n := unsafe.Sizeof(packedRow{}); n > 184 {
t.Fatalf("a packed row is %d bytes, want 184 at most", n)
}
at := time.Date(2026, 10, 21, 12, 0, 0, 0, time.Local)
c := &rowChunk{}
@@ -53,3 +54,15 @@ func TestRowsOfOneSessionShareAShape(t *testing.T) {
}
}
}
// A row keeps its times in ms in 32 bits: one longer than that, which no
// call takes, reads back as the longest it can keep, not wrapped around.
func TestRowTimesPastWhatARowKeeps(t *testing.T) {
long := int64(30 * 24 * time.Hour / time.Millisecond)
c := &rowChunk{}
c.add(Row{Record: Record{Time: time.Date(2026, 10, 21, 12, 0, 0, 0, time.UTC), Millis: long, Queued: -long, TTFT: 1500}}, "", 0, false)
got := c.row(0)
if got.Millis != math.MaxInt32 || got.Queued != math.MinInt32 || got.TTFT != 1500 {
t.Fatalf("read back Millis %d, Queued %d, TTFT %d; want %d, %d, 1500", got.Millis, got.Queued, got.TTFT, int64(math.MaxInt32), int64(math.MinInt32))
}
}