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), base3393e61d0vs 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 on2beed3b04): 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).
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package sessions
|
|
|
|
import (
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestObjectEnd(t *testing.T) {
|
|
for _, c := range []struct {
|
|
in string
|
|
want int
|
|
}{
|
|
{`{}`, 2},
|
|
{`{"a":1},{"b":2}`, 7},
|
|
{`{"a":{"b":[1,{"c":2}]}}tail`, 23},
|
|
{`{"a":"}{]["}x`, 12},
|
|
{`{"a":"\"}"}x`, 11},
|
|
{`{"a":"\\"}x`, 10},
|
|
{`{"a":1`, -1},
|
|
{`{"a":"}`, -1},
|
|
{` {}`, -1},
|
|
{``, -1},
|
|
} {
|
|
if got := objectEnd([]byte(c.in)); got != c.want {
|
|
t.Errorf("objectEnd(%s) = %d, want %d", c.in, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A reply's tool blocks and usage are decoded from the object itself. A
|
|
// json.Decoder over the rest of the line copied all of it each time: a
|
|
// reply with a large tool input cost its whole length again per block.
|
|
func TestClaudeReplyDecodesWithoutCopyingTheLine(t *testing.T) {
|
|
at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC)
|
|
big := strings.Repeat("x", 1<<20)
|
|
content := `[{"type":"tool_use","id":"tool-1","name":"Write","input":{"content":"` + big + `"}},{"type":"tool_use","id":"tool-2","name":"Skill","input":{"skill":"review"}}]`
|
|
line := claudeUsageLine(at, "a", "req-a", "m", Tokens{Input: 10, Output: 2}, content, true)
|
|
s := &state{}
|
|
claudeLine(s, line, true)
|
|
if s.Models.of("m") != (Tokens{Input: 10, Output: 2}) {
|
|
t.Fatalf("tokens %+v", s.Models.of("m"))
|
|
}
|
|
const n = 20
|
|
var before, after runtime.MemStats
|
|
runtime.ReadMemStats(&before)
|
|
for range n {
|
|
claudeLine(&state{}, line, true)
|
|
}
|
|
runtime.ReadMemStats(&after)
|
|
// the old decoders copied ~1 MiB a block: 2+ MiB a line
|
|
if per := (after.TotalAlloc - before.TotalAlloc) / n; per > 256<<10 {
|
|
t.Fatalf("a %d KB reply allocated %d KB to read", len(line)>>10, per>>10)
|
|
}
|
|
}
|