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.
This commit is contained in:
+4
-2
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user