sessions: a kept folder listing is one array (#2278)
readDirectory keeps each folder's listing to tell an unchanged folder from a changed one. os.ReadDir's entries are a pointer each to a separate unixDirent plus its name, so a Codex sessions tree of 10k files held 20k small objects for as long as magpie ran. The kept listing is now one []keptEntry (dir, name, type) with pointers into it: one object per entry, and Info() still lstats on demand as os.ReadDir's does. Windows keeps os.ReadDir's entries (their Info is already filled and cheap). Measured on mote, 10k-file folder, HeapAlloc/HeapObjects kept across readDirectory, 3 runs each: before: 1,297,928 B, 19,996 objects (129.8 B/entry) after: 1,042,664 B, 9,994 objects (104.3 B/entry) -20% bytes and half the objects the GC scans each cycle. Run: go test ./internal/sessions on mote (TestKeptListingIsCompact checks entries equal os.ReadDir's in Name/IsDir/Type/Info, are contiguous, and an unchanged folder is not listed again). Not tried: Windows (path unchanged there).
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -63,6 +64,7 @@ func readDirectory(path string) []os.DirEntry {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
entries = compactEntries(path, entries)
|
||||
directoryCache.Lock()
|
||||
if len(directoryCache.entries) >= 16384 {
|
||||
directoryCache.entries = map[string]directoryEntry{}
|
||||
@@ -72,6 +74,36 @@ func readDirectory(path string) []os.DirEntry {
|
||||
return entries
|
||||
}
|
||||
|
||||
// keptEntry is a kept listing's entry. A listing is kept as long as its
|
||||
// folder doesn't change, ~16k of them across a long Claude Code history, so
|
||||
// they sit in one array its entries point into: os.ReadDir gives each its
|
||||
// own allocation, and the slice it returns room to grow (#2278).
|
||||
type keptEntry struct {
|
||||
dir, name string
|
||||
typ fs.FileMode
|
||||
}
|
||||
|
||||
func (e *keptEntry) Name() string { return e.name }
|
||||
func (e *keptEntry) IsDir() bool { return e.typ.IsDir() }
|
||||
func (e *keptEntry) Type() fs.FileMode { return e.typ }
|
||||
func (e *keptEntry) Info() (fs.FileInfo, error) { return os.Lstat(filepath.Join(e.dir, e.name)) }
|
||||
func (e *keptEntry) String() string { return fs.FormatDirEntry(e) }
|
||||
|
||||
// compactEntries is entries, read from dir, held as keptEntry. Not on
|
||||
// Windows, whose entries carry what Info answers and keptEntry would stat for.
|
||||
func compactEntries(dir string, entries []os.DirEntry) []os.DirEntry {
|
||||
if runtime.GOOS == "windows" {
|
||||
return entries
|
||||
}
|
||||
all := make([]keptEntry, len(entries))
|
||||
out := make([]os.DirEntry, len(entries))
|
||||
for i, e := range entries {
|
||||
all[i] = keptEntry{dir, e.Name(), e.Type()}
|
||||
out[i] = &all[i]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// unreadDirectory is what a folder lists as when reading it failed with
|
||||
// err: nothing when it is gone, else the last good listing, if there is
|
||||
// one.
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// A kept listing's entries sit in one array and answer as os.ReadDir's do.
|
||||
func TestKeptListingIsCompact(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Windows keeps os.ReadDir's entries, which carry their info")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
for _, n := range []string{"a.jsonl", "b.jsonl", "c.json"} {
|
||||
if err := os.WriteFile(filepath.Join(dir, n), []byte(n), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := os.Mkdir(filepath.Join(dir, "sub"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(filepath.Join(dir, "sub"), filepath.Join(dir, "link")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := time.Now().Add(-time.Minute)
|
||||
if err := os.Chtimes(dir, old, old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, want := readDirectory(dir), mustReadDir(t, dir)
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("%d entries, want %d", len(got), len(want))
|
||||
}
|
||||
for i, e := range got {
|
||||
w := want[i]
|
||||
if e.Name() != w.Name() || e.IsDir() != w.IsDir() || e.Type() != w.Type() {
|
||||
t.Errorf("entry %d = %s %v %v, want %s %v %v", i, e.Name(), e.IsDir(), e.Type(), w.Name(), w.IsDir(), w.Type())
|
||||
}
|
||||
gi, err1 := e.Info()
|
||||
wi, err2 := w.Info()
|
||||
if err1 != nil || err2 != nil || gi.Size() != wi.Size() || gi.Mode() != wi.Mode() || !gi.ModTime().Equal(wi.ModTime()) {
|
||||
t.Errorf("entry %s info = %v %v, want %v %v", e.Name(), gi, err1, wi, err2)
|
||||
}
|
||||
}
|
||||
first, ok := got[0].(*keptEntry)
|
||||
if !ok {
|
||||
t.Fatalf("kept entry is %T, not listed", got[0])
|
||||
}
|
||||
for i, e := range got {
|
||||
p, _ := e.(*keptEntry)
|
||||
if p == nil || uintptr(unsafe.Pointer(p))-uintptr(unsafe.Pointer(first)) != uintptr(i)*unsafe.Sizeof(keptEntry{}) {
|
||||
t.Fatalf("entry %d has an allocation of its own", i)
|
||||
}
|
||||
}
|
||||
if again := readDirectory(dir); &again[0] != &got[0] {
|
||||
t.Error("an unchanged folder was listed again")
|
||||
}
|
||||
}
|
||||
|
||||
func mustReadDir(t *testing.T, dir string) []os.DirEntry {
|
||||
t.Helper()
|
||||
es, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return es
|
||||
}
|
||||
Reference in New Issue
Block a user