Files
magpie/internal/appdir/homes_alloc_test.go
T
yetone 390341f9ed appdir: the homes look tells folders apart by inode, not EvalSymlinks (#2278)
ClaudeHomesIn and CodexHomesIn run on every Sessions poll, and keyed
each folder they found by filepath.EvalSymlinks, which Lstats every part
of the name: ~3 MB of garbage per 40 polls on a real home. On unix a
folder is now keyed by its device and inode, which a link to it shares;
a folder that can't be looked at is keyed by its name, and other systems
keep EvalSymlinks. The folder checks use DirStat, moved here from
internal/sessions so both share one.

Run (mica, at this tree): go vet for darwin and windows of appdir,
sessions, provider and gui, for linux of the first three (gui needs cgo
there); GOOS=freebsd go build; go test ./internal/appdir ./internal/sessions
./internal/provider ok. TestHomesLookAllocatesLittle: 55 and 68
allocations a look now, 387 and 400 on the old code, which fails it.

Real account (mote, 40 polls of /api/sessions): w9 31.28 MB allocated
with os.lstatNolog 3 MB; w10 25.98 MB, lstat gone from the top list.
CPU over 30s 1.47s, within noise of w8's 1.29s at mote's load.
2026-11-12 05:44:12 +08:00

45 lines
1.6 KiB
Go

package appdir
import (
"os"
"path/filepath"
"runtime"
"slices"
"testing"
)
// A Sessions poll looks for the Claude and Codex folders every time, and
// the look told one folder from another through EvalSymlinks, which
// allocated for each part of each name (#2278). A link to a folder is
// still the folder, in a home with more of each.
func TestHomesLookAllocatesLittle(t *testing.T) {
t.Setenv("CLAUDE_CONFIG_DIR", "")
t.Setenv("CODEX_HOME", "")
home := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "cfg"))
for _, p := range []string{".claude/projects", ".claude-a/projects", ".claude-b/projects", ".codex/sessions", ".codex-a/sessions", ".codex-b/sessions"} {
if err := os.MkdirAll(filepath.Join(home, p), 0o755); err != nil {
t.Fatal(err)
}
}
if err := os.Symlink(filepath.Join(home, ".claude-a"), filepath.Join(home, ".claude-link")); err != nil {
t.Skip(err)
}
os.Symlink(filepath.Join(home, ".codex"), filepath.Join(home, ".codex-link"))
if got := ClaudeHomesIn(home); len(got) != 3 || slices.Contains(got, filepath.Join(home, ".claude-link")) {
t.Fatalf("ClaudeHomesIn = %v", got)
}
if got := CodexHomesIn(home); len(got) != 3 || slices.Contains(got, filepath.Join(home, ".codex-link")) {
t.Fatalf("CodexHomesIn = %v", got)
}
if runtime.GOOS == "windows" {
return
}
claude := testing.AllocsPerRun(20, func() { ClaudeHomesIn(home) })
codex := testing.AllocsPerRun(20, func() { CodexHomesIn(home) })
t.Logf("allocations: claude %.0f, codex %.0f", claude, codex)
if claude > 120 || codex > 120 {
t.Fatalf("a look allocated %.0f (claude) and %.0f (codex) times", claude, codex)
}
}