From 7037cd9801199e2ff7eeb6a33394114cf6a6a0ea Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 30 Oct 2026 10:26:03 +0800 Subject: [PATCH] sessions: the reads under a hold share one listing, and a pass's path set is reused (#2278) A minute of the page's polls over a long history (52G Codex, 13G Claude) allocated ~70 MB copying the held Codex listing for every reader, ~35 MB on pruneCalls' and refresh's path maps, and ~40 MB more on distinctOf's set and the Requests/effort listing made for each ask. Allocation is what drives the collector's ~33% of CPU. - heldFiles and the warm-up's listing hand every reader the same slice, clipped so an append is made elsewhere. Every caller only reads it or copies it (distinctOf, appends into their own); the one that sorted it, traceLineFiles, now concatenates into its own slice. - distinctOf, pruneCalls and refresh take their path sets from a pool and clear them after the pass. - CallSourcesAndCodexIdentities builds its listing in a pooled buffer given back once its sources are taken. TestWarmUpListsRolloutsOnce checked that a caller could write over the kept listing; it now checks the listing is shared and clipped. TestHeldListingShared and TestListingPoolsGiveBackEmpty are new. Run on mote at this tree, rebased onto e863f520c (gateway only) and rerun after; then onto 7632e1be1 (internal/redact only): - go vet -tags nogui ./internal/sessions/ ./internal/gui/ ./internal/usage/ for linux, windows and darwin: clean. - go test -race ./internal/sessions/ ./internal/usage/: ok. ./internal/gui/: ok but TestMainWindowCorners (#3949, known). - Broken by hand: heldFiles' waiter returning slices.Clone again turns TestHeldListingShared red ("each read under the hold got a copy of the listing"). - The full-history bench (10 endpoints every 5s, heap profiles at 50s and 110s, alloc_space diff), two rounds each, v0.1.2121 against this: slices.Clone of the listing 69 / 30 MB -> gone; pruneCalls 36 / 25 MB -> 5 / 0.5 MB; distinctOf 122 / 81 MB -> 83 / 84 MB (its make and distinctInto). The totals per minute (1216 / 649 MB against 598 / 675 MB) swing with mote's load (~100 from other sessions) more than the change moves them, so they say nothing either way. Not changed: the Codex listing itself (codexFilesIn, ~160-230 MB a minute) is still made new for each hold, and readRequestState (~310-390 MB) still reads every source per ask. Those are next. --- internal/sessions/call_cache.go | 11 ++++---- internal/sessions/calls.go | 32 ++++++++++++++++++++--- internal/sessions/one_listing.go | 15 ++++++----- internal/sessions/one_listing_test.go | 33 +++++++++++++++++++++++ internal/sessions/sessions.go | 36 ++++++++++++++++++++------ internal/sessions/traces.go | 5 +++- internal/sessions/warm_listing_test.go | 8 +++--- 7 files changed, 114 insertions(+), 26 deletions(-) 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 {