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) + } +}