Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gui: the Sessions list is encoded once, into the answer it is sent as

yetoneecd063827699
diff --git a/internal/gui/api.go b/internal/gui/api.go
index 556c64881..0a9248103 100644
--- a/internal/gui/api.go
+++ b/internal/gui/api.go
@@ -2546,6 +2546,10 @@ var sentRows struct {
const sentRowsKept = 4
+// rowsSize is the size of the last answer writeJSONRows wrote of each key,
+// what the next one's buffer is made for.
+var rowsSize sync.Map
+
// writeJSONRows is writeJSONSame for an answer that is a long list, key,
// beside the fields of rest: one asked with the tag of a list sent before
// that changed since gets X-Magpie-Delta: key, and its list holds only the
@@ -2554,30 +2558,38 @@ const sentRowsKept = 4
// session's row changes every poll, and the Sessions tab read all ~5000
// rows (~8 MB) again for it.
func writeJSONRows[T any](rw http.ResponseWriter, r *http.Request, key string, rows []T, rest any) {
- raw := make([][]byte, len(rows))
- sums := make([][16]byte, len(rows))
- for i, row := range rows {
- b, err := json.Marshal(row)
- if err != nil {
- fail(rw, err)
- return
- }
- raw[i] = b
- sum := sha256.Sum256(b)
- sums[i] = [16]byte(sum[:16])
- }
tail, err := json.Marshal(rest)
if err != nil {
fail(rw, err)
return
}
k, _ := json.Marshal(key)
- body := func(items [][]byte) []byte {
- var b bytes.Buffer
- b.WriteByte('{')
- b.Write(k)
- b.WriteString(":[")
- b.Write(bytes.Join(items, []byte(",")))
+ // the rows are written once, into the answer itself: each row's
+ // Marshal, their Join and the answer's buffer were three copies of the
+ // ~8 MB list a poll, and the buffer grew to it from nothing
+ var whole bytes.Buffer
+ if n, ok := rowsSize.Load(key); ok {
+ whole.Grow(n.(int) + n.(int)/8)
+ }
+ whole.WriteByte('{')
+ whole.Write(k)
+ whole.WriteString(":[")
+ enc := json.NewEncoder(&whole)
+ ends := make([]int, len(rows)+1)
+ ends[0] = whole.Len()
+ for i := range rows {
+ if err := enc.Encode(rows[i]); err != nil {
+ fail(rw, err)
+ return
+ }
+ // Encode ends the row with a newline: it is the comma after it
+ whole.Bytes()[whole.Len()-1] = ','
+ ends[i+1] = whole.Len()
+ }
+ if len(rows) > 0 {
+ whole.Truncate(whole.Len() - 1)
+ }
+ end := func(b *bytes.Buffer) {
b.WriteByte(']')
if len(tail) > 2 {
b.WriteByte(',')
@@ -2586,10 +2598,27 @@ func writeJSONRows[T any](rw http.ResponseWriter, r *http.Request, key string, r
b.WriteByte('}')
}
b.WriteByte('\n')
+ }
+ end(&whole)
+ rowsSize.Store(key, whole.Len())
+ all := whole.Bytes()
+ raw := make([][]byte, len(rows))
+ sums := make([][16]byte, len(rows))
+ for i := range rows {
+ raw[i] = all[ends[i] : ends[i+1]-1]
+ sum := sha256.Sum256(raw[i])
+ sums[i] = [16]byte(sum[:16])
+ }
+ body := func(items [][]byte) []byte {
+ var b bytes.Buffer
+ b.WriteByte('{')
+ b.Write(k)
+ b.WriteString(":[")
+ b.Write(bytes.Join(items, []byte(",")))
+ end(&b)
return b.Bytes()
}
- whole := body(raw)
- sum := sha256.Sum256(whole)
+ sum := sha256.Sum256(all)
tag := hex.EncodeToString(sum[:16])
rw.Header().Set("X-Magpie-Tag", tag)
had := r.Header.Get("X-Magpie-Had")
@@ -2614,7 +2643,7 @@ func writeJSONRows[T any](rw http.ResponseWriter, r *http.Request, key string, r
sentRows.Unlock()
rw.Header().Set("Content-Type", "application/json")
if !ok || had == "" {
- _, _ = rw.Write(whole)
+ _, _ = rw.Write(all)
return
}
at := make(map[[16]byte][]int, len(was))
diff --git a/internal/gui/writejsonrows_buf_test.go b/internal/gui/writejsonrows_buf_test.go
new file mode 100644
index 000000000..f89b3a592
--- /dev/null
+++ b/internal/gui/writejsonrows_buf_test.go
@@ -0,0 +1,78 @@
+package gui
+
+import (
+ "bytes"
+ "encoding/json"
+ "net/http/httptest"
+ "testing"
+)
+
+type rowsBufRow struct {
+ Name string `json:"name"`
+ Note string `json:"note,omitempty"`
+ N float64 `json:"n"`
+}
+
+// writeJSONRows writes the rows straight into the answer, and the answer is
+// byte for byte what Marshal of each row, joined, gave: the page's delta
+// and its tag both rest on those bytes.
+func TestWriteJSONRowsWritesWhatMarshalDid(t *testing.T) {
+ rows := []rowsBufRow{
+ {Name: "a<b>&c", Note: "\u2028 línea", N: 1.5},
+ {Name: "", N: 0},
+ {Name: "引号\"", Note: "x\ny", N: 1e21},
+ }
+ old := func(rows []rowsBufRow, rest any) []byte {
+ var items [][]byte
+ for _, r := range rows {
+ b, err := json.Marshal(r)
+ if err != nil {
+ t.Fatal(err)
+ }
+ items = append(items, b)
+ }
+ tail, _ := json.Marshal(rest)
+ var b bytes.Buffer
+ b.WriteString(`{"rows":[`)
+ b.Write(bytes.Join(items, []byte(",")))
+ b.WriteByte(']')
+ if len(tail) > 2 {
+ b.WriteByte(',')
+ b.Write(tail[1:])
+ } else {
+ b.WriteByte('}')
+ }
+ b.WriteByte('\n')
+ return b.Bytes()
+ }
+ for _, rest := range []any{struct{}{}, map[string]any{"total": 3, "at": "<now>"}} {
+ for n := 0; n <= len(rows); n++ {
+ // twice, so the second is written into a buffer sized from the first
+ for range 2 {
+ w := httptest.NewRecorder()
+ writeJSONRows(w, httptest.NewRequest("GET", "/x", nil), "rows", rows[:n], rest)
+ if want := old(rows[:n], rest); !bytes.Equal(w.Body.Bytes(), want) {
+ t.Fatalf("%d rows, rest %v:\n got %q\nwant %q", n, rest, w.Body.Bytes(), want)
+ }
+ }
+ }
+ }
+
+ // a row that changed goes in a delta as its own bytes, and the rest as runs
+ w := httptest.NewRecorder()
+ writeJSONRows(w, httptest.NewRequest("GET", "/x", nil), "rows", rows, struct{}{})
+ tag := w.Header().Get("X-Magpie-Tag")
+ changed := append([]rowsBufRow(nil), rows...)
+ changed[1].Name = "new"
+ r := httptest.NewRequest("GET", "/x", nil)
+ r.Header.Set("X-Magpie-Had", tag)
+ w = httptest.NewRecorder()
+ writeJSONRows(w, r, "rows", changed, struct{}{})
+ if w.Header().Get("X-Magpie-Delta") != "rows" {
+ t.Fatalf("no delta: %q", w.Body)
+ }
+ mid, _ := json.Marshal(changed[1])
+ if want := `{"rows":[[0,1],` + string(mid) + `,[2,1]]}` + "\n"; w.Body.String() != want {
+ t.Fatalf("delta\n got %q\nwant %q", w.Body, want)
+ }
+}