@@ -30,6 +30,7 @@ import (
3030
3131 "github.com/erigontech/erigon/common/dbg"
3232 "github.com/erigontech/erigon/common/log/v3"
33+ "github.com/erigontech/erigon/diagnostics/metrics"
3334 "github.com/erigontech/erigon/rpc/jsonstream"
3435)
3536
@@ -64,6 +65,9 @@ type callback struct {
6465 isSubscribe bool // true if this is a subscription callback
6566 streamable bool // support JSON streaming (more efficient for large responses)
6667 logger log.Logger
68+
69+ timerSuccess metrics.Summary // pre-cached success timer, avoids per-call GetOrCreateSummary
70+ timerFailure metrics.Summary // pre-cached failure timer
6771}
6872
6973func (r * serviceRegistry ) registerName (name string , rcvr any ) error {
@@ -90,25 +94,30 @@ func (r *serviceRegistry) registerName(name string, rcvr any) error {
9094 }
9195 r .services [name ] = svc
9296 }
93- for name , cb := range callbacks {
97+ for shortName , cb := range callbacks {
98+ // Pre-cache metrics timers using the full "namespace_method" name so the
99+ // hot call path never needs to call GetOrCreateSummary or fmt.Sprintf.
100+ fullMethod := name + serviceMethodSeparator + shortName
101+ cb .timerSuccess = newRPCServingTimerMS (fullMethod , true )
102+ cb .timerFailure = newRPCServingTimerMS (fullMethod , false )
94103 if cb .isSubscribe {
95- svc .subscriptions [name ] = cb
104+ svc .subscriptions [shortName ] = cb
96105 } else {
97- svc .callbacks [name ] = cb
106+ svc .callbacks [shortName ] = cb
98107 }
99108 }
100109 return nil
101110}
102111
103112// callback returns the callback corresponding to the given RPC method name.
104113func (r * serviceRegistry ) callback (method string ) * callback {
105- elem := strings .SplitN (method , serviceMethodSeparator , 2 )
106- if len ( elem ) != 2 {
114+ svc , name , ok := strings .Cut (method , serviceMethodSeparator )
115+ if ! ok {
107116 return nil
108117 }
109118 r .mu .Lock ()
110119 defer r .mu .Unlock ()
111- return r .services [elem [ 0 ]] .callbacks [elem [ 1 ] ]
120+ return r .services [svc ] .callbacks [name ]
112121}
113122
114123// subscription returns a subscription callback in the given service.
0 commit comments