Files
magpie/internal/sessions/one_listing_test.go
T
yetone eadeb89c48 sessions, dirent: a poll's listings are kept across collections (#2278)
A full history's page polls listed the same folders every few seconds
into a fresh slice, map and set each time: the sync.Pools meant to keep
them were emptied by the collector, which runs about twice a second at
this size, before the next poll took them.

- sessions: listingBufs and pathSets are shelves (shelf.go), which keep
  up to four across collections. listCCFiles makes its slice with the
  room of the folder's last listing.
- dirent: Stats keeps each folder's last listing (Listing.Done(dir)) and
  the room of the one before, up to 1<<17 entries in all. A name already
  in the folder's last listing is taken from it, not copied again. 1<<16
  kept too little of ~23k session files and their folders: the first
  bench of this change (budget 1<<16) took dirent.entry from 67-102 to
  80-100 MB a minute, no gain.

Bench, mote, the real full history (52G Codex in 12,495 files, 13G
Claude Code in 10,186), fullallogh.sh, alloc_space between the 50s and
110s heap profiles, MB per minute, before (v0.1.2132 equivalent) -> after:
  distinctOf      59      -> 22
  listCCFiles     42      -> 17
  dirent.entry    67-102  -> 55
  dirent.nameOf   24      -> 5
Totals swing 400-1200 MB with mote's load (other sessions), so these are
per function.

Run at this head (rebased on 3bebc3839), on mote:
- go build ./... ok
- go test -race -count=1 ./internal/dirent ok, ./internal/sessions ok
  (109.7s; one earlier run had TestLongReadKeepsWhatItRead red with a
  data race in testenv.Zone, red on 3bebc3839 too, 1 of 80 at -count=40
  -cpu 1,4: #4360)
- ./internal/gui: TestMainWindowCorners red, #3949 (mote is macOS 27);
  gui is not touched here
- GOOS=linux, windows, darwin go vet -tags nogui ./internal/dirent
  ./internal/sessions ok; gofmt -l clean

Broken by hand:
- TestListingKeptAcrossCollections (dirent) and
  TestListingPoolsKeptAcrossCollections (sessions) are red on the
  sync.Pool code: "a listing given back was let go by a garbage
  collection".
- name reuse turned off: "a name listed before was copied again".

Not tried: the Windows and Linux listers (stats_other.go returns no
listing there, as before).
2026-10-30 12:25:23 +08:00

188 lines
5.1 KiB
Go

package sessions
import (
"runtime"
"sync/atomic"
"testing"
"unsafe"
)
// holdsEnd ends, when the test does, every hold it left: one that fails
// before its done() would otherwise have every later test in the package
// read the listing it held (#4177).
func holdsEnd(t *testing.T) {
t.Cleanup(func() {
held.Lock()
held.n, held.lists = 0, nil
held.Unlock()
})
}
// Under a hold a folder is listed once; a hold begun after a listing has
// it listed again, so no request reads files from before it came; with
// no hold left every read lists.
func TestOneListing(t *testing.T) {
holdsEnd(t)
n := 0
list := func() []file { n++; return []file{{path: "a"}} }
read := func() {
if fs := heldFiles("k", list); len(fs) != 1 || fs[0].path != "a" {
t.Fatalf("heldFiles = %v", fs)
}
}
read()
read()
if n != 2 {
t.Fatalf("listed %d times with no hold, want 2", n)
}
n = 0
done := OneListing()
read()
read()
if n != 1 {
t.Fatalf("listed %d times under one hold, want 1", n)
}
other := OneListing()
read()
if n != 2 {
t.Fatalf("a hold begun after the listing read it: listed %d times, want 2", n)
}
read()
done()
read()
if n != 2 {
t.Fatalf("listed %d times while a hold was left, want 2", n)
}
other()
other()
read()
if n != 3 {
t.Fatalf("listed %d times after the holds ended, want 3", n)
}
if held.n != 0 || held.lists != nil {
t.Fatalf("holds left: %d, lists %v", held.n, held.lists)
}
}
// A read that comes while a folder is being listed under a hold waits for
// that listing; a shared hold reads the listing a hold before it took, and
// begins one of its own when there is none.
func TestShareListing(t *testing.T) {
holdsEnd(t)
var n atomic.Int32
listing, release := make(chan struct{}), make(chan struct{})
slow := func() []file {
if n.Add(1) == 1 {
close(listing)
<-release
}
return []file{{path: "a"}}
}
waiting := make(chan struct{}, 1)
heldWaiting = func() { waiting <- struct{}{} }
t.Cleanup(func() { heldWaiting = nil })
done := OneListing()
got := make(chan []file, 2)
go func() { got <- heldFiles("k", slow) }()
select {
case <-listing:
case <-got:
t.Fatal("a hold's first read didn't list: a listing was left from before it")
}
shared := ShareListing()
go func() { got <- heldFiles("k", slow) }()
select {
case <-waiting:
case <-got:
t.Fatal("a read during the listing didn't wait for it")
}
close(release)
for range 2 {
if fs := <-got; len(fs) != 1 || fs[0].path != "a" {
t.Fatalf("heldFiles = %v", fs)
}
}
if n.Load() != 1 {
t.Fatalf("listed %d times for a read during the listing and a shared hold, want 1", n.Load())
}
done()
shared()
n.Store(1) // listing again is quick
shared = ShareListing()
heldFiles("k", slow)
heldFiles("k", slow)
shared()
if n.Load() != 2 {
t.Fatalf("a shared hold with none before it listed %d times, want 1", n.Load()-1)
}
if held.n != 0 || held.lists != nil {
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)])
}
}
// A listing or path set given back is kept across garbage collections: a
// poll collects about twice a second, and the sync.Pools they were in,
// emptied by each, had none for most reads (#2278).
func TestListingPoolsKeptAcrossCollections(t *testing.T) {
for ok := true; ok; _, ok = listingBufs.take(nil) {
}
for ok := true; ok; _, ok = pathSets.take(nil) {
}
fs := append(listingBuf(), make([]file, 64)...)
room := unsafe.SliceData(fs)
putListingBuf(fs)
m := pathSet()
m["a"] = struct{}{}
putPathSet(m)
runtime.GC()
runtime.GC()
if b := listingBuf(); cap(b) == 0 || unsafe.SliceData(b[:1]) != room {
t.Error("a listing given back was let go by a garbage collection")
} else {
putListingBuf(b)
}
got := pathSet()
defer putPathSet(got)
if len(got) != 0 {
t.Errorf("a path set given back kept its paths: %v", got)
}
got["b"] = struct{}{}
if _, ok := m["b"]; !ok {
t.Error("a path set given back was let go by a garbage collection")
}
}