sessions: an unchanged folder's look allocates nothing (#2278)

readDirectory looked at each folder with os.Stat on every Sessions poll
to see whether its cached listing still held. os.Stat builds a FileInfo
and a name each time, ~6 MB per 40 polls on a real history. dirStat reads
the same mod time, size and kind with unix.Stat into a stack Stat_t;
other systems keep os.Stat.

Run (mica, at this tree): go vet ./internal/sessions for darwin, linux,
windows; GOOS=freebsd go build ./internal/sessions; go test ./internal/sessions
ok (61.8s). TestDirStatAsOSStat passes; with the old discovery.go it fails
("an unchanged folder's look allocated 2 times").

Real account (mote, 40 polls of /api/sessions): w8 34.16 MB allocated,
os.statNolog 6 MB; w9 31.28 MB, os.statNolog not in the top list.
This commit is contained in:
yetone
2026-11-12 05:36:00 +08:00
parent 57855191f0
commit f8248ccd70
4 changed files with 97 additions and 4 deletions
+17
View File
@@ -0,0 +1,17 @@
//go:build !unix
package sessions
import (
"os"
"time"
)
// dirStat is os.Stat's mod time, size and whether path is a folder.
func dirStat(path string) (mod time.Time, size int64, dir bool, err error) {
st, err := os.Stat(path)
if err != nil {
return time.Time{}, 0, false, err
}
return st.ModTime(), st.Size(), st.IsDir(), nil
}
+47
View File
@@ -0,0 +1,47 @@
package sessions
import (
"errors"
"io/fs"
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
// A folder a Sessions poll looks at again, unchanged, costs its listing
// nothing and its stat no FileInfo: os.Stat's was ~4.5 MB of garbage per
// 40 polls on a real history (#2278). dirStat says what os.Stat does.
func TestDirStatAsOSStat(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "f.jsonl")
if err := os.WriteFile(file, []byte("{}\n"), 0o600); err != nil {
t.Fatal(err)
}
old := time.Now().Add(-time.Hour).Truncate(time.Millisecond)
os.Chtimes(dir, old, old)
for _, p := range []string{dir, file} {
st, err := os.Stat(p)
if err != nil {
t.Fatal(err)
}
mod, size, isDir, err := dirStat(p)
if err != nil || !mod.Equal(st.ModTime()) || size != st.Size() || isDir != st.IsDir() {
t.Fatalf("%s: %v %d %t %v, os.Stat %v %d %t", p, mod, size, isDir, err, st.ModTime(), st.Size(), st.IsDir())
}
}
if _, _, _, err := dirStat(filepath.Join(dir, "gone")); !errors.Is(err, fs.ErrNotExist) {
t.Fatalf("a folder gone: %v", err)
}
if got := readDirectory(dir); len(got) != 1 {
t.Fatalf("listed %d", len(got))
}
if runtime.GOOS == "windows" {
return // os.Stat there
}
// the path's C string is all an unchanged folder's look allocates
if n := testing.AllocsPerRun(50, func() { readDirectory(dir) }); n > 1 {
t.Fatalf("an unchanged folder's look allocated %.0f times", n)
}
}
+29
View File
@@ -0,0 +1,29 @@
//go:build unix
package sessions
import (
"io/fs"
"time"
"golang.org/x/sys/unix"
)
// dirStat is os.Stat's mod time, size and whether path is a folder, read
// into a Stat_t on the stack: os.Stat made a FileInfo of each of the few
// hundred folders a Sessions poll looks at, ~4.5 MB of garbage per 40 polls
// on a real history (#2278). Its errors are the errno, which errors.Is
// matches to fs.ErrNotExist as os.Stat's are.
func dirStat(path string) (mod time.Time, size int64, dir bool, err error) {
var st unix.Stat_t
for {
err = unix.Stat(path, &st)
if err != unix.EINTR {
break
}
}
if err != nil {
return time.Time{}, 0, false, &fs.PathError{Op: "stat", Path: path, Err: err}
}
return time.Unix(st.Mtim.Unix()), st.Size, st.Mode&unix.S_IFMT == unix.S_IFDIR, nil
}
+4 -4
View File
@@ -40,17 +40,17 @@ const racyDirectory = 2 * time.Second
// left the Sessions page, and its parses were let go with them, until a
// later poll read the folder again and parsed them anew.
func readDirectory(path string) []os.DirEntry {
st, err := os.Stat(path)
mod, size, dir, err := dirStat(path)
if err != nil {
return unreadDirectory(path, err)
}
if !st.IsDir() {
if !dir {
return nil
}
directoryCache.Lock()
old, ok := directoryCache.entries[path]
directoryCache.Unlock()
if ok && old.mod.Equal(st.ModTime()) && old.size == st.Size() && old.at.Sub(old.mod) > racyDirectory {
if ok && old.mod.Equal(mod) && old.size == size && old.at.Sub(old.mod) > racyDirectory {
return old.entries
}
at := time.Now()
@@ -65,7 +65,7 @@ func readDirectory(path string) []os.DirEntry {
if len(directoryCache.entries) >= 16384 {
directoryCache.entries = map[string]directoryEntry{}
}
directoryCache.entries[path] = directoryEntry{st.ModTime(), st.Size(), at, entries}
directoryCache.entries[path] = directoryEntry{mod, size, at, entries}
directoryCache.Unlock()
return entries
}