Every 15 s Sessions poll built every Claude Code file's and Codex
rollout's list again, and merged and filtered them into more new
lists, while nothing on disk had changed. On the owner's real history
that was ~120 MB of garbage per 40 polls in listedRollouts,
claudeFiles, listCCFiles and filesKept.
Each lister now keeps its last list (listsKept). A listing walks the
folders as before and compares each file with the kept list's file at
the same place; while they match nothing is allocated, and the kept
list itself is returned. On the first difference it copies the matched
part and goes on, so a grown, new or gone file is listed as a fresh
listing has it, and the kept list is never written. listCCFiles reuses
its walk's scratch slice per lister. distinctPaths and
distinctCodexFiles return their input when nothing is dropped and never
filter in place; codexFilesIn's twin filter clones before deleting.
rolloutsSeen's day entry is its part of the kept list (clipped), not a
clone. The idle cache drop forgets all of it.
Measured on mote with the owner's real account (75 GB history),
40 /api/sessions polls, `magpie heap` before and after, alloc_space
diff:
before (w6): 155.7 MB; listedRollouts 42.8, claudeFiles 32.2 (71.1
cum), listCCFiles 31.0, filesKept.distinct 18.0
after (w7): 34.8 MB; none of the listers in the top; distinct 4.5,
provider.All 9.7 (next)
CPU over 30 s of polling: 1.79 s -> 1.37 s.
Tests: lists_kept_test.go, red on the old code (an unchanged listing
made a list of its own; distinctPaths wrote its input), green now.
TestClaudeFilesMadeAtTheirSize expects the clipped kept list.
Run on mica at this tree (rebased on 0cff3429f, sessions vet + test again ok): go vet ./internal/sessions for darwin, linux
and windows, ./internal/gui for darwin; go test ./internal/sessions ok
(64 s); go test ./internal/gui ok but TestPanelHiddenByWailsMarksPageOutOfSight
(#6203, flaky on mica, passes alone). -race of sessions: #4360 and
#6213, both red on the old code too, no stack in the changed code.
Not tried: Windows and Linux runs of the new test (the codex subtest is
darwin-only).
98 lines
3.0 KiB
Go
98 lines
3.0 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()
|
|
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
|
|
}
|