dirent.Stats returned a map per folder. Eight Sessions-page polls on the
owner's real ~/.codex and ~/.claude (copied into a sandbox HOME) spent
~90 MB of their 2.3 GB on those maps alone. It now returns a Listing
sorted by name, found with a binary search (Listing.Get), and
listedRollouts walks it in order instead of sorting the map's keys.
listCodexFiles took the one folder most users have into a fresh slice;
that copy was ~65 MB of the same polls, and the folder's own slice is
now kept.
Run at this head: go test ./internal/dirent ./internal/sessions
./internal/usage ./internal/usageapi ./internal/gui ok; gofmt clean;
GOOS=linux and GOOS=windows go vet -tags nogui on dirent, sessions,
usage and gui clean. stats_test checks the listing is in name order.
A/B on real session files (8 polls of the Sessions trio, 15 s apart,
v0.1.1661 vs this with the next commit): listCodexFiles 129 -> 89 MB,
listedRollouts 87 -> 76 MB, dirent.stats 44 -> 36 MB.
Not tried: the Linux and Windows readdir paths beyond go vet; they
build the same Listing through stats_other.go.
Rebased on 65ee8c65c (gateway and plugin fake only) and run again
there with the next commit: dirent, sessions and gui ok.
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package dirent
|
|
|
|
import (
|
|
"cmp"
|
|
"io/fs"
|
|
"slices"
|
|
"time"
|
|
)
|
|
|
|
// Stat is what a folder's listing says of one entry: its kind (Mode's type
|
|
// bits, 0 for a regular file), and a regular file's size and modification
|
|
// time, as os.Lstat gives them.
|
|
type Stat struct {
|
|
Mode fs.FileMode
|
|
Size int64
|
|
Mod time.Time
|
|
}
|
|
|
|
// 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
|
|
}
|