From 0516c18ca3b5d1f9597c00e2d516ef5578a96a25 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 14:25:55 +0800 Subject: [PATCH] gui: writeJSONRows hands the encoder each row by pointer Every Sessions poll encodes all ~17k rows for their hashes, even when the page already has the answer. enc.Encode(rows[i]) boxed each row into an any, a copy allocated per row per poll. &rows[i] encodes the same bytes (no type in sessionJSON has a pointer-receiver marshaler) without the copy. TestJSONRowsNotCopiedEach: a 2000-row poll the page already has makes at most 200 allocations. Broken by hand (back to rows[i]): red, "made 2022 allocations". TestWriteJSONRowsWritesWhatMarshalDid still checks the bytes against json.Marshal per row. Measured on mote's build against the real ~17k sessions (sandbox HOME, APFS clones), 30 polls, heap diff alloc_space: writeJSONRows 201 MB -> 98 MB whole poll 854 MB -> 726 MB CPU over 30 polls 4.46s -> 4.44s (no change past noise); /api/sessions median 74ms -> 73ms. Run on mote: go test -run JSONRows ./internal/gui ok. Not run: the full suite (one-line change in writeJSONRows, its tests run). Then rebased onto 586b59fe7 (provider: Devin's plugin min); not rerun. --- internal/gui/api.go | 6 ++++-- internal/gui/writejsonrows_buf_test.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/internal/gui/api.go b/internal/gui/api.go index adb034675..1fb29b085 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -3006,10 +3006,12 @@ func writeJSONRows[T any](rw http.ResponseWriter, r *http.Request, key string, r end = append(end, '\n') var row bytes.Buffer enc := json.NewEncoder(&row) - // encode is row i as Marshal gives it: Encode ends it with a newline + // encode is row i as Marshal gives it: Encode ends it with a newline. + // It is handed a pointer: rows[i] as an any was a copy of the row + // allocated for each row of each poll, ~2.7 MB a Sessions poll. encode := func(i int) ([]byte, error) { row.Reset() - if err := enc.Encode(rows[i]); err != nil { + if err := enc.Encode(&rows[i]); err != nil { return nil, err } return row.Bytes()[:row.Len()-1], nil diff --git a/internal/gui/writejsonrows_buf_test.go b/internal/gui/writejsonrows_buf_test.go index f89b3a592..44352d9fb 100644 --- a/internal/gui/writejsonrows_buf_test.go +++ b/internal/gui/writejsonrows_buf_test.go @@ -76,3 +76,26 @@ func TestWriteJSONRowsWritesWhatMarshalDid(t *testing.T) { t.Fatalf("delta\n got %q\nwant %q", w.Body, want) } } + +// TestJSONRowsNotCopiedEach: a poll whose answer the page has already +// encodes every row for its hash, and each row handed to the encoder as +// an any was a copy allocated for it, ~2.7 MB a Sessions poll. +func TestJSONRowsNotCopiedEach(t *testing.T) { + if raceOn { + t.Skip("-race: sync.Pool drops what it holds, so allocations are not what a build allocates") + } + rows := make([]rowsBufRow, 2000) + for i := range rows { + rows[i] = rowsBufRow{Name: "row", Note: "a note", N: float64(i)} + } + w := httptest.NewRecorder() + writeJSONRows(w, httptest.NewRequest("GET", "/x", nil), "rows", rows, struct{}{}) + r := httptest.NewRequest("GET", "/x", nil) + r.Header.Set("X-Magpie-Had", w.Header().Get("X-Magpie-Tag")) + allocs := testing.AllocsPerRun(5, func() { + writeJSONRows(httptest.NewRecorder(), r, "rows", rows, struct{}{}) + }) + if allocs > float64(len(rows))/10 { + t.Fatalf("a poll of %d rows the page has made %.0f allocations", len(rows), allocs) + } +}