Skip to content

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

sessions: the reads under a hold share one listing, and a pass's path set is reused (#2278)

yetone7037cd980119
diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go
index 11d52f5e2..1d361424a 100644
--- a/internal/sessions/call_cache.go
+++ b/internal/sessions/call_cache.go
@@ -102,13 +102,14 @@ func pruneCalls(files []file) {
callCache, callOrder, callRoot = map[string]*callFile{}, nil, root
callCounts = map[string]int{}
}
- on := make(map[string]bool, len(files))
+ on := pathSet()
+ defer putPathSet(on)
for _, f := range files {
- on[f.path] = true
+ on[f.path] = struct{}{}
}
order := callOrder[:0]
for _, path := range callOrder {
- if on[path] {
+ if _, ok := on[path]; ok {
order = append(order, path)
} else {
delete(callCache, path)
@@ -117,14 +118,14 @@ func pruneCalls(files []file) {
callOrder = order
gone := false
for path := range callCounts {
- if !on[path] {
+ if _, ok := on[path]; !ok {
delete(callCounts, path)
gone = true
}
}
order = callContinuationOrder[:0]
for _, path := range callContinuationOrder {
- if on[path] {
+ if _, ok := on[path]; ok {
order = append(order, path)
} else {
delete(callContinuations, path)
diff --git a/internal/sessions/calls.go b/internal/sessions/calls.go
index aabd1f25a..972137696 100644
--- a/internal/sessions/calls.go
+++ b/internal/sessions/calls.go
@@ -331,12 +331,18 @@ func callFileLists(codex []file) [][]file {
func callSources() []file { return callSourcesWith(codexFiles()) }
// callSourcesWith is callSources with codex the rollouts codexFiles found.
-func callSourcesWith(codex []file) []file {
+func callSourcesWith(codex []file) []file { return callSourcesInto(nil, codex) }
+
+// callSourcesInto is callSourcesWith appended to out.
+func callSourcesInto(out []file, codex []file) []file {
lists := append(callFileLists(codex), openCodeCallFiles(), zcodeCallFiles(), dshCallFiles(),
minimaxCallFiles(), kimiCallFiles(), wslFiles("kimi"), piCallFiles(), codebuddyCallFiles(),
agyCallFiles(), // #2097
workbuddyCallFiles())
- return allowed(distinctOf(lists...))
+ if out == nil {
+ return allowed(distinctOf(lists...))
+ }
+ return allowed(distinctInto(out, lists...))
}
// desktopDataDirs are Claude Desktop's folders on this computer that
@@ -1178,10 +1184,30 @@ func CallSources() []CallSource { return callSourcesOf(callSources()) }
// ~150ms of each rebuild.
func CallSourcesAndCodexIdentities() ([]CallSource, map[string]CodexIdentity) {
codex := codexFiles()
- sources := callSourcesOf(callSourcesWith(codex))
+ // the listing is let go once its sources are taken: made new on each
+ // Requests and effort ask, it was ~100 MB of a minute of polls (#2278)
+ fs := callSourcesInto(listingBuf(), codex)
+ sources := callSourcesOf(fs)
+ putListingBuf(fs)
return sources, codexIdentitiesOf(codex)
}
+// listingBufs are listings a read takes and lets go of before it ends.
+var listingBufs sync.Pool
+
+func listingBuf() []file {
+ if b, ok := listingBufs.Get().(*[]file); ok {
+ return (*b)[:0]
+ }
+ return nil
+}
+
+func putListingBuf(fs []file) {
+ clear(fs[:cap(fs)]) // its files' strings go with the listing
+ fs = fs[:0]
+ listingBufs.Put(&fs)
+}
+
func callSourcesOf(fs []file) []CallSource {
pruneCalls(fs)
out := make([]CallSource, 0, len(fs))
diff --git a/internal/sessions/one_listing.go b/internal/sessions/one_listing.go
index 8e214c5f7..a6b4a4259 100644
--- a/internal/sessions/one_listing.go
+++ b/internal/sessions/one_listing.go
@@ -72,7 +72,8 @@ var heldWaiting func()
// heldFiles is list(), or, under a hold, the listing of key taken since
// the latest hold began. The folders are listed outside the lock, once
-// for the reads that ask meanwhile.
+// for the reads that ask meanwhile. The listing is theirs together: a
+// reader never writes to it, and sorts or filters a copy of its own.
func heldFiles(key string, list func() []file) []file {
held.Lock()
if held.n == 0 {
@@ -85,7 +86,7 @@ func heldFiles(key string, list func() []file) []file {
heldWaiting()
}
<-l.ready
- return slices.Clone(l.files)
+ return l.files
}
mine := &heldList{gen: held.gen, ready: make(chan struct{})}
if held.lists == nil {
@@ -94,8 +95,10 @@ func heldFiles(key string, list func() []file) []file {
held.lists[key] = mine
held.Unlock()
defer close(mine.ready)
- files := list()
- // before ready is closed, so whoever waited reads it
- mine.files = slices.Clone(files)
- return files
+ // one listing for every read under the hold, not a copy each: ~95 MB
+ // of a minute of polls (#2278). They only read it, and clipped, an
+ // append to it is made elsewhere. Before ready is closed, so whoever
+ // waited reads it.
+ mine.files = slices.Clip(list())
+ return mine.files
}
diff --git a/internal/sessions/one_listing_test.go b/internal/sessions/one_listing_test.go
index 23f597e13..4dc4b757d 100644
--- a/internal/sessions/one_listing_test.go
+++ b/internal/sessions/one_listing_test.go
@@ -118,3 +118,36 @@ func TestShareListing(t *testing.T) {
t.Fatalf("holds left: %d, lists %v", held.n, held.lists)
}
}
+
+// The reads under a hold share one listing, not a copy each (#2278), and
+// it is clipped: an append made to it by one reader leaves the others'
+// as it was.
+func TestHeldListingShared(t *testing.T) {
+ holdsEnd(t)
+ list := func() []file { return append(make([]file, 0, 8), file{path: "a"}, file{path: "b"}) }
+ done := OneListing()
+ defer done()
+ one, two := heldFiles("k", list), heldFiles("k", list)
+ if &one[0] != &two[0] {
+ t.Fatal("each read under the hold got a copy of the listing")
+ }
+ _ = append(one, file{path: "c"})
+ if three := heldFiles("k", list); len(three) != 2 || three[:cap(three)][len(three)-1].path != "b" {
+ t.Fatalf("an append to one read's listing reached another's: %v", three[:cap(three)])
+ }
+}
+
+// A listing set or buffer given back holds nothing of the pass before.
+func TestListingPoolsGiveBackEmpty(t *testing.T) {
+ for range 3 {
+ got := distinctOf([]file{{path: "a"}, {path: "b"}}, []file{{path: "a"}, {path: "c"}})
+ if len(got) != 3 || got[0].path != "a" || got[1].path != "b" || got[2].path != "c" {
+ t.Fatalf("distinctOf = %v", got)
+ }
+ }
+ fs := append(listingBuf(), file{path: "x", agent: "codex"})
+ putListingBuf(fs)
+ if b := listingBuf(); len(b) != 0 || (cap(b) > 0 && b[:1][0].path != "") {
+ t.Fatalf("a listing given back kept its files: %v", b[:cap(b)])
+ }
+}
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index a3f40e3a3..2b25591dd 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -645,8 +645,13 @@ func distinctOf(lists ...[]file) []file {
for _, fs := range lists {
n += len(fs)
}
- out := make([]file, 0, n)
- seen := make(map[string]struct{}, n)
+ return distinctInto(make([]file, 0, n), lists...)
+}
+
+// distinctInto is distinctOf appended to out.
+func distinctInto(out []file, lists ...[]file) []file {
+ seen := pathSet()
+ defer putPathSet(seen)
for _, fs := range lists {
for _, f := range fs {
if _, ok := seen[f.path]; !ok {
@@ -658,6 +663,20 @@ func distinctOf(lists ...[]file) []file {
return out
}
+// pathSets are sets of paths made for one pass over a listing and given
+// back after it: a set of every session file's path, made new on each of
+// a poll's several passes (distinctOf, refresh, pruneCalls), was ~100 MB
+// of what a minute of polls allocated over ~34k files (#2278). A set
+// keeps its room when cleared.
+var pathSets = sync.Pool{New: func() any { return map[string]struct{}{} }}
+
+func pathSet() map[string]struct{} { return pathSets.Get().(map[string]struct{}) }
+
+func putPathSet(m map[string]struct{}) {
+ clear(m)
+ pathSets.Put(m)
+}
+
// distinctPaths is fs with each path kept once, the first time it is
// listed. A path is one file to read and count: a session a database
// holds twice under one id (a TEXT and a BLOB row, #2932), or a folder
@@ -742,10 +761,10 @@ func codexFiles() []file {
warmListing.Lock()
defer warmListing.Unlock()
if warmListing.files != nil && time.Since(warmListing.at) < warmListingFor {
- return slices.Clone(warmListing.files)
+ return warmListing.files // only read, as heldFiles' are
}
- files := listCodexFiles()
- warmListing.at, warmListing.files = time.Now(), slices.Clone(files)
+ files := slices.Clip(listCodexFiles())
+ warmListing.at, warmListing.files = time.Now(), files
return files
}
@@ -1165,12 +1184,13 @@ func refresh(want, all []file) {
}
gone := false
if len(cache) > 0 && all != nil {
- on := make(map[string]bool, len(all))
+ on := pathSet()
+ defer putPathSet(on)
for _, f := range all {
- on[f.path] = true
+ on[f.path] = struct{}{}
}
for p := range cache {
- if !on[p] {
+ if _, ok := on[p]; !ok {
delete(cache, p)
gone = true
}
diff --git a/internal/sessions/traces.go b/internal/sessions/traces.go
index 15ecea47b..14ab98dd6 100644
--- a/internal/sessions/traces.go
+++ b/internal/sessions/traces.go
@@ -13,6 +13,7 @@ import (
"math"
"os"
"path/filepath"
+ "slices"
"strings"
"time"
)
@@ -87,7 +88,9 @@ func TraceAgent(agent string) bool {
// Claude child transcripts are discovered alongside their parent stores.
func traceLineFiles() []file {
- files := append(codexFiles(), piFiles()...)
+ // a slice of its own: traceRecentFiles sorts it, and Codex's listing
+ // is shared (heldFiles)
+ files := slices.Concat(codexFiles(), piFiles())
claude := func(agent, dir string) { files = append(files, ccFiles(agent, dir)...) }
claude("claude", ClaudeDir())
for _, dir := range desktopReadDirs() {
diff --git a/internal/sessions/warm_listing_test.go b/internal/sessions/warm_listing_test.go
index c5441d98b..0a50f3f9e 100644
--- a/internal/sessions/warm_listing_test.go
+++ b/internal/sessions/warm_listing_test.go
@@ -23,9 +23,11 @@ func TestWarmUpListsRolloutsOnce(t *testing.T) {
if len(fs) != 1 {
t.Fatalf("warm-up listed again: %d rollouts", len(fs))
}
- fs[0] = file{}
- if again := codexFiles(); len(again) != 1 || again[0].path != first {
- t.Fatalf("a caller's change reached the kept listing: %+v", again)
+ // the kept listing is shared, not copied for each read (#2278), and
+ // clipped: an append to one read's is made elsewhere
+ _ = append(fs, file{})
+ if again := codexFiles(); len(again) != 1 || &again[0] != &fs[0] || cap(again) != 1 {
+ t.Fatalf("the kept listing was copied, or an append reached it: %+v", again)
}
done := Ask()
if n := len(codexFiles()); n != 2 {