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).
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
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
|
|
}
|