sessions: the index is written at most once a minute, not after every poll

A Sessions or Usage page polls every 15s, and a live session changes its
file between any two polls, so each poll's refresh saved the whole index
again: tens of MB of JSON for a long history, and encodeCache allocated
~20-30 MB a write (170 MB over 8 polls). The index only spares the next
start a read, and a parse a save hasn't written yet is read again then,
so a save now waits saveGap (1 minute) after the last one written; saves
in a row are still folded into the latest. Saved (a CLI or the app
ending) sets hurry, so every save queued is written at once without the
gap, and an unused wake-up is drained when the saves end so it cuts no
later gap short. The package's tests write saves as they come
(saveGap = 0 in TestMain).

Run:
- TestSaveWaitsOutTheGap (new): a second and third save right after the
  first are not written while the gap stands, and Saved writes them, as
  one, at once. Red with the wait removed ("2 writes with saveGap not
  yet past") and red with Saved not hurrying ("Saved waited out the
  gap", 10s).
- go test -timeout 45m ./internal/sessions ./internal/gui ./internal/usage
  ./internal/usageapi ./internal/cli ./internal/tui: ok at this change on
  205e3b8ae. gofmt clean; GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/sessions ok.
- A/B, 8 sequential polls of the Sessions and Usage pages under a
  sandbox HOME with the real ~/.codex and ~/.claude sessions (live, so
  totals vary run to run): writeCache 169.5 -> 95.9 MB, encodeCache
  163.7 -> 92.5 MB; the run spans a few minutes, so a write a minute
  still lands in it. At the GUI's 15s polls that is one write a minute
  instead of four.
Not tried: quitting the real app and checking the index on disk; the
CLI's exit path calls Saved, as before.

Rebased on 2badacaad (provider and plugin changes): go test -count=1
./internal/sessions ./internal/gui ok at 26ed62752 (15.6s, 162.0s).
This commit is contained in:
yetone
2026-10-27 10:21:10 +08:00
parent 2badacaad8
commit a87f051974
3 changed files with 91 additions and 3 deletions
+3
View File
@@ -11,5 +11,8 @@ import (
// a line past its scan fails here rather than for a user.
func TestMain(m *testing.M) {
poisonReleased = true
// a test's saves are written as they come; TestSaveWaitsOutTheGap
// sets the gap
saveGap = 0
testenv.Main(m)
}
+45
View File
@@ -0,0 +1,45 @@
package sessions
import (
"sync/atomic"
"testing"
"time"
)
// A save right after another waits out saveGap rather than writing the
// whole index again (each Sessions poll wrote it), and Saved writes it at
// once, not after the gap.
func TestSaveWaitsOutTheGap(t *testing.T) {
setup(t)
var writes atomic.Int32
old := saveGap
saveGap = time.Hour
saveHook = func() { writes.Add(1) }
t.Cleanup(func() { saveGap, saveHook = old, nil })
saved()
saving.Lock()
saving.wrote = time.Time{}
saving.Unlock()
saveCache()
saved()
if n := writes.Load(); n != 1 {
t.Fatalf("%d writes of the first save, want 1", n)
}
saveCache()
saveCache()
time.Sleep(50 * time.Millisecond) // room for a write the gap should hold back
if n := writes.Load(); n != 1 {
t.Fatalf("%d writes with saveGap not yet past, want the first only", n)
}
done := make(chan struct{})
go func() { saved(); close(done) }()
select {
case <-done:
case <-time.After(10 * time.Second):
t.Fatal("Saved waited out the gap")
}
if n := writes.Load(); n != 2 {
t.Fatalf("%d writes once Saved, want the two saves in a row folded into one", n)
}
}
+43 -3
View File
@@ -936,7 +936,8 @@ func loadCache() {
// for a long history) takes a second or more, so it is done behind the
// request: the parses in the cache are never changed once put there (a new
// parse starts from a clone), so a copy of the map is a snapshot; saves in a
// row are folded into the latest.
// row are folded into the latest, and one is written saveGap after the
// last at the soonest.
func saveCache() {
snap := save{cacheFile{Version: cacheVersion, Zone: zone(), Files: maps.Clone(cache)}, CachePath()}
saving.Lock()
@@ -948,26 +949,58 @@ func saveCache() {
saving.running = true
go func() {
for {
saving.Lock()
wait := saveGap - time.Since(saving.wrote)
if saving.hurry {
wait = 0
}
saving.Unlock()
if wait > 0 {
select {
case <-time.After(wait):
case <-saveNow:
}
}
saving.Lock()
f := saving.next
saving.next = nil
if f == nil {
saving.running = false
saving.running, saving.hurry = false, false
select { // a Saved's wake-up, unused, cuts no later gap
case <-saveNow:
default:
}
saving.Broadcast()
saving.Unlock()
return
}
saving.Unlock()
writeCache(f)
saving.Lock()
saving.wrote = time.Now()
saving.Unlock()
}
}()
}
// saveGap is how long a save waits after the last one written. A Sessions
// or Usage page polls every 15 s, and a live session changes its file
// between any two, so each poll wrote the whole index again: tens of MB
// for a long history, ~20 MB allocated to encode it each time. The index
// only spares the next start a read; what a save not yet written misses is
// read again then. Saved does not wait it out.
var saveGap = time.Minute
// saveNow wakes a save waiting out saveGap once saving.hurry is set.
var saveNow = make(chan struct{}, 1)
var saving struct {
sync.Mutex
*sync.Cond
running bool
next *save
wrote time.Time // when the last save was written
hurry bool // Saved is waiting: no save waits out saveGap
}
type save struct {
@@ -981,9 +1014,16 @@ func init() { saving.Cond = sync.NewCond(&saving.Mutex) }
// it on disk.
func Saved() { saved() }
// saved waits for the saves under way.
// saved waits for the saves under way, written without their saveGap.
func saved() {
saving.Lock()
if saving.running {
saving.hurry = true
select {
case saveNow <- struct{}{}:
default:
}
}
for saving.running {
saving.Wait()
}