diff --git a/internal/sessions/cache_decode.go b/internal/sessions/cache_decode.go new file mode 100644 index 000000000..79314e4dc --- /dev/null +++ b/internal/sessions/cache_decode.go @@ -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 +} diff --git a/internal/sessions/cache_decode_test.go b/internal/sessions/cache_decode_test.go new file mode 100644 index 000000000..143d9c9ff --- /dev/null +++ b/internal/sessions/cache_decode_test.go @@ -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) + } + } +} diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index a248cb6e7..54d5cb3dc 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -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 + } } }