Files
magpie/internal/sessions/cache_idle.go
T
yetone 75f872ef19 sessions: a page gone from view lets the parsed session files go in 30 s, not 3 minutes
v0.1.4150's away timer let the Sessions stats and the Requests chunks go
30 s after the app's page went from view, but a web page open 70 s and
closed still held 92-93 MB 45 and 90 s on. A heap taken 45 s after the
close (liveHeap 57 MB, 15 MB at +180 s) showed why: opening the page
starts the warm-up (warmStart), which parses every session file, and
those parses (decodeCache, the listings, Codex identities: ~40 MB live
for the acctp history) go only by the sessions package's own cacheIdle,
3 minutes after their last read.

forgetAwayPage now also calls sessions.ForgetParses, which is
dropIdleCache without the 3-minute wait (dropCache(0)): it still keeps
the parses while a save isn't on disk, the last failed, or a file is
being parsed, and the next read has them back from sessions.json with
no session file read. It runs only when the stats went too, so a range
being read keeps them.

Test: TestForgetParsesLetsGoAtOnce (red with ForgetParses waiting
cacheIdle, broken by hand).
Measured on mote (pageclose2.sh, acctp, page open 70 s and closed,
footprint-overlay build like the release):
  v0.1.4150 (w90): close 122 MB, +45 s 93, +90 s 90, +180 s 55, +600 s 52
  this (w92):      close 125 MB, +45 s 64, +90 s 61, +180 s 56, +600 s 53
A debug build (w91) logged the away timer firing with no read since,
so the 90 MB left was this cache, not a skipped let-go.

Run: go test ./internal/sessions ./internal/gui ./internal/usage on mote
(ok); GOOS=linux go vet -tags nogui and GOOS=windows go vet of the two
packages (ok).
2026-11-14 17:45:41 +08:00

114 lines
3.6 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 { return dropCache(cacheIdle) }
// ForgetParses lets the parses go now, not cacheIdle after the last read:
// the app's page has gone from view and asked nothing since (the gui's
// pageAwayFor). What a page opened on the app's start had the warm-up
// parse (~40 MB live for a real history) otherwise stayed three minutes
// after it closed. It says whether they went, or there were none.
func ForgetParses() bool {
mu.Lock()
had := loaded
mu.Unlock()
return had && dropCache(0)
}
// dropCache is dropIdleCache with the time nothing must have read the
// parses for: idle, or none at all.
func dropCache(idle time.Duration) bool {
saved() // what the next read loads is what is in memory now
mu.Lock()
defer mu.Unlock()
if !loaded {
return true
}
if idle > 0 && time.Since(time.Unix(0, cacheKept.last.Load())) < idle {
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()
forgetListsKept() // each lister's last list (~5 MB)
ccListed.Clear() // every Claude Code file's path and key (~2 MB)
dirent.Forget()
cacheDrops.Add(1)
return true
}