dirent: getattrlistbulk's read buffers are kept past a collection, not in a sync.Pool (#5990)

TestListingsOfDifferentSizesMadeOnce went red on macOS CI (run
42998160416, at f10c546db) and in #5990's full run: ten polls of
unchanged folders allocated 71-94 KB against a 64 KiB bound. The 64 KiB
read buffer came from a sync.Pool, which a collection empties, a Get on
another P misses, and -race drops one Put in four of; each such miss
made a new buffer inside the window. In the app, that is a new 64 KiB
buffer on the next poll after each collection.

The buffers are now kept on a small free list under a mutex, up to
four, the way listings already are (stats.go says why those aren't in
a pool). The test runs two collections in its window.

Run on mote at origin/main de13afbf6 plus this change:
- go test -count=20 -run TestListingsOfDifferentSizesMadeOnce ./internal/dirent: ok
- go test -race -count=5 ./internal/dirent: ok (#5990: every -race run failed before)
Broken by hand: the old stats_darwin.go with the new test, -count=5:
5 of 5 red, 73856 to 93624 bytes.
Not tried: Linux and Windows, where the file isn't built and the test skips.
This commit is contained in:
yetone
2026-11-11 18:55:01 +08:00
parent de13afbf6f
commit 6b31147b63
2 changed files with 43 additions and 5 deletions
+35 -4
View File
@@ -36,8 +36,39 @@ type attrList struct {
// bufs are getattrlistbulk's answers, one a folder being read, kept for
// the next of a poll's few hundred Codex day folders and Claude Code
// projects (#2278). Nothing is kept of one once its folder's entries are
// read: each name is copied.
var bufs = sync.Pool{New: func() any { b := make([]byte, 64<<10); return &b }}
// read: each name is copied. They are kept the way listings are, not in
// a sync.Pool: a collection empties a pool, a Get on another P doesn't
// take the buffer the last Put left on this one, and -race drops one Put
// in four, so a poll made a new 64 KiB buffer after each (#5990).
var bufs struct {
sync.Mutex
free []*[]byte
}
// keptBufs is how many read buffers are kept: as many as folders read at
// once in a poll, give or take.
const keptBufs = 4
func getBuf() *[]byte {
bufs.Lock()
defer bufs.Unlock()
if n := len(bufs.free); n > 0 {
b := bufs.free[n-1]
bufs.free[n-1] = nil
bufs.free = bufs.free[:n-1]
return b
}
b := make([]byte, 64<<10)
return &b
}
func putBuf(b *[]byte) {
bufs.Lock()
defer bufs.Unlock()
if len(bufs.free) < keptBufs {
bufs.free = append(bufs.free, b)
}
}
// stats reads dir's entries with getattrlistbulk: a Sessions read stats
// each of ~21k session files on every 15 s poll, ~10 µs a file on APFS,
@@ -72,8 +103,8 @@ func each(dir string, f func(name []byte, s Stat) bool) bool {
}
defer unix.Close(fd)
al := attrList{bitmapCount: attrBitMapCount, common: attrCmnReturned | attrCmnName | attrCmnObjType | attrCmnModTime, file: attrFileDataLen}
bp := bufs.Get().(*[]byte)
defer bufs.Put(bp)
bp := getBuf()
defer putBuf(bp)
buf := *bp
for {
n, _, errno := syscall.Syscall6(unix.SYS_GETATTRLISTBULK, uintptr(fd), uintptr(unsafe.Pointer(&al)), uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)), 0, 0)
+8 -1
View File
@@ -215,8 +215,15 @@ func TestListingsOfDifferentSizesMadeOnce(t *testing.T) {
poll()
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
for range 10 {
for i := range 10 {
poll()
if i == 4 {
// Two collections, as a busy process has between polls:
// the read buffers and listings are both still there after
// them (#5990).
runtime.GC()
runtime.GC()
}
}
runtime.ReadMemStats(&after)
if got := after.TotalAlloc - before.TotalAlloc; got > 64<<10 {