Files
magpie/internal/sessions/cache_decode.go
T
yetone bbb4fd478a sessions: a parse's models, days and tools are sorted slices, not maps
A kept parse holds a model or two, a day or two and a few tools, and Go's
map of one entry is a whole group of eight: ~500 bytes for one model's
Tokens, where a sorted slice entry is 56. Over a long history (15.9k kept
parses) state.Models, Days, day.Models, Tools and Skills held 18.4MB as
maps and 4.2MB as named[V], a slice kept in name order. It is written as
the same JSON object a map writes, byte for byte, so the parse cache
saved before reads unchanged; names are interned with unique.Make, and
plain integers in ints and Tokens are read without reflection.

Measured with the real ~/.codex and ~/.claude histories (sandbox HOME,
warm cache, 40s after start), base 3393e61d0 vs this change, two
alternating runs each:
  live heap   102 / 105MB  ->  92 / 91MB
  footprint   164 / 167MB  -> 156 / 150MB
Cache decode at start: ~58ms -> ~61ms (best of 7).
Side by side on the same data, /api/sessions/overview?agent=codex and
/api/stats?days=30 are identical; the other overview queries differed
only in the two Claude sessions being written during the run.

TestNamedIsAMapInJSON: marshal is byte-equal to a map's (HTML-escaped
key included), round trip, a repeated name keeps its last value, an
unknown Tokens field is ignored, omitempty leaves none out, and 1.5,
"1", [1], 1e3 and a too-big int fail as they fail into a map. Broken by
hand (the int fast path accepting 1.5) it went red.

Run at this head (rebased on 2beed3b04): gofmt clean; go build ./...;
GOOS=linux go vet -tags nogui sessions, gui, usage; GOOS=windows vet
sessions; go test -timeout 45m ./internal/sessions ./internal/usage
./internal/gui ./internal/gateway all ok.
Not run: the full ./... suite and the GUI node tests (no GUI change).
2026-10-27 04:39:56 +08:00

157 lines
4.1 KiB
Go

package sessions
import (
"bytes"
"encoding/json"
"io"
"maps"
"runtime"
"slices"
"strings"
"sync"
"sync/atomic"
"unique"
"unsafe"
"github.com/tidwall/gjson"
)
// decodeCache is the kept file b as json.Unmarshal reads it, with the files
// decoded on a few goroutines: one Unmarshal of a long history's 40MB took
// 320 to 440ms of every start (#3493). False when b isn't valid JSON, is of
// another version or zone, or a file's parse doesn't decode.
func decodeCache(b []byte, zone string) (map[string]*state, bool) {
if !gjson.ValidBytes(b) {
return nil, false
}
// b is never changed, and nothing read from it is kept but cloned keys
s := unsafe.String(unsafe.SliceData(b), len(b))
head := gjson.GetMany(s, "version", "zone", "files")
if head[0].Type != gjson.Number || head[0].Int() != cacheVersion || head[1].String() != zone || !head[2].IsObject() {
return nil, false
}
var keys, raws []string
head[2].ForEach(func(k, v gjson.Result) bool {
keys = append(keys, strings.Clone(k.String()))
raws = append(raws, v.Raw)
return true
})
parsed := make([]*state, len(raws))
var bad atomic.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 {
for i := range raws {
one(i)
}
} else {
var next atomic.Int64
var wg sync.WaitGroup
for range workers {
wg.Go(func() {
for {
i := int(next.Add(64)) - 64
if i >= len(raws) || bad.Load() {
return
}
for j := i; j < min(i+64, len(raws)); j++ {
one(j)
}
}
})
}
wg.Wait()
}
if bad.Load() {
return nil, false
}
files := make(map[string]*state, len(keys))
for i, k := range keys {
files[k] = parsed[i] // a key given twice is its last, as Unmarshal has it
}
return files, true
}
// encodeCache writes c as json.Marshal does, a file's parse at a time: one
// Marshal of a long history's 23MB grew its buffer to twice that, ~85MB a
// save, and a save follows every refresh that read a changed file.
func encodeCache(w io.Writer, c cacheFile) error {
var b bytes.Buffer
enc := json.NewEncoder(&b)
// put appends v as Marshal has it: Encode ends it with a newline
put := func(v any) error {
if err := enc.Encode(v); err != nil {
return err
}
b.Truncate(b.Len() - 1)
return nil
}
if err := put(struct {
Version int `json:"version"`
Zone string `json:"zone"`
}{c.Version, c.Zone}); err != nil {
return err
}
b.Truncate(b.Len() - 1)
b.WriteString(`,"files":`)
if c.Files == nil {
b.WriteString("null}")
_, err := w.Write(b.Bytes())
return err
}
b.WriteByte('{')
for i, k := range slices.Sorted(maps.Keys(c.Files)) {
if i > 0 {
b.WriteByte(',')
}
if err := put(k); err != nil {
return err
}
b.WriteByte(':')
if err := put(c.Files[k]); err != nil {
return err
}
if b.Len() >= 256<<10 {
if _, err := w.Write(b.Bytes()); err != nil {
return err
}
b.Reset()
}
}
b.WriteString("}}")
_, err := w.Write(b.Bytes())
return err
}
// 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()
}
}
}
// 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 {
if d := st.Days[0].v; d != nil && slices.Equal(d.Models, st.Models) {
d.Models = st.Models
}
}
}