When nobody has read the session parses for three minutes they are let go (v0.1.3540), but the end heap of a real history still held ~10 MB made from them or from the folders, kept for the life of the process: - Codex's thread identities (codexIdentityMemo, ~3.5 MB); - each Codex day folder's rollouts as last listed (rolloutsSeen, ~1.5 MB); - every folder's listing given back to dirent (~4 MB of names and entries). dropIdleCache now lets them go too, and dirent.Forget empties the kept listings. Each is made again on the next read, from the parses and the folders, with no session file parsed. CC's per-file UUIDs (ccUUIDMemo) stay: letting them go would read every Claude Code file again. Run: - mote (macOS): go test ./internal/sessions ./internal/dirent ./internal/gui ./internal/usage ok; GOOS=linux and windows go vet ok. - Broken by hand (the three drops and Forget's body removed): red, "idle: 1 Codex identity lists and 2 folders' rollouts kept" and "9 kept after Forget". On Linux dirent.Stats lists nothing, so the test asks for rollouts kept before the drop only on macOS. - Real accounts (a copy of the owner's history on mote, 300 s each, the web app opened cold, order t53 t54 t54 t53): idle footprint 117 / 120 MB before, 103 / 85 MB after; end heap 53.3 / 50.9 MB before, 43.4 / 44.6 MB after. Peak unchanged (it is the start). Not tried: Windows and Linux at runtime (the change is the same code).
96 lines
2.9 KiB
Go
96 lines
2.9 KiB
Go
package sessions
|
|
|
|
import (
|
|
"runtime/debug"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/dirent"
|
|
)
|
|
|
|
// cacheIdle is how long the parses of every session file stay in memory
|
|
// after the last read of them. They are kept on disk too (sessions.json,
|
|
// written saveGap after a change), and they were the largest thing magpie
|
|
// held while nobody looked: ~14.5 MB of a 58 MB idle heap for a real
|
|
// history of 24.6 MB on disk, kept for the life of the process after one
|
|
// visit to the Sessions or Usage page. Read again, they come back from
|
|
// the file, with no session file read.
|
|
const cacheIdle = 3 * time.Minute
|
|
|
|
var cacheKept struct {
|
|
last atomic.Int64
|
|
armed atomic.Bool
|
|
off bool
|
|
}
|
|
|
|
func init() { cacheKept.off = testing.Testing() }
|
|
|
|
// cacheDrops counts the times the parses were let go, for the tests.
|
|
var cacheDrops atomic.Int64
|
|
|
|
// cacheUsed notes a read of the parses and arms the idle check. mu is held.
|
|
func cacheUsed() {
|
|
if cacheKept.off {
|
|
return
|
|
}
|
|
cacheKept.last.Store(time.Now().UnixNano())
|
|
if cacheKept.armed.CompareAndSwap(false, true) {
|
|
time.AfterFunc(cacheIdle, cacheIdleCheck)
|
|
}
|
|
}
|
|
|
|
// cacheIdleCheck lets the parses go once nothing has read them for
|
|
// cacheIdle, or looks again when that will be.
|
|
func cacheIdleCheck() {
|
|
if left := cacheIdle - time.Since(time.Unix(0, cacheKept.last.Load())); left > 0 {
|
|
time.AfterFunc(left, cacheIdleCheck)
|
|
return
|
|
}
|
|
at := time.Now().UnixNano()
|
|
if !dropIdleCache() {
|
|
time.AfterFunc(cacheIdle, cacheIdleCheck)
|
|
return
|
|
}
|
|
cacheKept.armed.Store(false)
|
|
if cacheKept.last.Load() > at && cacheKept.armed.CompareAndSwap(false, true) {
|
|
time.AfterFunc(cacheIdle, cacheIdleCheck) // read again meanwhile
|
|
}
|
|
debug.FreeOSMemory()
|
|
}
|
|
|
|
// dropIdleCache lets go of the parses in memory when nothing is reading
|
|
// or about to change them, once what was parsed since the last save is
|
|
// on disk. It says whether it did, or there was nothing to let go.
|
|
func dropIdleCache() bool {
|
|
saved() // what the next read loads is what is in memory now
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if !loaded {
|
|
return true
|
|
}
|
|
if time.Since(time.Unix(0, cacheKept.last.Load())) < cacheIdle {
|
|
return false // read while the save was written
|
|
}
|
|
saving.Lock()
|
|
pending := saving.running || saving.next != nil || saving.failed
|
|
saving.Unlock()
|
|
if pending || len(parsing) > 0 || len(overlaid) > 0 || len(behind) > 0 {
|
|
return false
|
|
}
|
|
cache, loaded = map[string]*state{}, false
|
|
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
|
|
}
|