Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

sessions: a folder's listing is a sorted slice, and one Codex folder isn't copied

yetoned4c6230964ff
diff --git a/internal/dirent/stats.go b/internal/dirent/stats.go
index a41451341..7bca64aeb 100644
--- a/internal/dirent/stats.go
+++ b/internal/dirent/stats.go
@@ -1,7 +1,9 @@
package dirent
import (
+ "cmp"
"io/fs"
+ "slices"
"time"
)
@@ -14,11 +16,36 @@ type Stat struct {
Mod time.Time
}
-// Stats is each entry of dir by name, as os.Lstat gives it, read in a few
-// calls for the whole folder where the system has them (getattrlistbulk on
-// macOS), rather than one per entry. ok is false where the system has none
-// or the folder couldn't be read, and the caller asks after each entry
-// itself.
-func Stats(dir string) (map[string]Stat, bool) {
- return stats(dir)
+// Entry is one entry of a Listing.
+type Entry struct {
+ Name string
+ Stat
+}
+
+// Listing is a folder's entries in name order. It is a slice, not a map:
+// every 15 s Sessions poll lists every Codex day folder and Claude Code
+// project again, and a map of each folder's entries was ~90 MB of the 2.3 GB
+// eight polls allocated.
+type Listing []Entry
+
+// Get is name's entry.
+func (l Listing) Get(name string) (Stat, bool) {
+ if i, ok := slices.BinarySearchFunc(l, name, func(e Entry, name string) int { return cmp.Compare(e.Name, name) }); ok {
+ return l[i].Stat, true
+ }
+ return Stat{}, false
+}
+
+// Stats is each entry of dir in name order, as os.Lstat gives it, read in
+// a few calls for the whole folder where the system has them
+// (getattrlistbulk on macOS), rather than one per entry. ok is false where
+// the system has none or the folder couldn't be read, and the caller asks
+// after each entry itself.
+func Stats(dir string) (Listing, bool) {
+ l, ok := stats(dir)
+ if !ok {
+ return nil, false
+ }
+ slices.SortFunc(l, func(a, b Entry) int { return cmp.Compare(a.Name, b.Name) })
+ return l, true
}
diff --git a/internal/dirent/stats_darwin.go b/internal/dirent/stats_darwin.go
index 9e64b8c24..4c1cac4d4 100644
--- a/internal/dirent/stats_darwin.go
+++ b/internal/dirent/stats_darwin.go
@@ -34,7 +34,7 @@ type attrList struct {
// 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,
// and this is one call for every few hundred.
-func stats(dir string) (map[string]Stat, bool) {
+func stats(dir string) (Listing, bool) {
fd, err := unix.Open(dir, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
if err != nil {
return nil, false
@@ -42,7 +42,7 @@ func stats(dir string) (map[string]Stat, bool) {
defer unix.Close(fd)
al := attrList{bitmapCount: attrBitMapCount, common: attrCmnReturned | attrCmnName | attrCmnObjType | attrCmnModTime, file: attrFileDataLen}
buf := make([]byte, 64<<10)
- out := map[string]Stat{}
+ var out Listing
for {
n, _, errno := syscall.Syscall6(unix.SYS_GETATTRLISTBULK, uintptr(fd), uintptr(unsafe.Pointer(&al)), uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)), 0, 0)
if errno == syscall.EINTR {
@@ -56,7 +56,7 @@ func stats(dir string) (map[string]Stat, bool) {
}
b := buf
for range int(n) {
- if !entry(b, out) {
+ if !entry(b, &out) {
return nil, false
}
b = b[binary.NativeEndian.Uint32(b):]
@@ -67,7 +67,7 @@ func stats(dir string) (map[string]Stat, bool) {
// entry reads one entry of getattrlistbulk's answer into out: its length,
// the attributes it returned, then each in the order of their bits, a
// file's data length after the common ones.
-func entry(b []byte, out map[string]Stat) bool {
+func entry(b []byte, out *Listing) bool {
if len(b) < 4 {
return false
}
@@ -126,6 +126,6 @@ func entry(b []byte, out map[string]Stat) bool {
if name == "" || common&attrCmnName == 0 || common&attrCmnObjType == 0 || common&attrCmnModTime == 0 || typ == vReg && file&attrFileDataLen == 0 {
return false
}
- out[name] = s
+ *out = append(*out, Entry{name, s})
return true
}
diff --git a/internal/dirent/stats_other.go b/internal/dirent/stats_other.go
index f825fb9a8..0a8c67d9d 100644
--- a/internal/dirent/stats_other.go
+++ b/internal/dirent/stats_other.go
@@ -2,4 +2,4 @@
package dirent
-func stats(string) (map[string]Stat, bool) { return nil, false }
+func stats(string) (Listing, bool) { return nil, false }
diff --git a/internal/dirent/stats_test.go b/internal/dirent/stats_test.go
index 0501b613e..aadee9592 100644
--- a/internal/dirent/stats_test.go
+++ b/internal/dirent/stats_test.go
@@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"runtime"
+ "slices"
"strings"
"testing"
"time"
@@ -47,6 +48,9 @@ func TestStatsAsLstat(t *testing.T) {
}
return
}
+ if !slices.IsSortedFunc(got, func(a, b Entry) int { return strings.Compare(a.Name, b.Name) }) {
+ t.Errorf("%s: Stats isn't in name order", dir)
+ }
if len(got) != want {
t.Errorf("%s: Stats has %d entries, want %d", dir, len(got), want)
}
@@ -59,7 +63,7 @@ func TestStatsAsLstat(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- s, ok := got[e.Name()]
+ s, ok := got.Get(e.Name())
if !ok {
t.Errorf("%q: not in Stats", e.Name())
continue
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 12b56e282..117952471 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -416,7 +416,7 @@ func stat(f *file) bool {
// is read in one listing where the system has one (dirent.Stats), and the
// rest are stat()ed on a few goroutines.
func statFiles(files []file, follow bool) []file {
- dirs := map[string]map[string]dirent.Stat{}
+ dirs := map[string]dirent.Listing{}
var order []string
for _, f := range files {
d := filepath.Dir(f.path)
@@ -425,7 +425,7 @@ func statFiles(files []file, follow bool) []file {
order = append(order, d)
}
}
- listed := make([]map[string]dirent.Stat, len(order))
+ listed := make([]dirent.Listing, len(order))
parallel(len(order), 8, func(i int) {
if s, ok := dirent.Stats(order[i]); ok {
listed[i] = s
@@ -438,7 +438,7 @@ func statFiles(files []file, follow bool) []file {
parallel(len(files), 256, func(i int) {
f := &files[i]
if in := dirs[filepath.Dir(f.path)]; in != nil {
- s, there := in[filepath.Base(f.path)]
+ s, there := in.Get(filepath.Base(f.path))
if !there || s.Mode != 0 && (s.Mode != fs.ModeSymlink || !follow) {
return
}
@@ -676,7 +676,13 @@ func forgetWarmListing() {
func listCodexFiles() []file {
var out []file
for _, dir := range CodexDirs() {
- out = append(out, codexFilesIn(dir)...)
+ // one folder's listing is the whole of it: copied, it was ~65 MB
+ // of what eight Sessions polls allocated
+ if out == nil {
+ out = codexFilesIn(dir)
+ } else {
+ out = append(out, codexFilesIn(dir)...)
+ }
}
return distinctPaths(out)
}
@@ -791,8 +797,8 @@ func listedRollouts(root string) (out []file, ok bool) {
if !ok {
return false
}
- for _, name := range slices.Sorted(maps.Keys(entries)) {
- e := entries[name]
+ for _, e := range entries {
+ name := e.Name
switch e.Mode {
case fs.ModeDir:
if !in(filepath.Join(real, name), filepath.Join(shown, name)) {