sessions: an unchanged Sessions poll keeps the last file list
Every 15 s poll made the whole deduped file list again (~1.5 MB on a
real history, distinctOf's 58 MB over 40 polls). allFiles and
allFilesForTitles now compare each pass with the last list, held
weakly so a magpie with the page shut keeps none of it, and give the
same array back while nothing differs. A changed or gone file makes a
new list; the old one is never written into (cap == len). An empty
pass is an empty slice, never nil: refresh reads nil as "not listed"
and kept the parses of deleted files (TestGoneAgentLeavesNoSession
went red on the first try, caught before commit).
Run (mica): go test -count=1 -timeout 45m ./internal/sessions/
./internal/dirent/ ok; go vet of both ok; gofmt clean.
Broken by hand: the nil return makes TestFilesKeptWhileTheSame ("an
empty pass: nil") and TestGoneAgentLeavesNoSession ("5 parses kept")
red; a fresh slice on every pass makes TestFilesKeptWhileTheSame red.
Real account on mote, 40 unchanged /api/sessions polls, alloc_space:
179.5 MB (v0.1.3794 build) -> 147.9 MB; distinctOf gone, the kept
list's own compare 12 MB. /api/sessions body sha identical on both.
Not run: the full ./... suite (mote over its cores; this touches the
sessions package only) and the GUI suite.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"unsafe"
|
||||
"weak"
|
||||
)
|
||||
|
||||
// filesKept is the last list of every agent's session files a pass made,
|
||||
// held weakly. A Sessions poll lists ~20k files, and while none of them
|
||||
// changed it made the same list again, ~1.5 MB a poll (the largest share
|
||||
// of what a poll allocates, and so of its garbage collection). A pass
|
||||
// that lists the files as the last one did gives that list back instead,
|
||||
// and allocates nothing for it. The list is held only as long as
|
||||
// something else holds it or no collection has run since: a magpie with
|
||||
// the page shut keeps none of it.
|
||||
type filesKept struct {
|
||||
sync.Mutex
|
||||
first weak.Pointer[file]
|
||||
n int
|
||||
}
|
||||
|
||||
// allKept are allFiles' and allFilesForTitles' last lists.
|
||||
var allKept [2]filesKept
|
||||
|
||||
func (k *filesKept) last() []file {
|
||||
k.Lock()
|
||||
defer k.Unlock()
|
||||
if k.n == 0 {
|
||||
return nil
|
||||
}
|
||||
p := k.first.Value()
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
return unsafe.Slice(p, k.n)
|
||||
}
|
||||
|
||||
// distinct is distinctOf(lists...), given as the last list it made when
|
||||
// it is the same, file for file. Its readers only read it (heldFiles
|
||||
// gives one list to every reader under a hold already), and it is
|
||||
// clipped, so an append to it is made elsewhere.
|
||||
func (k *filesKept) distinct(lists ...[]file) []file {
|
||||
last := k.last()
|
||||
seen := pathSet()
|
||||
defer putPathSet(seen)
|
||||
var out []file // nil while each file is last's at its place
|
||||
i := 0
|
||||
for _, fs := range lists {
|
||||
for _, f := range fs {
|
||||
if _, ok := seen[f.path]; ok {
|
||||
continue
|
||||
}
|
||||
seen[f.path] = struct{}{}
|
||||
if out == nil {
|
||||
if i < len(last) && last[i] == f {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
n := 0
|
||||
for _, fs := range lists {
|
||||
n += len(fs)
|
||||
}
|
||||
out = append(make([]file, 0, n), last[:i]...)
|
||||
}
|
||||
out = append(out, f)
|
||||
}
|
||||
}
|
||||
if out == nil {
|
||||
if i == len(last) && i > 0 {
|
||||
return last
|
||||
}
|
||||
// files gone from its end; never nil, which callers read as "not
|
||||
// listed" and keep every parse of files that are gone for
|
||||
out = append(make([]file, 0, i), last[:i]...)
|
||||
}
|
||||
out = out[:len(out):len(out)]
|
||||
k.Lock()
|
||||
k.first, k.n = weak.Pointer[file]{}, len(out)
|
||||
if len(out) > 0 {
|
||||
k.first = weak.Make(&out[0])
|
||||
}
|
||||
k.Unlock()
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func sameArray(a, b []file) bool {
|
||||
return len(a) > 0 && len(b) > 0 && unsafe.SliceData(a) == unsafe.SliceData(b)
|
||||
}
|
||||
|
||||
// A pass that lists the files the last one did gives its list back and
|
||||
// allocates nothing for it; any change, a file gone included, is a new
|
||||
// list, and each path is still listed once.
|
||||
func TestFilesKeptWhileTheSame(t *testing.T) {
|
||||
var k filesKept
|
||||
a := []file{{agent: "claude", key: "claude:1", path: "/a"}, {agent: "claude", key: "claude:2", path: "/b"}}
|
||||
b := []file{{agent: "codex", key: "codex:3", path: "/c"}, {agent: "claude", key: "claude:1", path: "/a"}}
|
||||
first := k.distinct(a, b)
|
||||
if want := []file{a[0], a[1], b[0]}; !slices.Equal(first, want) {
|
||||
t.Fatalf("first = %v, want %v", first, want)
|
||||
}
|
||||
again := k.distinct(a, b)
|
||||
if !sameArray(first, again) {
|
||||
t.Fatal("the same files were listed in a new slice")
|
||||
}
|
||||
if n := testing.AllocsPerRun(20, func() { k.distinct(a, b) }); n != 0 {
|
||||
t.Fatalf("an unchanged pass allocated %v times", n)
|
||||
}
|
||||
|
||||
grown := slices.Clone(b)
|
||||
grown[0].size = 10
|
||||
changed := k.distinct(a, grown)
|
||||
if sameArray(changed, first) || changed[2].size != 10 || !slices.Equal(first, []file{a[0], a[1], b[0]}) {
|
||||
t.Fatalf("a grown file: %v (last list now %v)", changed, first)
|
||||
}
|
||||
gone := k.distinct(a)
|
||||
if sameArray(gone, changed) || !slices.Equal(gone, a) {
|
||||
t.Fatalf("a file gone from the end: %v", gone)
|
||||
}
|
||||
// nil reads as "not listed": the parses of files gone would be kept
|
||||
if l := k.distinct(); l == nil || len(l) != 0 {
|
||||
t.Fatalf("an empty pass: %#v", l)
|
||||
}
|
||||
if l := k.distinct(); l == nil {
|
||||
t.Fatal("a second empty pass gave nil")
|
||||
}
|
||||
if len(k.distinct(a)) != 2 {
|
||||
t.Fatal("a pass after an empty one")
|
||||
}
|
||||
if l := k.distinct(a); cap(l) != len(l) {
|
||||
t.Fatalf("cap %d, len %d: an append would write into the kept list", cap(l), len(l))
|
||||
}
|
||||
}
|
||||
@@ -911,14 +911,14 @@ func listCCFiles(agent, dir string) []file {
|
||||
// files.
|
||||
var filesHook func()
|
||||
|
||||
func allFiles() []file { return allFilesWith(zcodeFiles) }
|
||||
func allFiles() []file { return allFilesWith(&allKept[0], zcodeFiles) }
|
||||
|
||||
// allFilesForTitles is allFiles with ZCode's sessions listed from their own
|
||||
// rows alone: a name poll needs titles, not every message counted
|
||||
// (zcodeFiles joins them for a session's size and last activity).
|
||||
func allFilesForTitles() []file { return allFilesWith(zcodeTitleFiles) }
|
||||
func allFilesForTitles() []file { return allFilesWith(&allKept[1], zcodeTitleFiles) }
|
||||
|
||||
func allFilesWith(zcode func() []file) []file {
|
||||
func allFilesWith(kept *filesKept, zcode func() []file) []file {
|
||||
if filesHook != nil {
|
||||
filesHook()
|
||||
}
|
||||
@@ -929,7 +929,7 @@ func allFilesWith(zcode func() []file) []file {
|
||||
antigravityFiles()) // #2190
|
||||
// each lister asked Reads already; a file listed under another
|
||||
// agent's name is held to that agent too
|
||||
return allowed(distinctOf(lists...))
|
||||
return allowed(kept.distinct(lists...))
|
||||
}
|
||||
|
||||
// distinctOf is lists one after another with each path kept once, the
|
||||
|
||||
Reference in New Issue
Block a user