Files
magpie/internal/gui/providerswrite.go
T
yetone fd70def825 gui: a Providers poll sums what it keeps while writing the answer
Each poll of /api/providers encoded the answer for the page and then
encoded the kept last-known list again only to sum it (keepProviders,
encodedSum): 32-34 MB of what forty polls allocated with real accounts.

writeProvidersJSON now returns the encodedSum of keptProviders(s), taken
from the same encoding, as writeStateJSON does for /api/state. A row
kept as it is (no headers, proxy, extra body or account proxies) is
encoded once into the answer and the sum; only a row with those is
encoded again without them. The head and tail of the kept file come
from marshaling it with no rows. A nil row list, or a head that doesn't
match, falls back to the old writeJSON and encodedSum. The other
keepProviders callers (after a save) are unchanged.

Run on mote at this tree:
- gofmt, go vet ./internal/gui, GOOS=windows go vet -tags nogui: ok.
- go test -run 'Providers|Kept' ./internal/gui 3 times alone: ok.
- New TestProvidersWriteSumsWhatIsKept: the sum equals
  encodedSum(keptProviders(s)) for bigProviders, rows with each secret
  field, gateway calls, moved models, and empty and nil lists. Broken
  by hand twice: keepsAllOf always true (secrets summed) and the kept
  tail left out of the sum. Red both times.
- go test ./internal/gui: only the known reds,
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners (#3949).
- -race -run 'Providers|Kept': TestProvidersWriteGrowsNoBufferItsSize
  red, and red the same way at the base (c76bcaea7, twice): an
  allocation count under -race, where sync.Pool drops items. Fixed in
  its own commit next.

Real accounts, sandbox HOME, 40 polls of state, providers and drift,
v0.1.2509 (msteady) vs this (mpsum), two rounds each:
- alloc_space shown: 365.6/364.3 MB -> 341.5/329.8 MB; keepProviders
  32.9/32.1 MB -> gone; writeProvidersJSON 15.3/13.6 -> 9.9/14.9 MB.
- poll CPU 4.21/4.27 s -> 4.18/4.10 s; footprint peak 111/116 ->
  112/108 MB.
- /api/state and drift answers byte-identical; /api/providers differs
  only in the providers' "fetched" time.

Not run: the Linux and Windows apps; a save's keepProviders path is
unchanged and was not exercised.
2026-11-01 09:04:26 +08:00

78 lines
2.4 KiB
Go

package gui
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/json"
"io"
"net/http"
)
// providersNull is how a providersJSON with no providers begins:
// Providers is its first field.
const providersNull = `{"providers":null`
// writeProvidersJSON writes s to rw as writeJSON does, each provider's row
// encoded alone. Encoding the answer whole grew a buffer to its size each
// poll: 27 MB of what forty polls of the Providers page allocated with
// real accounts.
//
// It returns the encodedSum of what keepProviders keeps of s, taken from
// the same encoding: a row kept as it is is summed as it is written, and
// only one with headers, a proxy or an extra body is encoded again
// without them. Encoding the kept list again only to sum it was 37 MB of
// what forty polls allocated.
func writeProvidersJSON(rw http.ResponseWriter, s providersJSON) ([32]byte, bool) {
rows := s.Providers
f := keptProviders(s)
s.Providers, f.Providers.Providers = nil, nil
tail, err := json.Marshal(s)
keptTail, kerr := json.Marshal(f)
v, _ := json.Marshal(Version)
keptHead := `{"version":` + string(v) + `,"providers":` + providersNull
if rows == nil || err != nil || kerr != nil ||
!bytes.HasPrefix(tail, []byte(providersNull)) || !bytes.HasPrefix(keptTail, []byte(keptHead)) {
s.Providers = rows
writeJSON(rw, s)
sum, err := encodedSum(keptProviders(s))
return sum, err == nil
}
rw.Header().Set("Content-Type", "application/json")
out := bufio.NewWriterSize(rw, 32<<10)
defer out.Flush()
h := sha256.New()
_, _ = out.WriteString(`{"providers":[`)
h.Write([]byte(keptHead[:len(keptHead)-len("null")] + "["))
enc := json.NewEncoder(lineless{out})
both := json.NewEncoder(lineless{io.MultiWriter(out, h)})
kept := json.NewEncoder(lineless{h})
for i := range rows {
if i > 0 {
_ = out.WriteByte(',')
h.Write([]byte{','})
}
// a pointer, so a method on *T is used as it is in the slice
if keepsAllOf(&rows[i]) {
if both.Encode(&rows[i]) != nil {
return [32]byte{}, false
}
continue
}
if enc.Encode(&rows[i]) != nil {
return [32]byte{}, false
}
k := keptProvider(rows[i])
if kept.Encode(&k) != nil {
return [32]byte{}, false
}
}
_ = out.WriteByte(']')
_, _ = out.Write(tail[len(providersNull):])
_ = out.WriteByte('\n')
h.Write([]byte{']'})
h.Write(keptTail[len(keptHead):])
h.Write([]byte{'\n'})
return [32]byte(h.Sum(nil)), true
}