From 94503901c0cfb7db807d5eacad2f2393d67406a7 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 06:27:13 +0800 Subject: [PATCH] sessions: a string many kept files repeat is held once decodeCache's Unmarshal made a string for every file's title, cwd and model names, however many files gave the same one. On the real history copy (sbpairs, 27,699 kept files) 23,689 sessions a script ran carry one ~420-byte prompt as their title. The kept parses held 54 MB after a collection, 11 MB of it such copies. Each decode worker now takes those fields (title, cwd, first, named, model, provider, msg_model, msg_day, codex_nickname, and the Codex identity's role and parents) through unique.Make, so the parses hold 44 MB. The decode took 104-114 ms, as it did (121 ms when measured for e2dbb0340). One QueryPage(All) from requests-v1.bin under GOGC=25, as the GUI's warm-up runs it: max RSS 319-327 MB -> 296-305 MB. From cold (no file): 390 MB, against 448-462 MB before this and 31bff66be. TestDecodeCacheSharesRepeats decodes 2000 files of two titles and one cwd, model and role, and counts the copies held. It is red with share taken out (2000 of each). Run at this head: go test ./internal/sessions ./internal/usage (ok); go vet ./internal/sessions, GOOS=linux and GOOS=windows -tags nogui; gofmt -l. Not run: the full suite (one per machine). --- internal/sessions/cache_decode.go | 22 +++++++++++++++ internal/sessions/cache_decode_test.go | 37 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/internal/sessions/cache_decode.go b/internal/sessions/cache_decode.go index 79314e4dc..49f2f25cf 100644 --- a/internal/sessions/cache_decode.go +++ b/internal/sessions/cache_decode.go @@ -6,6 +6,7 @@ import ( "strings" "sync" "sync/atomic" + "unique" "unsafe" "github.com/tidwall/gjson" @@ -36,6 +37,8 @@ func decodeCache(b []byte, zone string) (map[string]*state, bool) { one := func(i int) { if json.Unmarshal(unsafe.Slice(unsafe.StringData(raws[i]), len(raws[i])), &parsed[i]) != nil { bad.Store(true) + } else if parsed[i] != nil { + parsed[i].share() } } if workers := min(runtime.GOMAXPROCS(0), 8, len(raws)/256); workers < 2 { @@ -69,3 +72,22 @@ func decodeCache(b []byte, zone string) (map[string]*state, bool) { } return files, true } + +// share has st's strings that many files repeat held once: an agent run +// by a script gives each of its sessions the same prompt, and a long +// history's 23.7k copies of one ~420-byte title, with its cwds and model +// names, were 11 of the 54 MB its kept parses held. +func (st *state) share() { + for _, s := range []*string{&st.Title, &st.Cwd, &st.First, &st.Named, &st.Model, &st.Provider, &st.MsgModel, &st.MsgDay, &st.CodexNickname} { + if *s != "" { + *s = unique.Make(*s).Value() + } + } + if id := st.CodexIdentity; id != nil { + for _, s := range []*string{&id.Role, &id.ParentSession, &id.RootSession} { + if *s != "" { + *s = unique.Make(*s).Value() + } + } + } +} diff --git a/internal/sessions/cache_decode_test.go b/internal/sessions/cache_decode_test.go index 143d9c9ff..a778adf15 100644 --- a/internal/sessions/cache_decode_test.go +++ b/internal/sessions/cache_decode_test.go @@ -6,6 +6,7 @@ import ( "reflect" "strings" "testing" + "unsafe" ) // The kept file decoded on a few goroutines is what one json.Unmarshal of @@ -54,3 +55,39 @@ func TestDecodeCacheIsUnmarshal(t *testing.T) { } } } + +// A title, cwd or model that many kept files repeat is held once, not +// once a file: 23.7k sessions a script ran with one prompt held 11 MB of +// copies of it. +func TestDecodeCacheSharesRepeats(t *testing.T) { + var b strings.Builder + fmt.Fprintf(&b, `{"version":%d,"zone":"z","files":{`, cacheVersion) + for i := range 2000 { + if i > 0 { + b.WriteString(",") + } + fmt.Fprintf(&b, `"/s/%d.jsonl":{"size":%d,"title":"重要执行方式 %d","cwd":"/w/p","model":"gpt-5.5","codex_identity":{"id":"c%d","role":"subagent"}}`, i, i, i%2, i) + } + b.WriteString(`}}`) + files, ok := decodeCache([]byte(b.String()), "z") + if !ok || len(files) != 2000 { + t.Fatalf("decoded %d files, %v", len(files), ok) + } + held := map[string]map[*byte]bool{} + for _, st := range files { + for name, s := range map[string]string{"title": st.Title, "cwd": st.Cwd, "model": st.Model, "role": st.CodexIdentity.Role} { + if held[name] == nil { + held[name] = map[*byte]bool{} + } + held[name][unsafe.StringData(s)] = true + } + } + for name, want := range map[string]int{"title": 2, "cwd": 1, "model": 1, "role": 1} { + if len(held[name]) != want { + t.Errorf("%s: %d copies held, want %d", name, len(held[name]), want) + } + } + if st := files["/s/3.jsonl"]; st.Title != "重要执行方式 1" || st.CodexIdentity.ID != "c3" { + t.Fatalf("file 3: %+v", st) + } +}