Files
magpie/internal/sessions/files_kept.go
T
yetone 10c3315312 sessions: an unchanged listing is the last one, not a new list (#2278)
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).
2026-11-12 04:52:19 +08:00

183 lines
4.5 KiB
Go

package sessions
import (
"slices"
"sync"
"unsafe"
"weak"
)
// filesKept is the last list of every agent's session files a pass made,
// held weakly. A Sessions poll lists ~20k files, and while none of them
// changed it made the same list again, ~1.5 MB a poll (the largest share
// of what a poll allocates, and so of its garbage collection). A pass
// that lists the files as the last one did gives that list back instead,
// and allocates nothing for it. The list is held only as long as
// something else holds it or no collection has run since: a magpie with
// the page shut keeps none of it.
type filesKept struct {
sync.Mutex
first weak.Pointer[file]
n int
}
// allKept are allFiles' and allFilesForTitles' last lists.
var allKept [2]filesKept
func (k *filesKept) last() []file {
k.Lock()
defer k.Unlock()
if k.n == 0 {
return nil
}
p := k.first.Value()
if p == nil {
return nil
}
return unsafe.Slice(p, k.n)
}
// distinct is distinctOf(lists...), given as the last list it made when
// it is the same, file for file. Its readers only read it (heldFiles
// gives one list to every reader under a hold already), and it is
// clipped, so an append to it is made elsewhere.
func (k *filesKept) distinct(lists ...[]file) []file {
last := k.last()
seen := pathSet()
defer putPathSet(seen)
var out []file // nil while each file is last's at its place
i := 0
for _, fs := range lists {
for _, f := range fs {
if _, ok := seen[f.path]; ok {
continue
}
seen[f.path] = struct{}{}
if out == nil {
if i < len(last) && last[i] == f {
i++
continue
}
n := 0
for _, fs := range lists {
n += len(fs)
}
out = append(make([]file, 0, n), last[:i]...)
}
out = append(out, f)
}
}
if out == nil {
if i == len(last) && i > 0 {
return last
}
// files gone from its end; never nil, which callers read as "not
// listed" and keep every parse of files that are gone for
out = append(make([]file, 0, i), last[:i]...)
}
out = out[:len(out):len(out)]
k.Lock()
k.first, k.n = weak.Pointer[file]{}, len(out)
if len(out) > 0 {
k.first = weak.Make(&out[0])
}
k.Unlock()
return out
}
// sameFiles builds a lister's list a file at a time against the list it
// made last, and allocates nothing while each file is the last list's at
// its place. Every 15 s Sessions poll listed every rollout and Claude Code
// file again into lists made new, ~2.5 MB a poll on a real history while
// nothing in them had changed, and that garbage was a third of a poll's
// collections (#2278). Its lists are only read (heldFiles' contract), and
// clipped, so an append to one is made elsewhere.
type sameFiles struct {
last, out []file
i int
made bool
hint int // the least room out is made with
}
func (s *sameFiles) add(f file) {
if !s.made {
if s.i < len(s.last) && s.last[s.i] == f {
s.i++
return
}
s.made = true
s.out = append(make([]file, 0, max(len(s.last)+len(s.last)/16, s.hint)), s.last[:s.i]...)
}
s.out = append(s.out, f)
}
// n is how many files were added.
func (s *sameFiles) n() int {
if s.made {
return len(s.out)
}
return s.i
}
// list is the files added, the last list itself when they are all of it,
// and whether it is another list than the last.
func (s *sameFiles) list() ([]file, bool) {
switch {
case s.made:
return slices.Clip(s.out), true
case s.i == len(s.last):
return s.last, false
}
// files gone from its end
return append(make([]file, 0, s.i), s.last[:s.i]...), true
}
// listsKept are the listers' last lists, by lister and folder. Unlike
// filesKept's they are held strongly: nothing else holds a lister's list
// from one poll to the next. forgetListsKept lets them go when the cache
// does.
var listsKept sync.Map // key → []file
func keptFiles(key string) []file {
l, _ := listsKept.Load(key)
fs, _ := l.([]file)
return fs
}
// sameAsKept is s's list, kept under key for the next pass when it is new.
func sameAsKept(key string, s *sameFiles) []file {
l, changed := s.list()
if changed {
listsKept.Store(key, l)
}
return l
}
// concatKept is lists one after another: the one list itself when the
// others are empty, else the last such list when it is the same.
func concatKept(key string, lists ...[]file) []file {
var one []file
n := 0
for _, l := range lists {
if len(l) > 0 {
n++
one = l
}
}
if n <= 1 {
return one
}
s := sameFiles{last: keptFiles(key)}
for _, l := range lists {
for _, f := range l {
s.add(f)
}
}
return sameAsKept(key, &s)
}
func forgetListsKept() {
listsKept.Clear()
ccScratch.Clear()
}