dirent: each folder keeps its listing before last as room for its next (#2278)
A Sessions poll lists every Codex day folder and Claude Code project. The few rooms given back were shared by all of them, so a small folder's room went to a large one and was grown again, entry by entry, on every poll: 78 MB of 40 unchanged polls with the real page's X-Magpie-Had, the largest single site. Each folder now keeps the listing before its last, cleared, as its own room (keepSpare), inside the same keptEntries bound; the shared rooms are left for folders seen the first time. Run on mote (M5 Pro, macOS 27), with a copy of the real accounts: - go test ./internal/dirent ./internal/sessions: ok. - TestListingsOfDifferentSizesMadeOnce (new): five folders of 1 to 600 files read ten times allocate under 64 KB. Broken by hand (taken never hands back the spare): 394,808 bytes, red. - 40 /api/sessions polls with X-Magpie-Had, real history (~23k session files): 282 MB allocated at v0.1.3492's code, 208 MB with this; the dirent.entry append is gone from the profile. Not changed: distinctOf's slice (1.6 MB a poll). Holding it would keep as much resident as it saves in garbage.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user