diff --git a/internal/dirent/stats.go b/internal/dirent/stats.go index ed7b207e8..bee81334b 100644 --- a/internal/dirent/stats.go +++ b/internal/dirent/stats.go @@ -172,3 +172,13 @@ func (l Listing) Done(dir string) { listings.last[dir] = l listings.entries += cap(l) } + +// Forget lets go of every listing kept for the next Stats, and the room +// kept for them: once the session folders go unread, the listings and +// their names were held for the life of the process (~4 MB for a real +// history). A listing given back later is kept again. +func Forget() { + listings.Lock() + listings.last, listings.spare, listings.room, listings.entries = nil, nil, nil, 0 + listings.Unlock() +} diff --git a/internal/dirent/stats_test.go b/internal/dirent/stats_test.go index eea5eac91..e6b6de9c4 100644 --- a/internal/dirent/stats_test.go +++ b/internal/dirent/stats_test.go @@ -223,3 +223,34 @@ func TestListingsOfDifferentSizesMadeOnce(t *testing.T) { t.Errorf("ten polls of unchanged folders allocated %d bytes, want next to none", got) } } + +// Forget lets go of every listing given back, and Stats still answers +// what the folder has after it. +func TestForgetLetsListingsGo(t *testing.T) { + dir := t.TempDir() + for i := range 5 { + if err := os.WriteFile(filepath.Join(dir, fmt.Sprint(i)), nil, 0o644); err != nil { + t.Fatal(err) + } + } + l, ok := Stats(dir) + if !ok { + t.Skip("Stats reads nothing here") + } + l.Done(dir) + kept := func() int { + listings.Lock() + defer listings.Unlock() + return len(listings.last) + len(listings.spare) + len(listings.room) + listings.entries + } + if kept() == 0 { + t.Fatal("nothing kept of a listing given back") + } + Forget() + if n := kept(); n != 0 { + t.Fatalf("%d kept after Forget", n) + } + if l, _ := Stats(dir); len(l) != 5 { + t.Fatalf("Stats after Forget has %d entries, want 5", len(l)) + } +} diff --git a/internal/sessions/cache_idle.go b/internal/sessions/cache_idle.go index 473652fce..2e4c147e4 100644 --- a/internal/sessions/cache_idle.go +++ b/internal/sessions/cache_idle.go @@ -5,6 +5,8 @@ import ( "sync/atomic" "testing" "time" + + "github.com/yetone/magpie/internal/dirent" ) // cacheIdle is how long the parses of every session file stay in memory @@ -80,6 +82,14 @@ func dropIdleCache() bool { cacheGeneration++ // a refresh still holding the old generation drops its parses forgetKeptStats() forgetKeptList() + // what is made again from the parses or the folders on the next read: + // Codex's thread identities (~3.5 MB), and each Codex day folder's + // rollouts and every folder's listing as last read (~5.5 MB) + codexIdentityMemo.Lock() + codexIdentityMemo.kept = nil + codexIdentityMemo.Unlock() + rolloutsSeen.Clear() + dirent.Forget() cacheDrops.Add(1) return true } diff --git a/internal/sessions/cache_idle_test.go b/internal/sessions/cache_idle_test.go index 18f48cd08..7b9664abc 100644 --- a/internal/sessions/cache_idle_test.go +++ b/internal/sessions/cache_idle_test.go @@ -1,6 +1,7 @@ package sessions import ( + "runtime" "sync/atomic" "testing" "time" @@ -34,11 +35,26 @@ func TestIdleParsesLetGoAndComeBackFromDisk(t *testing.T) { t.Fatal("the parses were let go though read just now") } + memos := func() (identities, rollouts int) { + codexIdentityMemo.Lock() + identities = len(codexIdentityMemo.kept) + codexIdentityMemo.Unlock() + rolloutsSeen.Range(func(any, any) bool { rollouts++; return true }) + return + } + // folders are listed so only on macOS (dirent.Stats) + if i, r := memos(); i == 0 || r == 0 && runtime.GOOS == "darwin" { + t.Fatalf("before: %d Codex identity lists and %d folders' rollouts kept; the test reads none", i, r) + } + idle() drops := cacheDrops.Load() if !dropIdleCache() || loaded || len(cache) != 0 || cacheDrops.Load() != drops+1 { t.Fatalf("idle parses kept: loaded %v, %d in memory", loaded, len(cache)) } + if i, r := memos(); i != 0 || r != 0 { + t.Fatalf("idle: %d Codex identity lists and %d folders' rollouts kept", i, r) + } var parses atomic.Int32 parseHook = func() { parses.Add(1) }