sessions: a poll's session list is sorted and gathered in slices made for it (#2278)
listSessions kept each group's latest time in a map and sorted a key slice that looked it up; the /api/sessions handler grew its native-key map and its output slice by appending. Under 80s of polls while a session was written (ab17, v0.1.1978 with stats kept) those were 72 MB (latest map), 129 MB (listSessions out) and 139 MB (gui out.Sessions) of 5.4 GB allocated. The groups are now one slice of key and latest, sorted once, and every output is made at its size. Run at this head (mote, Go 1.26.8): - GOOS=linux/windows/darwin go vet -tags nogui ./internal/sessions/ ./internal/gui/ ./internal/usage/: clean. - go test -timeout 45m ./internal/sessions/ ./internal/gui/: sessions ok; gui red only on TestCoreScriptBudget (#4133, red on main sincea4e929212) and TestMainWindowCorners/macOS_26_SDK (#3949, red on macOS 27), neither touched here. Rebased onto origin/mainf982e46f2after the run above; the three #2278 commits were run again together there (see the last commit's body).
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
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]])
|
||||
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
|
||||
}
|
||||
}
|
||||
return keys[i] < keys[j]
|
||||
order = append(order, o)
|
||||
}
|
||||
slices.SortFunc(order, func(a, b keyed) int {
|
||||
if c := b.latest.Compare(a.latest); c != 0 {
|
||||
return c
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user