sessions: a kept file of one day holds that day's models once (#3493)

A long history's kept parses held 28MB live after a start's decode of
sessions.json (15,624 files). 15,345 of them have exactly one day, and
that day's model map is the file's own, so every such file held two
equal maps of ~700B each. decodeCache's share now points the day at the
file's map when they are equal.

A kept state is never written: use, unuse and dropUse are the only
writers of Models, and every parser (sessions.go, calls.go, calls_dsh,
dsh, droid, grok, kimi, cline) writes a clone, which copies the two
maps apart.

Run, at this head on 42ab93551:
- the real sessions.json (sbpairs, 22.8MB): live heap after decode
  28MB -> 21MB, 3 of 3.
- StatsAt(3650) over the real sessions in a sandbox HOME, digest of
  the 96 days before 10-24: a21dc1d1... with this change (twice, the
  second from the cache it wrote) and with the sharing turned off
  (twice): the same.
- TestDecodeCacheSharesOneDaysModels: red with the sharing removed
  ("holds its models twice") and red with clone keeping the shared map
  ("the kept state was written").
- go test -count=1 ./internal/sessions ./internal/usage ./internal/gui
  ok; go test -race ./internal/sessions ok; GOOS=linux and
  GOOS=windows go vet -tags nogui on the three ok; gofmt -l clean.

Not tried: a full go test ./... (one suite per machine).
This commit is contained in:
yetone
2026-10-25 07:33:35 +08:00
parent 42ab935516
commit 13f9070135
2 changed files with 43 additions and 0 deletions
+10
View File
@@ -145,4 +145,14 @@ func (st *state) share() {
}
}
}
// a file of one day (15,345 of a long history's 15,624) has that day's
// models as its own: held once, they were ~10 of the 28MB kept. A kept
// state is never written: a parse writes a clone, which holds them apart.
if len(st.Days) == 1 {
for _, d := range st.Days {
if len(d.Models) == len(st.Models) && maps.Equal(d.Models, st.Models) {
d.Models = st.Models
}
}
}
}
+33
View File
@@ -94,6 +94,39 @@ func TestDecodeCacheSharesRepeats(t *testing.T) {
}
}
// A file of one day holds that day's models once, and a parse of it, from
// a clone, counts a call once in each: the kept state is as it was read.
func TestDecodeCacheSharesOneDaysModels(t *testing.T) {
m := `{"gpt-5.5":{"input":10,"output":2,"cache_read":0,"cache_write":0}}`
b := fmt.Sprintf(`{"version":%d,"zone":"z","files":{"/one":{"size":1,"models":%s,"days":{"2026-10-24":{"m":%s,"a":5}}},"/other":{"size":1,"models":%s,"days":{"2026-10-24":{"m":{"o3":{"input":1,"output":0,"cache_read":0,"cache_write":0}}}}},"/two":{"size":1,"models":%s,"days":{"2026-10-23":{"m":%s},"2026-10-24":{"a":1}}}}}`, cacheVersion, m, m, m, m, m)
files, ok := decodeCache([]byte(b), "z")
if !ok || len(files) != 3 {
t.Fatalf("decoded %d files, %v", len(files), ok)
}
same := func(a, b map[string]Tokens) bool {
return reflect.ValueOf(a).UnsafePointer() == reflect.ValueOf(b).UnsafePointer()
}
one, other, two := files["/one"], files["/other"], files["/two"]
if !same(one.Models, one.Days["2026-10-24"].Models) {
t.Errorf("a file of one day holds its models twice")
}
if same(other.Models, other.Days["2026-10-24"].Models) || same(two.Models, two.Days["2026-10-23"].Models) {
t.Errorf("models held once that aren't the same, or of a file of two days")
}
c := one.clone()
c.use("2026-10-24", "gpt-5.5", Tokens{Input: 1})
if c.Models["gpt-5.5"].Input != 11 || c.Days["2026-10-24"].Models["gpt-5.5"].Input != 11 {
t.Fatalf("a parse counted %d and %d, want 11 in each", c.Models["gpt-5.5"].Input, c.Days["2026-10-24"].Models["gpt-5.5"].Input)
}
c.dropUse("2026-10-24", "gpt-5.5", Tokens{Input: 11, Output: 2})
if len(c.Models) != 0 || c.Days["2026-10-24"] == nil || len(c.Days["2026-10-24"].Models) != 0 {
t.Fatalf("a parse took back to %v and %+v", c.Models, c.Days["2026-10-24"])
}
if one.Models["gpt-5.5"].Input != 10 || one.Days["2026-10-24"].Models["gpt-5.5"].Input != 10 {
t.Fatalf("the kept state was written: %v", one.Models)
}
}
// A save writes the file json.Marshal would, byte for byte, a file's parse
// at a time: keys in order and escaped as Marshal escapes them, a file of
// no parse, an empty index and none at all.