Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

usage: pairing a poll's calls allocates 40% less, in a few slabs (#2278)

yetone06469694b759
diff --git a/internal/usage/codex_identity.go b/internal/usage/codex_identity.go
index 2e0f35f91..b213bb1ee 100644
--- a/internal/usage/codex_identity.go
+++ b/internal/usage/codex_identity.go
@@ -194,28 +194,42 @@ func codexChunkResponsePairs(gateways, locals []*rowChunk, duplicateLocal map[ro
compaction *bool
}
// the picks are one slice the map points into: a pick each was ~300 MB
- // of what two minutes of polls allocated while an agent worked (#2278)
- picks := map[string]int32{}
- var slab []pick
+ // of what two minutes of polls allocated while an agent worked (#2278).
+ // Both are made the size of the gateway's ids, and a gateway call is
+ // judged by the few strings that decide it, not made a Row: grown
+ // from nothing, and a Row each, they were ~2.4 GB of what 80 s of
+ // polls allocated while calls came in, on a year's history.
var rows chunkRows
defer rows.release()
+ withID := 0
for _, chunk := range gateways {
open := rows.of(chunk)
for i := range open.Rows {
- if open.text(&open.Rows[i], rowResponseID) == 0 {
+ if open.text(&open.Rows[i], rowResponseID) != 0 {
+ withID++
+ }
+ }
+ }
+ picks := make(map[string]int32, withID)
+ slab := make([]pick, 0, withID)
+ for _, chunk := range gateways {
+ open := rows.of(chunk)
+ for i := range open.Rows {
+ p := &open.Rows[i]
+ id := open.Strings[open.text(p, rowResponseID)]
+ if id == "" {
continue
}
- row := open.row(i)
- k, ok := picks[row.ResponseID]
+ k, ok := picks[id]
if !ok {
k = int32(len(slab))
- picks[row.ResponseID] = k
+ picks[id] = k
slab = append(slab, pick{})
}
- p := &slab[k]
- p.gateways++
- p.gateway = rowRef{chunk, i}
- p.gatewayOK = isCodexRow(row) && !row.IsRejected() && row.Source != SourceReported
+ pk := &slab[k]
+ pk.gateways++
+ pk.gateway = rowRef{chunk, i}
+ pk.gatewayOK = open.codexGatewayCall(p)
}
}
lent := lentRows() // picks are looked up by a local's id, not keyed
@@ -263,6 +277,24 @@ func codexChunkResponsePairs(gateways, locals []*rowChunk, duplicateLocal map[ro
return pairs, compactions
}
+// codexGatewayCall is whether p, a row of c, is a Codex call the gateway
+// served and logged itself: isCodexRow, not IsRejected and not
+// SourceReported of c.row(p's index), read from p's strings alone.
+func (c *rowChunk) codexGatewayCall(p *packedRow) bool {
+ str := func(slot int) string { return c.Strings[c.text(p, slot)] }
+ if client := str(rowClient); client != "" {
+ if client != "codex" {
+ return false
+ }
+ } else if str(0) != "codex" {
+ return false
+ }
+ if p.Flags&4 != 0 || str(1) == "" && p.Status >= 400 {
+ return false
+ }
+ return str(16) != SourceReported
+}
+
// foldIDs are each chunk's calls by a hash of their response id, kept by
// the chunk's serial (chunkIDs). A page polled while one session was live
// decoded every session file of the period again to hash the same ids:
diff --git a/internal/usage/pair_bench_test.go b/internal/usage/pair_bench_test.go
new file mode 100644
index 000000000..4934c2c93
--- /dev/null
+++ b/internal/usage/pair_bench_test.go
@@ -0,0 +1,47 @@
+package usage
+
+import (
+ "fmt"
+ "testing"
+ "time"
+)
+
+// pairFixture is a long history as pairRows meets it on a poll: files
+// session files of calls Codex calls each, every one also through the
+// gateway by its response id, and as many Claude Code calls matched by
+// session and tokens.
+func pairFixture(files, calls int) (gateways, locals []*rowChunk) {
+ now := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC)
+ g := &rowChunk{serial: 1}
+ n := 0
+ for f := 0; f < files; f++ {
+ c := &rowChunk{serial: uint64(100 + f)}
+ for i := 0; i < calls; i++ {
+ at := now.Add(-time.Duration(n) * 20 * time.Second)
+ n++
+ if f%2 == 0 {
+ id := fmt.Sprintf("resp_%d", n)
+ g.add(Row{Record: Record{Time: at, Agent: "codex", Provider: "openai", Model: "m", Input: 100 + i, Output: 5, ResponseID: id, Session: fmt.Sprint("s", f)}}, "", int64(n), false)
+ c.add(Row{Record: Record{Time: at, Agent: "codex", Provider: "openai", Model: "m", Input: 100 + i, Output: 5, ResponseID: id, Session: fmt.Sprint("s", f)}, Source: "log"}, "", int64(i), false)
+ } else {
+ g.add(Row{Record: Record{Time: at, Agent: "claude-code", Provider: "anthropic", Model: "c", Input: 100 + i, Output: 7, Session: fmt.Sprint("c", f)}}, "", int64(n), false)
+ c.add(Row{Record: Record{Time: at, Agent: "claude-code", Provider: "anthropic", Model: "c", Input: 100 + i, Output: 7, Session: fmt.Sprint("c", f)}, Source: "log"}, fmt.Sprint("msg_", n), int64(i), false)
+ }
+ }
+ locals = append(locals, c)
+ }
+ return []*rowChunk{g}, locals
+}
+
+func BenchmarkPairRows(b *testing.B) {
+ gateways, locals := pairFixture(2000, 100)
+ now := time.Date(2026, 10, 1, 13, 0, 0, 0, time.UTC)
+ b.ReportAllocs()
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ ps := pairRows(All, now, gateways, locals, nil, nil)
+ if len(ps.matched) != 200000 {
+ b.Fatal(len(ps.matched))
+ }
+ }
+}
diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go
index 40a16ceb5..36d56ee5f 100644
--- a/internal/usage/request_page.go
+++ b/internal/usage/request_page.go
@@ -1329,9 +1329,13 @@ type matchEnd struct {
at time.Time
index rowRef
}
+
+// matchGroup is a key's gateway calls, ends[off:off+n] of one slab, by
+// their end; its range counts are counts[off+k:off+k+n+1] for the k-th
+// group. A key holds a call or two, a slice each was ~1 GB of what 80 s
+// of polls allocated while calls came in, on a year's history (#2278).
type matchGroup struct {
- ends []matchEnd
- counts []int
+ off, n int32
}
// agyMatchSession stands for the session of an Antigravity CLI (agy) call
@@ -1454,48 +1458,91 @@ func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip map[rowRef]boo
matched[ref.rowRef], used[calls[k].at(j)] = true, true
}
}
- groupOf := map[matchKey]int32{} // a key's place in groups
- var groups []matchGroup
+ // keyOf is the key of a gateway call the local calls left without a
+ // request id may match, ok false for one they never do
+ keyOf := func(gateway *rowChunk, i int) (matchKey, bool) {
+ if reserved[rowRef{gateway, i}] || used[rowRef{gateway, i}] {
+ return matchKey{}, false
+ }
+ p := &gateway.Rows[i]
+ if (p.Flags&4 != 0 || gateway.text(p, 1) == 0 && p.Status >= 400) || p.Time.Before(gatewaySince) {
+ return matchKey{}, false
+ }
+ session := gateway.Strings[gateway.text(p, 14)]
+ if session == "" {
+ session = gateway.Strings[gateway.text(p, 13)]
+ }
+ if session == "" && gateway.Strings[gateway.text(p, 0)] == "agy" {
+ session = agyMatchSession
+ }
+ if session == "" {
+ return matchKey{}, false
+ }
+ return matchKey{session, matchTokens(p.Tokens), p.Status >= 400 || gateway.text(p, 9) != 0, gateway.text(p, 11) != 0}, true
+ }
+ kept := 0
for _, gateway := range gateways {
for i := range gateway.Rows {
- ref := rowRef{gateway, i}
- if reserved[ref] {
- continue
- }
- p := &gateway.Rows[i]
- if used[ref] || (p.Flags&4 != 0 || gateway.text(p, 1) == 0 && p.Status >= 400) || p.Time.Before(gatewaySince) {
- continue
- }
- session := gateway.Strings[gateway.text(p, 14)]
- if session == "" {
- session = gateway.Strings[gateway.text(p, 13)]
- }
- if session == "" && gateway.Strings[gateway.text(p, 0)] == "agy" {
- session = agyMatchSession
+ if _, ok := keyOf(gateway, i); ok {
+ kept++
}
- if session == "" {
+ }
+ }
+ groupOf := make(map[matchKey]int32, kept) // a key's place in groups
+ var groups []matchGroup
+ ks := make([]int32, 0, kept) // each kept call's group, in the order read
+ for _, gateway := range gateways {
+ for i := range gateway.Rows {
+ key, ok := keyOf(gateway, i)
+ if !ok {
continue
}
- key := matchKey{session, matchTokens(p.Tokens), p.Status >= 400 || gateway.text(p, 9) != 0, gateway.text(p, 11) != 0}
k, ok := groupOf[key]
if !ok {
k = int32(len(groups))
groupOf[key] = k
groups = append(groups, matchGroup{})
}
- groups[k].ends = append(groups[k].ends, matchEnd{p.Time.Add(time.Duration(p.Millis) * time.Millisecond), rowRef{gateway, i}})
+ groups[k].n++
+ ks = append(ks, k)
}
}
- for i := range groups {
- g := &groups[i]
- slices.SortFunc(g.ends, func(a, b matchEnd) int { return a.at.Compare(b.at) })
- g.counts = make([]int, len(g.ends)+1)
+ off := int32(0)
+ for k := range groups {
+ groups[k].off, off = off, off+groups[k].n
+ groups[k].n = 0
}
+ ends := make([]matchEnd, len(ks))
+ next := 0
+ for _, gateway := range gateways {
+ for i := range gateway.Rows {
+ if _, ok := keyOf(gateway, i); !ok {
+ continue
+ }
+ g := &groups[ks[next]]
+ next++
+ p := &gateway.Rows[i]
+ ends[g.off+g.n] = matchEnd{p.Time.Add(time.Duration(p.Millis) * time.Millisecond), rowRef{gateway, i}}
+ g.n++
+ }
+ }
+ ks = nil
+ for k := range groups {
+ g := groups[k]
+ slices.SortFunc(ends[g.off:g.off+g.n], func(a, b matchEnd) int { return a.at.Compare(b.at) })
+ }
+ counts := make([]int32, len(ends)+len(groups))
named := make(map[uint64]struct{}, len(groups))
for key := range groupOf {
named[matchHash(key.session)] = struct{}{}
}
- candidates := map[rowRef]rowRef{}
+ // a local call with one gateway call in its range, and that call's
+ // place in ends
+ type candidate struct {
+ ref rowRef
+ at int32
+ }
+ var candidates []candidate
for n, chunk := range chunks {
if hints[n].latest.Before(since) || !namesAny(hints[n].sessions, named) {
continue
@@ -1519,7 +1566,7 @@ func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip map[rowRef]boo
if c.Strings[c.text(p, 0)] == "agy" {
key.session, to = agyMatchSession, p.Time.Add(agyStepLongest)
}
- count, candidate := 0, (rowRef{})
+ count, at := 0, int32(0)
// Without a local ID either gateway partition can match. With an ID,
// only an unnamed gateway call can match (different IDs stay distinct).
for _, hasID := range []bool{false, true} {
@@ -1531,37 +1578,38 @@ func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip map[rowRef]boo
if !ok {
continue
}
- g := &groups[k]
- lo := sort.Search(len(g.ends), func(i int) bool { return !g.ends[i].at.Before(from) })
- hi := sort.Search(len(g.ends), func(i int) bool { return g.ends[i].at.After(to) })
+ g := groups[k]
+ in := ends[g.off : g.off+g.n]
+ lo := sort.Search(len(in), func(i int) bool { return !in[i].at.Before(from) })
+ hi := sort.Search(len(in), func(i int) bool { return in[i].at.After(to) })
if hi > lo {
count += hi - lo
- candidate = g.ends[lo].index
+ at = g.off + int32(lo)
// Range counts preserve ambiguity even for thousands of identical
// retries, without enumerating their Cartesian product.
- g.counts[lo]++
- g.counts[hi]--
+ counts[g.off+k+int32(lo)]++
+ counts[g.off+k+int32(hi)]--
}
}
if count == 1 {
- candidates[ref] = candidate
+ candidates = append(candidates, candidate{ref, at})
}
}
}
- unique := map[rowRef]bool{}
- for i := range groups {
- g := &groups[i]
- count := 0
- for i, end := range g.ends {
- count += g.counts[i]
+ unique := make([]bool, len(ends))
+ for k := range groups {
+ g := groups[k]
+ count := int32(0)
+ for i := int32(0); i < g.n; i++ {
+ count += counts[g.off+int32(k)+i]
if count == 1 {
- unique[end.index] = true
+ unique[g.off+i] = true
}
}
}
- for ref, i := range candidates {
- if unique[i] {
- matched[ref] = true
+ for _, c := range candidates {
+ if unique[c.at] {
+ matched[c.ref] = true
}
}
return matched