Skip to content

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

sessions: a poll's session list is sorted and gathered in slices made for it (#2278)

yetone45fce0495d00
diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go
index b95363ebd..10a3d619f 100644
--- a/internal/gui/sessions.go
+++ b/internal/gui/sessions.go
@@ -104,7 +104,7 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
out.Dirs = append(out.Dirs, tilde(d))
}
list := sessions.List(sessions.All)
- nativeKeys := map[string]bool{}
+ nativeKeys := make(map[string]bool, len(list))
for _, s := range list {
for _, k := range sessions.GatewayKeys(s) {
nativeKeys[k] = true
@@ -140,6 +140,9 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
}
}
vias := usage.Vias(since.Add(-time.Minute))
+ // made to size: up to sessions.All rows, grown by append each poll
+ // was copied through every size on the way, ~140 MB of eight (#2278)
+ out.Sessions = make([]sessionJSON, 0, len(list))
for _, s := range list {
j := sessionJSON{Session: s, Name: s.Agent, Icon: "generic", Via: viaOf(vias, s)}
if a := agents[s.Agent]; a != nil {
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 70d6e4a19..28cd7485d 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -1435,21 +1435,27 @@ func listSessions(limit int) []Session {
defer useDBs()()
files := allFiles()
groups := byKey(files, nil)
- latest := map[string]time.Time{}
- for _, f := range files {
- if f.mod.After(latest[f.key]) {
- latest[f.key] = f.mod
+ // each session's latest file, beside its key: a map of them was
+ // ~72 MB of eight Sessions polls over 34k sessions (#2278)
+ type keyed struct {
+ key string
+ latest time.Time
+ }
+ order := make([]keyed, 0, len(groups))
+ for k, fs := range groups {
+ o := keyed{key: k}
+ for _, f := range fs {
+ if f.mod.After(o.latest) {
+ o.latest = f.mod
+ }
}
+ order = append(order, o)
}
- keys := make([]string, 0, len(groups))
- for k := range groups {
- keys = append(keys, k)
- }
- sort.Slice(keys, func(i, j int) bool {
- if !latest[keys[i]].Equal(latest[keys[j]]) {
- return latest[keys[i]].After(latest[keys[j]])
+ slices.SortFunc(order, func(a, b keyed) int {
+ if c := b.latest.Compare(a.latest); c != 0 {
+ return c
}
- return keys[i] < keys[j]
+ return strings.Compare(a.key, b.key)
})
// the keys are not truncated yet: assemble below drops an empty
// session, and a nonempty one must take its place rather than the
@@ -1465,8 +1471,11 @@ func listSessions(limit int) []Session {
copyIDs := copiesByKeeper(copies, groups)
price, titles := pricer(), codexTitleOf()
- out := []Session{}
- for _, k := range keys {
+ // made to size: grown by append, a list of every session was copied
+ // through each size it grew past, ~130 MB of eight polls (#2278)
+ out := make([]Session, 0, min(limit, len(order)))
+ for _, o := range order {
+ k := o.key
if copies[k] != "" {
continue
}