Commit Graph
5 Commits
Author SHA1 Message Date
yetone 195d79939e dirent: a listing's entry keeps its time as nanoseconds, 40 bytes not 56
Stat.Mod was a time.Time (24 bytes), so an Entry was 56 bytes. The
listings kept for a long history hold up to ~130k entries (keptEntries),
and on the owner's real cache they were the idle heap's 7.7 MB under
dirent.entry. Mod is now unix nanoseconds (int64) with ModTime() for the
three callers that want a time; getattrlistbulk's seconds and
nanoseconds are summed as time.Unix does. An Entry is now 40 bytes.

Run on mote at this head:
- go test ./internal/sessions ./internal/dirent: ok
- GOOS=darwin/linux/windows go vet ./internal/dirent ./internal/sessions:
  ok
- gofmt -l: clean
- Broken by hand: a 16-byte pad in Stat makes TestEntrySize red
  ("an Entry is 56 bytes, want 40").
- Real-account copy (~/r2278/acct), idle heap at 180s: dirent.Stats
  7.68 MB -> 4.20 MB (dirent.entry flat 5.68 -> 1.70 MB); Footprint
  148 MB.
2026-11-09 20:19:28 +08:00
yetone eadeb89c48 sessions, dirent: a poll's listings are kept across collections (#2278)
A full history's page polls listed the same folders every few seconds
into a fresh slice, map and set each time: the sync.Pools meant to keep
them were emptied by the collector, which runs about twice a second at
this size, before the next poll took them.

- sessions: listingBufs and pathSets are shelves (shelf.go), which keep
  up to four across collections. listCCFiles makes its slice with the
  room of the folder's last listing.
- dirent: Stats keeps each folder's last listing (Listing.Done(dir)) and
  the room of the one before, up to 1<<17 entries in all. A name already
  in the folder's last listing is taken from it, not copied again. 1<<16
  kept too little of ~23k session files and their folders: the first
  bench of this change (budget 1<<16) took dirent.entry from 67-102 to
  80-100 MB a minute, no gain.

Bench, mote, the real full history (52G Codex in 12,495 files, 13G
Claude Code in 10,186), fullallogh.sh, alloc_space between the 50s and
110s heap profiles, MB per minute, before (v0.1.2132 equivalent) -> after:
  distinctOf      59      -> 22
  listCCFiles     42      -> 17
  dirent.entry    67-102  -> 55
  dirent.nameOf   24      -> 5
Totals swing 400-1200 MB with mote's load (other sessions), so these are
per function.

