The gateway's projection of all time (gatewayProjection) was built
again from every call in the log whenever the log grew, every poll with
calls coming in: buildGatewaySessions was ~8% of CPU on the #2278 bench.
It is now counted on from the sessions its full blocks counted, which
never change: a session the new blocks touch is copied from the prefix
and counted on, the rest are reused as they are, and the list is sorted
again. The prefix is kept per price meta (usagePriceCacheKey, which
holds aliases and clients) and zone, and dropped when the log is read
afresh. Ranges and native exclusions still read out of the projection,
and buildGatewaySessions stays for a since.
finishGatewayModels now sorts a session's models before it sums their
cost, so the sum has one order however the models were gathered (it
was map order before).
TestGatewaySessionsCountedOnAreCountedAfresh DeepEquals every session
counted on with buildGatewaySessions afresh, after appends over a
block's edge, a new session and a call older than the rest, a zone
change and the log written over, and checks it was counted on. Broken
by hand (a prefix session's days dropped): red at "first".
Run at this tree on mote: go test -count=1 ./internal/usage/...
./internal/usageapi/... ./internal/tui/... ok; ./internal/gui/ red only
on TestMainWindowCorners/macOS_26_SDK (#3949, open); GOOS=linux,windows,
darwin go vet -tags nogui ./internal/usage/... ./internal/gui/ ok.
The #2278 bench on mote (15G .codex, 10 endpoints polled every 5s, 4
calls/s appended, 240s), v0.1.1966 then this tree, back to back:
- CPU profile 60s: 24.17s -> 20.83s samples; buildGatewaySessions
(1.07s) and buildVias (0.79s) gone
- poll mean 0.57s -> 0.51s; /api/sessions 1.47 -> 1.04s, overview 0.93
-> 0.63s, stats 0.49-0.52 -> 0.26-0.32s, heatmap 1.48 -> 1.19s
- cpu after feed 43.6% -> 41.7%; RSS peak 982 -> 1125 MiB, footprint
peak 742.5 -> 744.1M (the cold-start peak swings run to run; not this)
Not tried: a real account's log (the bench log is generated).
Refs #2278