sessions: the kept parses are decoded on a few goroutines at start

One json.Unmarshal of sessions.json (40MB, 27,679 files on a long
history) took 310 to 440ms of every start before the first page (#3493).
decodeCache checks the file is valid JSON (gjson), reads its version and
zone, splits the files without copying them, and decodes each file's
parse with json.Unmarshal on up to 8 goroutines. The result is what one
Unmarshal gave: a key given twice is its last, and the file is refused
where it was refused (bad JSON, another version or zone, no files, a
parse that doesn't decode). Only the keys are copied out of the read.

Real data (sandbox copy of the live sessions.json, load ~60): Unmarshal
311ms, decodeCache 121ms, reflect.DeepEqual the same 27,679 parses.

Run at 5d2260d3e (origin/main, fetched): go test -timeout 45m of
sessions (14.6s), usage (18.4s), usageapi (7.9s), cli (35.9s) and tui
(15.4s) ok; TestDecodeCacheIsUnmarshal -race ok. gofmt clean; GOOS=linux
and GOOS=windows go vet -tags nogui ./internal/sessions/ clean.

TestDecodeCacheIsUnmarshal (new) compares decodeCache with Unmarshal on
3000 files, escaped keys, a null parse, a key given twice, no files, an
old version, another zone, a bad parse first and last, a cut file,
trailing bytes, a missing comma and garbage. Red with the validity check
taken out (trailing, missing comma), with a bad parse ignored, with the
first of two keys kept, and with the zone not compared.
This commit is contained in:
yetone
2026-10-25 06:09:30 +08:00
parent 611b9e1731
commit e2dbb03405
3 changed files with 131 additions and 3 deletions
+71
View File
@@ -0,0 +1,71 @@
package sessions
import (
"encoding/json"
"runtime"
"strings"
"sync"
"sync/atomic"
"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)
}
}
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
}
+56
View File
@@ -0,0 +1,56 @@
package sessions
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"
)
// The kept file decoded on a few goroutines is what one json.Unmarshal of
// it was, and is refused where that was.
func TestDecodeCacheIsUnmarshal(t *testing.T) {
unmarshal := func(b []byte, zone string) (map[string]*state, bool) {
var c cacheFile
if json.Unmarshal(b, &c) != nil || c.Version != cacheVersion || c.Zone != zone || c.Files == nil {
return nil, false
}
return c.Files, true
}
var many strings.Builder
fmt.Fprintf(&many, `{"version":%d,"zone":"z","files":{`, cacheVersion)
for i := range 3000 {
if i > 0 {
many.WriteString(",")
}
fmt.Fprintf(&many, `"/s/%d.jsonl":{"size":%d,"mod":%d,"title":"té %d","models":{"m":{"input":%d}},"claude":[{"key":"message:%d","m":{"request":"r%d"}}],"days":{"2026-10-%02d":{"models":{"m":{"output":%d}}}}}`, i, i, i*7, i, i, i, i, 1+i%28, i)
}
many.WriteString(`}}`)
v := cacheVersion
cases := map[string]string{
"many": many.String(),
"escaped keys": fmt.Sprintf(`{"version":%d,"zone":"z","files":{"/a\"b\\cé.jsonl":{"size":1},"/n":null}}`, v),
"twice": fmt.Sprintf(`{"zone":"z","files":{"/a":{"size":1},"/a":{"size":2}},"version":%d}`, v),
"empty": fmt.Sprintf(`{"version":%d,"zone":"z","files":{}}`, v),
"no files": fmt.Sprintf(`{"version":%d,"zone":"z","files":null}`, v),
"old version": fmt.Sprintf(`{"version":%d,"zone":"z","files":{"/a":{"size":1}}}`, v-1),
"other zone": fmt.Sprintf(`{"version":%d,"zone":"y","files":{"/a":{"size":1}}}`, v),
"bad file": fmt.Sprintf(`{"version":%d,"zone":"z","files":{"/a":{"size":"one"}}}`, v),
"bad late": strings.Replace(many.String(), `"size":2999`, `"size":"x"`, 1),
"cut": many.String()[:many.Len()-5],
"trailing": many.String() + " x",
"no comma": fmt.Sprintf(`{"version":%d,"zone":"z","files":{"/a":{"size":1} "/b":{"size":2}}}`, v),
"garbage": "garbage",
"empty bytes": "",
}
for name, in := range cases {
want, wantOK := unmarshal([]byte(in), "z")
got, ok := decodeCache([]byte(in), "z")
if ok != wantOK || !reflect.DeepEqual(got, want) {
wj, _ := json.Marshal(want)
gj, _ := json.Marshal(got)
t.Errorf("%s: got %v %.300s, want %v %.300s", name, ok, gj, wantOK, wj)
}
}
}
+4 -3
View File
@@ -753,9 +753,10 @@ func loadCache() {
cacheGeneration++
cache = map[string]*state{}
saved() // the kept file as the last save left it
var c cacheFile
if b, err := os.ReadFile(CachePath()); err == nil && json.Unmarshal(b, &c) == nil && c.Version == cacheVersion && c.Zone == z && c.Files != nil {
cache = c.Files
if b, err := os.ReadFile(CachePath()); err == nil {
if files, ok := decodeCache(b, z); ok {
cache = files
}
}
}