Files
magpie/internal/sessions/named.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

209 lines
4.4 KiB
Go

package sessions
import (
"bytes"
"encoding/json"
"errors"
"iter"
"slices"
"strings"
"unique"
"unsafe"
"github.com/tidwall/gjson"
)
// named holds a few values by name, in the names' order, and is written as
// the JSON object a map is. A file's parse has a model or two, a day or two
// and a few tools, and Go's map of one is a group of eight: ~500 bytes for
// one model's Tokens, where this holds 56. A long history's 15.9k kept
// parses held ~18MB of such maps.
type named[V any] []entry[V]
type entry[V any] struct {
name string
v V
}
func (n named[V]) find(name string) (int, bool) {
return slices.BinarySearchFunc(n, name, func(e entry[V], name string) int { return strings.Compare(e.name, name) })
}
// get is name's value, and whether there is one.
func (n named[V]) get(name string) (V, bool) {
if i, ok := n.find(name); ok {
return n[i].v, true
}
var zero V
return zero, false
}
// of is name's value, or the zero value when there is none.
func (n named[V]) of(name string) V {
v, _ := n.get(name)
return v
}
// at is where name's value is held, a zero one put in when there is none.
// It holds until the next name is put in.
func (n *named[V]) at(name string) *V {
i, ok := n.find(name)
if !ok {
*n = slices.Insert(*n, i, entry[V]{name: name})
}
return &(*n)[i].v
}
// del takes name out, leaving nil when nothing is left, as a map's delete
// and the nil the code set after it did.
func (n *named[V]) del(name string) {
if i, ok := n.find(name); ok {
*n = slices.Delete(*n, i, i+1)
}
if len(*n) == 0 {
*n = nil
}
}
// all is each name and its value, in the names' order.
func (n named[V]) all() iter.Seq2[string, V] {
return func(yield func(string, V) bool) {
for _, e := range n {
if !yield(e.name, e.v) {
return
}
}
}
}
// MarshalJSON writes n as json.Marshal writes a map: keys in order.
func (n named[V]) MarshalJSON() ([]byte, error) {
if n == nil {
return []byte("null"), nil
}
var b bytes.Buffer
b.WriteByte('{')
for i, e := range n {
if i > 0 {
b.WriteByte(',')
}
k, err := json.Marshal(e.name)
if err != nil {
return nil, err
}
b.Write(k)
b.WriteByte(':')
v, err := json.Marshal(e.v)
if err != nil {
return nil, err
}
b.Write(v)
}
b.WriteByte('}')
return b.Bytes(), nil
}
var errNotObject = errors.New("sessions: not a JSON object")
// UnmarshalJSON reads an object as json.Unmarshal reads it into a map: a
// name given twice is its last. b is the cache file's own bytes, which are
// not kept: names are held once (unique) for all the parses that use them.
func (n *named[V]) UnmarshalJSON(b []byte) error {
r := gjson.ParseBytes(b)
if r.Type == gjson.Null {
*n = nil
return nil
}
if !r.IsObject() {
return errNotObject
}
size := 0
r.ForEach(func(_, _ gjson.Result) bool {
size++
return true
})
out := make(named[V], 0, size)
var err error
r.ForEach(func(k, v gjson.Result) bool {
var val V
if !quick(&val, v) {
if err = json.Unmarshal(unsafe.Slice(unsafe.StringData(v.Raw), len(v.Raw)), &val); err != nil {
return false
}
}
*out.at(unique.Make(k.String()).Value()) = val
return true
})
if err != nil {
return err
}
*n = out
return nil
}
// quick reads v into an int or Tokens without reflection when every number
// in it is a plain integer, as a cache magpie wrote has them; anything else
// is left to json.Unmarshal, and its errors.
func quick(val any, v gjson.Result) bool {
switch p := val.(type) {
case *int:
n, ok := integer(v.Raw)
*p = n
return ok
case *Tokens:
if !v.IsObject() {
return false
}
ok := true
v.ForEach(func(k, f gjson.Result) bool {
var at *int
switch k.Raw {
case `"input"`:
at = &p.Input
case `"output"`:
at = &p.Output
case `"cache_read"`:
at = &p.CacheRead
case `"cache_write"`:
at = &p.CacheWrite
case `"cache_write_1h"`:
at = &p.CacheWrite1h
default:
ok = false
return false
}
*at, ok = integer(f.Raw)
return ok
})
if !ok {
*p = Tokens{}
}
return ok
}
return false
}
// integer is s as an int when it is one written plainly: an optional minus
// and up to 18 digits, no leading zero.
func integer(s string) (int, bool) {
neg := strings.HasPrefix(s, "-")
if neg {
s = s[1:]
}
if s == "" || len(s) > 18 || len(s) > 1 && s[0] == '0' {
return 0, false
}
n := 0
for i := range len(s) {
c := s[i]
if c < '0' || c > '9' {
return 0, false
}
n = n*10 + int(c-'0')
}
if neg {
n = -n
}
return n, true
}