You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
stats: Re-use objects while calling multiple Handlers (#8639)
This PR improves performance by eliminating heap allocations when
multiple stats handlers are configured.
Previously, iterating through a list of handlers caused one heap
allocation per handler for each RPC. This change introduces a Handler
that combines multiple Handlers and implements the `Handler` interface.
The combined handler delegates calls to the handlers it contains.
This approach allows gRPC clients and servers to operate as if there
were only a single `Handler` registered, simplifying the internal logic
and removing the per-RPC allocation overhead. To avoid any performance
impact when stats are disabled, the combined `Handler` is only created
when at least one handler is registered.
# Tested
Since existing benchmarks don't register stats handler, I modified the
benchmark to add 2 stats handlers each on the server and client
(36ba616).
```sh
# test command
go run benchmark/benchmain/main.go -benchtime=60s -workloads=unary \
-compression=off -maxConcurrentCalls=200 -trace=off \
-reqSizeBytes=100 -respSizeBytes=100 -networkMode=Local -resultFile="${RUN_NAME}"
# results
go run benchmark/benchresult/main.go unary-before unary-after
Title Before After Percentage
TotalOps 7336128 7638892 4.13%
SendOps 0 0 NaN%
RecvOps 0 0 NaN%
Bytes/op 12382.19 11467.03 -7.39%
Allocs/op 173.93 165.91 -4.60%
ReqT/op 97815040.00 101851893.33 4.13%
RespT/op 97815040.00 101851893.33 4.13%
50th-Lat 1.463345ms 1.403011ms -4.12%
90th-Lat 2.557136ms 2.46828ms -3.47%
99th-Lat 3.073264ms 3.080081ms 0.22%
Avg-Lat 1.634153ms 1.569391ms -3.96%
GoVersion go1.24.7 go1.24.7
GrpcVersion 1.77.0-dev 1.77.0-dev
```
RELEASE NOTES:
* stats: Reduce heap allocations when multiple stats Handlers are
registered.
0 commit comments