Run at this head (rebased on 3bebc3839), on mote:
- go build ./... ok
- go test -race -count=1 ./internal/dirent ok, ./internal/sessions ok
  (109.7s; one earlier run had TestLongReadKeepsWhatItRead red with a
  data race in testenv.Zone, red on 3bebc3839 too, 1 of 80 at -count=40
  -cpu 1,4: #4360)
- ./internal/gui: TestMainWindowCorners red, #3949 (mote is macOS 27);
  gui is not touched here
- GOOS=linux, windows, darwin go vet -tags nogui ./internal/dirent
  ./internal/sessions ok; gofmt -l clean

Broken by hand:
- TestListingKeptAcrossCollections (dirent) and
  TestListingPoolsKeptAcrossCollections (sessions) are red on the
  sync.Pool code: "a listing given back was let go by a garbage
  collection".
- name reuse turned off: "a name listed before was copied again".

Not tried: the Windows and Linux listers (stats_other.go returns no
listing there, as before).
2026-10-30 12:25:23 +08:00
yetone 7fd65aad56 sessions: a Codex listing reuses the last one's names, and dirent its room (#2278)
Each Codex listing (several a minute of the page's polls over a long
history) made every rollout's path and key again, grew a new
dirent.Listing an entry at a time for every day folder, and gave each
rollout name a slice of its own in distinctCodexFiles: ~230 MB of a
minute of polls, from 20k unchanged rollouts.

- listedRollouts keeps each day folder's rollouts as its last listing
  found them; a rollout listed before keeps its path and key (made
  only from its folder and name) and takes its size and time now.
  A folder is kept again only when a rollout came or went.
- dirent.Stats takes its listing from a pool and its callers
  (listedRollouts, statFiles) give it back with Done once read; Done
  clears it. getattrlistbulk's 64 KB buffer is pooled too.
- distinctCodexFiles keeps a name's first file in a map of ints, and
  a slice only for names with more files.

New tests: TestListedRolloutsKeepTheirNames (a grown, a removed and a
new rollout; reused strings) and TestStatsAfterDone.

Run on mote. Written on 7037cd980 (v0.1.2126), rebased on 6db21d869
(no sessions or dirent change between); at the pushed head again:
go test -race ./internal/dirent/ ./internal/sessions/ ok, and the
three vets below clean.
At the tree before the rebase:
- go vet -tags nogui ./internal/sessions/ ./internal/dirent/ for
  linux, windows and darwin: clean.
- go test -race ./internal/dirent/ ./internal/sessions/: ok, at this
  tree. Before the dirent pool was added: also ./internal/usage/ ok
  and ./internal/gui/ ok but TestMainWindowCorners (#3949, known).
- Broken by hand: listedRollouts not reading its last listing turns
  TestListedRolloutsKeepTheirNames red ("had its path and key made
  again"); Done not clearing turns TestStatsAfterDone red.
- Full-history bench (10 endpoints every 5s, heap profiles at 50s and
  110s, alloc_space diff), two rounds each, v0.1.2126 against this:
  listCodexFiles 229 / 238 MB -> 72 / 92 MB;
  dirent.Stats 115 / 130 MB -> 62 / 72 MB;
  distinctCodexFiles 40 / 51 MB -> 7 / 12 MB;
  readRequestState 318 / 390 MB -> 225 / 222 MB.
  The totals (1186 / 694 against 477 / 960 MB) swing with mote's load
  more than the change moves them.

Not changed: dirent's names are still copied for each entry (~30 MB),
and Linux and Windows list without dirent.Stats (walkLinked, statFiles'
lstat), so only the distinctCodexFiles part reaches them.
2026-10-30 11:17:19 +08:00
yetone d4c6230964 sessions: a folder's listing is a sorted slice, and one Codex folder isn't copied
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.
2026-10-27 09:26:09 +08:00
yetone 7b2a330a56 sessions: each session folder is read in one listing on macOS, not a stat() a file
Every 15 s Sessions poll (and every Requests page) lists every agent's
session files and stat()s each one: on a real history of ~21.7k files
in 462 folders that was ~21.7k lstat()/stat() calls, ~10 µs each on
APFS, and the largest part of the poll's CPU. Codex's day folders were
also read twice, once by WalkDir and once more for the stat()s.

dirent.Stats reads a folder's names, kinds, sizes and modification
times with getattrlistbulk, a few calls for the whole folder. statFiles
looks each file up in its folder's listing and stat()s alone only a link
it follows (ccFiles) or a folder that couldn't be listed so. Codex's
rollouts are walked with the listings themselves (listedRollouts), in
WalkDir's order, falling back to walkLinked and statFiles when a folder
can't be read so. Other systems keep the stat()s as they were.

Measured on the real history (~/.claude/projects and ~/.codex
sessions, read only):
- Stats against os.ReadDir + os.Lstat over all 21,672 entries in 462
  folders: 0 mismatched kinds, sizes or times; 43 ms against 257 ms.
- `magpie serve` on the real history (sandbox HOME, its projects a link
  to the real ones), /api/sessions asked back to back with
  X-Magpie-Had for a 20 s CPU profile: before, 177 ms a poll, process
  CPU 48.5 s for the run, 17.5 s of samples in statFiles; after, 157 ms,
  34.3 s, and no per-file stat() in the profile beyond links.

Tests:
- TestStatsAsLstat: a file's size and time to the ns, a folder, links
  to a file, a folder and nothing, a unicode name, and 3000 long names
  (more than one call's 64 KB answer), each as os.Lstat says.
- TestStatFilesAsStat: follow and not, the same files kept with the same
  sizes and times as os.Stat/os.Lstat; on macOS only the two links are
  stat()ed alone. Red (7 alone, want 0) with Stats turned off by hand.
- TestListedRolloutsAsWalked: nested days, a folder and a link named as
  rollouts, a .zst, a non-rollout, and a sessions folder that is a link
  (#2233): the same rollouts, order, sizes and times as walkLinked plus
  statFiles. Red with the listing's sort taken out by hand.
- TestCodexFilesStatInOrder (existing) passes on the new path.

Run at this head:
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on
  internal/sessions and internal/dirent ok.
- go test -tags nogui -count=1 -timeout 45m ./internal/dirent/
  ./internal/sessions/ ./internal/usage/ ./internal/gui/: ok (2.4 s,
  13.9 s, 15.9 s, 51.1 s).
- go test -tags nogui -count=1 -timeout 45m at fe903798f, the packages that
  import sessions: agent 293s, cli 57s, davsync 73s, gateway 493s,
  library 192s, otlp 19s, tui 31s, tune 7s, all ok

Not tried: Windows and Linux run the stat()s as before, so no change
there; an SMB or other network volume on macOS, where getattrlistbulk
fails and the per-file path is taken.
2026-10-27 06:40:30 +08:00