diff --git a/internal/dirent/stats.go b/internal/dirent/stats.go index 5c4bdabcd..ed7b207e8 100644 --- a/internal/dirent/stats.go +++ b/internal/dirent/stats.go @@ -54,7 +54,7 @@ func Stats(dir string) (Listing, bool) { l, ok := stats(dir, room, old) if old != nil { listings.Lock() - keepRoom(old) + keepSpare(dir, old) listings.Unlock() } if !ok { @@ -79,6 +79,7 @@ func Stats(dir string) (Listing, bool) { var listings struct { sync.Mutex last map[string]Listing // a folder's last listing + spare map[string]Listing // the one before it, cleared: room for its next room []Listing // cleared entries int // the room of those kept } @@ -94,6 +95,11 @@ func taken(dir string) (old, room Listing) { delete(listings.last, dir) listings.entries -= cap(old) } + if room = listings.spare[dir]; room != nil { + delete(listings.spare, dir) + listings.entries -= cap(room) + return old, room + } n := len(listings.room) if n == 0 { return old, nil @@ -113,6 +119,26 @@ func taken(dir string) (old, room Listing) { return old, room } +// keepSpare keeps l, dir's listing before the one just read, cleared, as +// room for dir's next: the few rooms shared by every folder went from a +// small folder to a large one and were grown again, ~2 MB a poll (#2278). +// listings is held. +func keepSpare(dir string, l Listing) { + clear(l[:cap(l)]) + if listings.entries+cap(l) > keptEntries { + return + } + if prev := listings.spare[dir]; prev != nil { // read twice at once + listings.entries -= cap(prev) + keepRoom(prev) + } + if listings.spare == nil { + listings.spare = map[string]Listing{} + } + listings.spare[dir] = l[:0] + listings.entries += cap(l) +} + // keepRoom keeps l, cleared, as room for a next listing, while the kept // have room. listings is held. func keepRoom(l Listing) { diff --git a/internal/dirent/stats_test.go b/internal/dirent/stats_test.go index 7d89bda47..eea5eac91 100644 --- a/internal/dirent/stats_test.go +++ b/internal/dirent/stats_test.go @@ -138,7 +138,7 @@ func TestListingKeptAcrossCollections(t *testing.T) { t.Skip("listed in one call a folder on macOS only") } listings.Lock() - listings.last, listings.room, listings.entries = nil, nil, 0 // what tests before kept + listings.last, listings.spare, listings.room, listings.entries = nil, nil, nil, 0 // what tests before kept listings.Unlock() dir := t.TempDir() for _, name := range []string{"rollout-a.jsonl", "rollout-b.jsonl", "rollout-c.jsonl"} { @@ -182,3 +182,44 @@ func TestListingKeptAcrossCollections(t *testing.T) { } third.Done(dir) } + +// Folders of different sizes read one after another, as a poll reads +// Codex's day folders and Claude Code's projects, each take their own +// room: a room shared between them went from a small folder to a large +// one and was grown again on every poll (#2278). +func TestListingsOfDifferentSizesMadeOnce(t *testing.T) { + if runtime.GOOS != "darwin" { + t.Skip("listed in one call a folder on macOS only") + } + listings.Lock() + listings.last, listings.spare, listings.room, listings.entries = nil, nil, nil, 0 + listings.Unlock() + var dirs []string + for _, n := range []int{2, 600, 5, 300, 1} { + dir := t.TempDir() + for i := range n { + if err := os.WriteFile(filepath.Join(dir, fmt.Sprintf("rollout-%04d.jsonl", i)), nil, 0o644); err != nil { + t.Fatal(err) + } + } + dirs = append(dirs, dir) + } + poll := func() { + for _, dir := range dirs { + if l, ok := Stats(dir); ok { + l.Done(dir) + } + } + } + poll() + poll() + var before, after runtime.MemStats + runtime.ReadMemStats(&before) + for range 10 { + poll() + } + runtime.ReadMemStats(&after) + if got := after.TotalAlloc - before.TotalAlloc; got > 64<<10 { + t.Errorf("ten polls of unchanged folders allocated %d bytes, want next to none", got) + } +}