Skip to content

Commit 8aa7429

Browse files
rpc: remove 1 sprintf from hot-path (#19471)
remove 1 sprintf for metrics label creation from hot-path
1 parent 0adcd1f commit 8aa7429

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

rpc/handler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,10 @@ func (h *handler) handleCall(cp *callProc, msg *jsonrpcMessage, stream jsonstrea
575575
rpcRequestGauge.Inc()
576576
if answer != nil && answer.Error != nil {
577577
failedReqeustGauge.Inc()
578+
callb.timerFailure.ObserveDuration(start)
579+
} else {
580+
callb.timerSuccess.ObserveDuration(start)
578581
}
579-
newRPCServingTimerMS(msg.Method, answer == nil || answer.Error == nil).ObserveDuration(start)
580582
}
581583
return answer
582584
}

rpc/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ func (msg *jsonrpcMessage) isUnsubscribe() bool {
9393
}
9494

9595
func (msg *jsonrpcMessage) namespace() string {
96-
elem := strings.SplitN(msg.Method, serviceMethodSeparator, 2)
97-
return elem[0]
96+
ns, _, _ := strings.Cut(msg.Method, serviceMethodSeparator)
97+
return ns
9898
}
9999

100100
func (msg *jsonrpcMessage) String() string {

rpc/service.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6973
func (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.
104113
func (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

Comments
 (0)