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 ontoe863f520c(gateway only) and rerun after; then onto7632e1be1(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.
105 lines
3.1 KiB
Go
105 lines
3.1 KiB
Go
package sessions
|
|
|
|
import (
|
|
"slices"
|
|
"sync"
|
|
)
|
|
|
|
// held is the session folders' listings kept while a request that reads
|
|
// them more than once is answered. A Sessions poll lists every Claude
|
|
// Code and Codex file for its list (allFiles), then again for the Codex
|
|
// threads' effort (the Requests state's CallSources): ~21k files, read
|
|
// twice a poll.
|
|
var held struct {
|
|
sync.Mutex
|
|
n int
|
|
// gen counts the holds begun: a listing is taken after the latest
|
|
// hold began when it carries that hold's gen. A count, not a time:
|
|
// on Windows two time.Now() a moment apart can be the same, and a
|
|
// hold begun in the tick a listing was taken in counted that older
|
|
// listing as its own (#4114)
|
|
gen uint64
|
|
lists map[string]*heldList
|
|
}
|
|
|
|
type heldList struct {
|
|
gen uint64 // held.gen when it was taken
|
|
files []file
|
|
// closed once files is in: a read that comes while the folder is
|
|
// being listed waits for that listing rather than taking its own
|
|
ready chan struct{}
|
|
}
|
|
|
|
// OneListing has the session folders listed once until done is called:
|
|
// the reads between share each folder's listing, taken after the latest
|
|
// hold began, so none sees the files as they were before its own request
|
|
// came. done lets the listings go once no hold is left.
|
|
func OneListing() (done func()) {
|
|
held.Lock()
|
|
held.n++
|
|
held.gen++
|
|
held.Unlock()
|
|
return letGo()
|
|
}
|
|
|
|
// ShareListing is OneListing for a read that may see the listings the
|
|
// holds there are now share, taken before it came: it begins a hold only
|
|
// when there is none. The Usage page's Sessions tab asks for the list,
|
|
// the stats and the overview at once, and the stats, read again behind
|
|
// (statsFor), listed every session file a second time beside the list's
|
|
// own listing.
|
|
func ShareListing() (done func()) {
|
|
held.Lock()
|
|
defer held.Unlock()
|
|
if held.n++; held.n == 1 {
|
|
held.gen++
|
|
}
|
|
return letGo()
|
|
}
|
|
|
|
func letGo() func() {
|
|
return sync.OnceFunc(func() {
|
|
held.Lock()
|
|
if held.n--; held.n == 0 {
|
|
held.lists = nil
|
|
}
|
|
held.Unlock()
|
|
})
|
|
}
|
|
|
|
// heldWaiting is a test's: called as a read waits for another's listing.
|
|
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. 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 {
|
|
held.Unlock()
|
|
return list()
|
|
}
|
|
if l, ok := held.lists[key]; ok && l.gen == held.gen {
|
|
held.Unlock()
|
|
if heldWaiting != nil {
|
|
heldWaiting()
|
|
}
|
|
<-l.ready
|
|
return l.files
|
|
}
|
|
mine := &heldList{gen: held.gen, ready: make(chan struct{})}
|
|
if held.lists == nil {
|
|
held.lists = map[string]*heldList{}
|
|
}
|
|
held.lists[key] = mine
|
|
held.Unlock()
|
|
defer close(mine.ready)
|
|
// 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
|
|
}
|