From 75f872ef19aea3230659761ad1580300319856d4 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 14 Nov 2026 17:30:50 +0800 Subject: [PATCH] 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). --- internal/gui/page_memory.go | 4 +++- internal/sessions/cache_idle.go | 20 ++++++++++++++-- internal/sessions/cache_idle_test.go | 36 ++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/internal/gui/page_memory.go b/internal/gui/page_memory.go index bd7a3ca1d..6d1c1ea8e 100644 --- a/internal/gui/page_memory.go +++ b/internal/gui/page_memory.go @@ -5,6 +5,7 @@ import ( "sync" "time" + "github.com/yetone/magpie/internal/sessions" "github.com/yetone/magpie/internal/usage" ) @@ -129,7 +130,8 @@ func forgetAwayPage() { return } stats := letGoStats() - if usage.ForgetPages() || stats { + parses := stats && sessions.ForgetParses() + if usage.ForgetPages() || stats || parses { usage.ReleaseScratch() debug.FreeOSMemory() } diff --git a/internal/sessions/cache_idle.go b/internal/sessions/cache_idle.go index 486382415..86717eeeb 100644 --- a/internal/sessions/cache_idle.go +++ b/internal/sessions/cache_idle.go @@ -62,14 +62,30 @@ func cacheIdleCheck() { // 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 { +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 time.Since(time.Unix(0, cacheKept.last.Load())) < cacheIdle { + if idle > 0 && time.Since(time.Unix(0, cacheKept.last.Load())) < idle { return false // read while the save was written } saving.Lock() diff --git a/internal/sessions/cache_idle_test.go b/internal/sessions/cache_idle_test.go index d38c52ccb..d7db5f0f9 100644 --- a/internal/sessions/cache_idle_test.go +++ b/internal/sessions/cache_idle_test.go @@ -76,3 +76,39 @@ func TestIdleParsesLetGoAndComeBackFromDisk(t *testing.T) { } } } + +// A page gone from view lets the parses go at once (ForgetParses), read +// just now or not, and the next read has them back from sessions.json; a +// save not on disk yet still keeps them. +func TestForgetParsesLetsGoAtOnce(t *testing.T) { + setup(t) + before := List(0) + if len(before) == 0 { + t.Fatal("no sessions listed") + } + saved() + cacheKept.last.Store(time.Now().UnixNano()) + + saving.Lock() + saving.failed = true + saving.Unlock() + if ForgetParses() || !loaded { + t.Fatal("the parses were let go while the last save had failed") + } + saving.Lock() + saving.failed = false + saving.Unlock() + + if !ForgetParses() || loaded || len(cache) != 0 { + t.Fatalf("parses read just now kept: loaded %v, %d in memory", loaded, len(cache)) + } + if ForgetParses() { + t.Fatal("ForgetParses said it let go with nothing loaded") + } + var parses atomic.Int32 + parseHook = func() { parses.Add(1) } + t.Cleanup(func() { parseHook = nil }) + if after := List(0); len(after) != len(before) || parses.Load() != 0 { + t.Fatalf("%d sessions after (%d before), %d files read again", len(after), len(before), parses.Load()) + } +}