Skip to content

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

usage: GatewaySessions hands out the kept projection's lists instead of cloning every session's days for each ask (#2776)

yetone43bd998b1507
diff --git a/internal/usage/gateway_sessions.go b/internal/usage/gateway_sessions.go
index 8eb143629..db56f7f64 100644
--- a/internal/usage/gateway_sessions.go
+++ b/internal/usage/gateway_sessions.go
@@ -58,13 +58,19 @@ type GatewayModel struct {
// GatewaySessions aggregates one usage snapshot without reading routing
// history or agent files. NativeKeys contains identities already represented
// by local session files and is used to suppress the duplicate projection.
+//
+// A session's Models and Daily are the kept projection's own, shared by
+// every caller, which reads them and never changes them: each ask cloned
+// every session's days, and the start's first asks together held ~450 MB
+// of copies over ~720k calls (#2776). They are clipped, so an append
+// makes a slice of the caller's own.
func GatewaySessions(since time.Time, nativeKeys map[string]bool) []GatewaySession {
if since.IsZero() {
e := gatewayProjection()
out := make([]GatewaySession, 0, len(e.sessions))
for _, s := range e.sessions {
if !nativeKeys[s.Agent+"|"+s.ID] {
- out = append(out, cloneGatewaySession(s))
+ out = append(out, s)
}
}
return out
@@ -105,9 +111,9 @@ func gatewaySessionFrom(s GatewaySession, date string) (GatewaySession, bool) {
return GatewaySession{}, false
}
if i == 0 {
- return cloneGatewaySession(s), true
+ return s, true
}
- w := GatewaySession{Agent: s.Agent, ID: s.ID, Models: []GatewayModel{}, Daily: slices.Clone(s.Daily[i:])}
+ w := GatewaySession{Agent: s.Agent, ID: s.ID, Models: []GatewayModel{}, Daily: s.Daily[i:]}
models := map[string]int{} // index in w.Models, which grows
for _, d := range w.Daily {
if w.Start.IsZero() || d.first.Before(w.Start) {
@@ -204,6 +210,8 @@ func gatewayProjection() gatewayProjectionEntry {
e := gatewayProjectionEntry{version: snapshot.version, meta: meta, zone: time.Local, sessions: ss, byID: make(map[string]int, len(ss))}
for i, s := range ss {
e.byID[s.Agent+"|"+s.ID] = i
+ // handed out as they are: an append is the caller's own
+ ss[i].Models, ss[i].Daily = slices.Clip(s.Models), slices.Clip(s.Daily)
}
// A failed or partial read must be retried, not published as current.
if snapshot.info != nil && snapshot.off == snapshot.info.Size() {
diff --git a/internal/usage/gateway_sessions_shared_test.go b/internal/usage/gateway_sessions_shared_test.go
new file mode 100644
index 000000000..0cab4790d
--- /dev/null
+++ b/internal/usage/gateway_sessions_shared_test.go
@@ -0,0 +1,60 @@
+package usage
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+ "time"
+ "unsafe"
+)
+
+// GatewaySessions hands every caller the kept projection's sessions, not a
+// clone of each one's models and days: the start's first asks (the
+// Sessions list, Manage for each agent, Stats) each cloned all of them,
+// ~450 MB of copies at once over ~720k calls (#2776).
+func TestGatewaySessionsHandOutTheKeptProjection(t *testing.T) {
+ pageHome(t)
+ settleLogClock(t)
+ const sessionCount, days = 2000, 10
+ if err := os.MkdirAll(filepath.Dir(Path()), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ f, err := os.Create(Path())
+ if err != nil {
+ t.Fatal(err)
+ }
+ enc := json.NewEncoder(f)
+ now := Clock()
+ for d := range days {
+ for i := range sessionCount {
+ at := now.AddDate(0, 0, -d).Add(-time.Duration(i) * time.Second)
+ if err := enc.Encode(Record{Time: at, Agent: "pi", Session: fmt.Sprintf("s%d", i), Provider: "p", Model: "m", Input: 10, Status: 200}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ }
+ if err := f.Close(); err != nil {
+ t.Fatal(err)
+ }
+ if got := GatewaySessions(time.Time{}, nil); len(got) != sessionCount || len(got[0].Daily) < days {
+ t.Fatalf("%d sessions, the first with %d days", len(got), len(got[0].Daily))
+ }
+ const asks = 10
+ var before, after runtime.MemStats
+ runtime.GC()
+ runtime.ReadMemStats(&before)
+ for range asks {
+ GatewaySessions(time.Time{}, nil)
+ }
+ runtime.ReadMemStats(&after)
+ per := (after.TotalAlloc - before.TotalAlloc) / asks
+ list := uint64(sessionCount) * uint64(unsafe.Sizeof(GatewaySession{}))
+ t.Logf("an ask allocated %d bytes; the list it answers is %d", per, list)
+ // the list itself, and the log's check
+ if per > list+list/2 {
+ t.Fatalf("an ask allocated %d bytes, its list is %d", per, list)
+ }
+}
diff --git a/internal/usage/gateway_sessions_test.go b/internal/usage/gateway_sessions_test.go
index 9affd4158..61b06e89a 100644
--- a/internal/usage/gateway_sessions_test.go
+++ b/internal/usage/gateway_sessions_test.go
@@ -40,10 +40,19 @@ func TestGatewaySessionsCacheRefreshAndOwnership(t *testing.T) {
if len(first) != 1 {
t.Fatalf("first: %+v", first)
}
- first[0].Models[0].Model = "changed by caller"
- first[0].Daily[0].Input = 999
+ // the lists are the kept projection's, read only; an append is the
+ // caller's own (#2776)
again := GatewaySessions(time.Time{}, nil)
- if again[0].Models[0].Model != "m" || again[0].Daily[0].Input != 10 {
+ if &again[0].Daily[0] != &first[0].Daily[0] {
+ t.Fatal("each ask has a copy of the days of its own")
+ }
+ mine := append(first[0].Daily, GatewayDayUsage{Date: "mine"})
+ theirs := append(again[0].Daily, GatewayDayUsage{Date: "theirs"})
+ models := append(first[0].Models, GatewayModel{Model: "mine"})
+ if mine[1].Date != "mine" || theirs[1].Date != "theirs" || &mine[0] == &first[0].Daily[0] || &models[0] == &first[0].Models[0] {
+ t.Fatalf("two callers' appends share the kept lists: %+v %+v", mine, theirs)
+ }
+ if again := GatewaySessions(time.Time{}, nil); len(again[0].Models) != 1 || len(again[0].Daily) != 1 || again[0].Models[0].Model != "m" || again[0].Daily[0].Input != 10 {
t.Fatalf("caller changed cache: %+v", again)
}
if got := GatewaySessions(time.Time{}, map[string]bool{"pi|native": true}); len(got) != 0 {