Skip to content

Commit f71345c

Browse files
authored
Do not format expensive debug messages in non-debug levels in doDropRPC (#580)
In high load scenarios when consumer is slow, `doDropRPC` is called often and makes extra unnecessary allocations formatting `log.Debug` message. Fixed by checking log level before running expensive formatting. Before: ``` BenchmarkAllocDoDropRPC-10 13684732 76.28 ns/op 144 B/op 3 allocs/op ``` After: ``` BenchmarkAllocDoDropRPC-10 28140273 42.88 ns/op 112 B/op 1 allocs/op ```
1 parent 4c13974 commit f71345c

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/libp2p/go-msgio v0.3.0
1313
github.com/multiformats/go-multiaddr v0.13.0
1414
github.com/multiformats/go-varint v0.0.7
15+
go.uber.org/zap v1.27.0
1516
)
1617

1718
require (
@@ -98,7 +99,6 @@ require (
9899
go.uber.org/fx v1.22.2 // indirect
99100
go.uber.org/mock v0.4.0 // indirect
100101
go.uber.org/multierr v1.11.0 // indirect
101-
go.uber.org/zap v1.27.0 // indirect
102102
golang.org/x/crypto v0.26.0 // indirect
103103
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
104104
golang.org/x/mod v0.20.0 // indirect

gossipsub.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"github.com/libp2p/go-libp2p/core/protocol"
2020
"github.com/libp2p/go-libp2p/core/record"
2121
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
22+
23+
"go.uber.org/zap/zapcore"
2224
)
2325

2426
const (
@@ -1334,7 +1336,9 @@ func (gs *GossipSubRouter) sendRPC(p peer.ID, out *RPC, urgent bool) {
13341336
}
13351337

13361338
func (gs *GossipSubRouter) doDropRPC(rpc *RPC, p peer.ID, reason string) {
1337-
log.Debugf("dropping message to peer %s: %s", p, reason)
1339+
if log.Level() <= zapcore.DebugLevel {
1340+
log.Debugf("dropping message to peer %s: %s", p, reason)
1341+
}
13381342
gs.tracer.DropRPC(rpc, p)
13391343
// push control messages that need to be retried
13401344
ctl := rpc.GetControl()

gossipsub_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -3175,3 +3175,11 @@ func TestGossipsubIdontwantClear(t *testing.T) {
31753175

31763176
<-ctx.Done()
31773177
}
3178+
3179+
func BenchmarkAllocDoDropRPC(b *testing.B) {
3180+
gs := GossipSubRouter{tracer: &pubsubTracer{}}
3181+
3182+
for i := 0; i < b.N; i++ {
3183+
gs.doDropRPC(&RPC{}, "peerID", "reason")
3184+
}
3185+
}

0 commit comments

Comments
 (0)