sessions: a poll groups its files by session in one copy, not an append at a time
listSessions, statsRead (twice), nativeIDs, the Codex provider's list
and the two manage paths each grouped the files by key with
groups[f.key] = append(groups[f.key], f): every group grown by
doubling, copying a few-hundred-byte file struct each time, over ~16k
files. byKey counts the kept files per key, makes one slice for all of
them and cuts each group from it with its capacity capped, so a later
append to one group can't write into the next. Each group keeps the
files' order, as before.
Run:
- TestByKeyGroupsAsAppending (new): byKey equals the append loop with
and without a keep, and is unchanged after every group is appended to.
Red with the cap removed (kept[at:at]): a group's first file became
the previous group's appended one.
- go test -timeout 45m ./internal/sessions ./internal/gui
./internal/usageapi: ok (15.1s, 118.9s, 3.4s) at this change on
f3c4d7b7a. gofmt clean; GOOS=linux and GOOS=windows go vet -tags nogui
./internal/sessions ok.
- A/B, 8 sequential polls of the Sessions and Usage pages (state,
sessions, stats, usage, usage/requests, quotas, providers) under a
sandbox HOME holding the real ~/.codex and ~/.claude sessions, alloc
diff between heap profiles: listSessions flat 143 -> 77 MB, statsRead
flat 170 -> 84 MB, byKey's own 92 MB, so ~60 MB less in all;
sessionRoutes 1026 -> 993 MB.
Not tried: the GUI against a real window; the JSON the routes answer is
built from the same groups.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// byKey groups as appending file by file did: the kept files of each
|
||||
// session, in their order, and a group grown later doesn't write into
|
||||
// another's files.
|
||||
func TestByKeyGroupsAsAppending(t *testing.T) {
|
||||
var files []file
|
||||
for i := range 40 {
|
||||
files = append(files, file{agent: []string{"codex", "claude"}[i%2], key: fmt.Sprint("k", i%7), path: fmt.Sprint("p", i)})
|
||||
}
|
||||
for _, keep := range []func(*file) bool{nil, func(f *file) bool { return f.agent == "codex" }} {
|
||||
want := map[string][]file{}
|
||||
for _, f := range files {
|
||||
if keep == nil || keep(&f) {
|
||||
want[f.key] = append(want[f.key], f)
|
||||
}
|
||||
}
|
||||
got := byKey(files, keep)
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("byKey = %v\nwant %v", got, want)
|
||||
}
|
||||
for k := range got {
|
||||
_ = append(got[k], file{key: "grown", path: "grown"})
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("after each group grew, byKey's groups = %v\nwant %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -203,11 +203,10 @@ func MoveCodexProvider(id, to string) (CodexMove, error) {
|
||||
loadCache()
|
||||
defer useDBs()()
|
||||
all := allFiles()
|
||||
groups := map[string][]file{}
|
||||
groups := byKey(all, func(f *file) bool { return f.agent == "codex" })
|
||||
var mine []file
|
||||
for _, f := range all {
|
||||
if f.agent == "codex" {
|
||||
groups[f.key] = append(groups[f.key], f)
|
||||
mine = append(mine, f)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,13 +180,12 @@ func idInFile(agent string) bool {
|
||||
}
|
||||
|
||||
func nativeIDs(agents map[string]bool, all []file) map[string]map[string]bool {
|
||||
groups := map[string][]file{}
|
||||
groups := byKey(all, func(f *file) bool { return agents[f.agent] })
|
||||
var want []file
|
||||
for _, f := range all {
|
||||
if !agents[f.agent] {
|
||||
continue
|
||||
}
|
||||
groups[f.key] = append(groups[f.key], f)
|
||||
if idInFile(f.agent) {
|
||||
want = append(want, f)
|
||||
}
|
||||
@@ -203,11 +202,10 @@ func nativeIDs(agents map[string]bool, all []file) map[string]map[string]bool {
|
||||
}
|
||||
|
||||
func listAgent(agent string, all []file) []Managed {
|
||||
groups := map[string][]file{}
|
||||
groups := byKey(all, func(f *file) bool { return f.agent == agent })
|
||||
var mine []file
|
||||
for _, f := range all {
|
||||
if f.agent == agent {
|
||||
groups[f.key] = append(groups[f.key], f)
|
||||
mine = append(mine, f)
|
||||
}
|
||||
}
|
||||
@@ -313,11 +311,10 @@ func Delete(agent, id string) (Trashed, error) {
|
||||
loadCache()
|
||||
defer useDBs()()
|
||||
all := allFiles()
|
||||
groups := map[string][]file{}
|
||||
groups := byKey(all, func(f *file) bool { return f.agent == agent })
|
||||
var mine []file
|
||||
for _, f := range all {
|
||||
if f.agent == agent {
|
||||
groups[f.key] = append(groups[f.key], f)
|
||||
mine = append(mine, f)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -393,6 +393,35 @@ type file struct {
|
||||
cold bool
|
||||
}
|
||||
|
||||
// byKey groups the files keep takes by session, each group in the files'
|
||||
// order. The groups are cut from one copy of the kept files rather than
|
||||
// grown an append at a time: a file is a few hundred bytes, and a Sessions
|
||||
// poll grouped every one of ~16k files three times, ~150 MB in eight polls.
|
||||
// A nil keep takes every file.
|
||||
func byKey(files []file, keep func(*file) bool) map[string][]file {
|
||||
count := map[string]int{}
|
||||
n := 0
|
||||
for i := range files {
|
||||
if keep == nil || keep(&files[i]) {
|
||||
count[files[i].key]++
|
||||
n++
|
||||
}
|
||||
}
|
||||
kept := make([]file, n)
|
||||
groups := make(map[string][]file, len(count))
|
||||
at := 0
|
||||
for k, c := range count {
|
||||
groups[k] = kept[at : at : at+c]
|
||||
at += c
|
||||
}
|
||||
for i := range files {
|
||||
if f := &files[i]; keep == nil || keep(f) {
|
||||
groups[f.key] = append(groups[f.key], *f)
|
||||
}
|
||||
}
|
||||
return groups
|
||||
}
|
||||
|
||||
// ClaudeDir is Claude Code's folder: $CLAUDE_CONFIG_DIR, else ~/.claude.
|
||||
func ClaudeDir() string { return appdir.ClaudeDir() }
|
||||
|
||||
@@ -1308,10 +1337,9 @@ func listSessions(limit int) []Session {
|
||||
|
||||
defer useDBs()()
|
||||
files := allFiles()
|
||||
groups := map[string][]file{}
|
||||
groups := byKey(files, nil)
|
||||
latest := map[string]time.Time{}
|
||||
for _, f := range files {
|
||||
groups[f.key] = append(groups[f.key], f)
|
||||
if f.mod.After(latest[f.key]) {
|
||||
latest[f.key] = f.mod
|
||||
}
|
||||
@@ -2267,11 +2295,10 @@ func Titles(keys []string) map[string]string {
|
||||
loadCache()
|
||||
defer useDBs()()
|
||||
files := allFiles()
|
||||
groups := map[string][]file{}
|
||||
groups := byKey(files, func(f *file) bool { return want[f.key] })
|
||||
var fs []file
|
||||
for _, f := range files {
|
||||
if want[f.key] {
|
||||
groups[f.key] = append(groups[f.key], f)
|
||||
fs = append(fs, f)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,10 +190,7 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats {
|
||||
identities := codexIdentitySnapshot(files)
|
||||
taskTitles := codexSubagentTaskTitles(files)
|
||||
if native != nil {
|
||||
groups := map[string][]file{}
|
||||
for _, f := range files {
|
||||
groups[f.key] = append(groups[f.key], f)
|
||||
}
|
||||
groups := byKey(files, nil)
|
||||
price, titles := pricer(), codexTitleOf()
|
||||
for _, fs := range groups {
|
||||
// as List: a session nothing was said in is not listed
|
||||
@@ -375,12 +372,7 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats {
|
||||
}
|
||||
}
|
||||
|
||||
groups := map[string][]file{}
|
||||
for _, f := range files {
|
||||
if shares[f.key] != nil {
|
||||
groups[f.key] = append(groups[f.key], f)
|
||||
}
|
||||
}
|
||||
groups := byKey(files, func(f *file) bool { return shares[f.key] != nil })
|
||||
first, _ := time.ParseInLocation(time.DateOnly, out.From, time.Local)
|
||||
for key, sh := range shares {
|
||||
if len(sh.dates) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user