derp/derpserver: don't build debug log arguments on the packet path

sclient.debugLogf and Server.debugLogf check a debug flag before
logging, but Go evaluates and boxes their arguments before the call.
The per-packet call sites in run, handleFrameSendPacket,
handleFrameForwardPacket, sendPkt, recordDrop, and sendPacket's
deferred stats func were therefore calling key.NodePublic.ShortString
and boxing frame headers on every relayed packet, all for messages
that were then discarded.

On one busy server's heap profile, those discarded arguments were
about half of all objects allocated by the process. Guard each hot
call site with the debug flag so nothing is built unless it will be
logged, and document that requirement on both debugLogf methods.

While here, give the sender cardinality sketch its key bytes from a
stack array rather than an AppendTo(nil) allocation per packet.

BenchmarkSendRecv drops from 10 or 11 allocations per relayed packet
to 3, and BenchmarkConcurrentStreams from 11 to 4.

Updates #21064

Change-Id: Ibb7c4fbff546b6f1b40ce0a21d41ae0976705941
Signed-off-by: Brad Fitzpatrick <[email protected]>
This commit is contained in:
Brad Fitzpatrick authored and Brad Fitzpatrick committed 2026-09-18 06:52:51 -07:00
1 parent 1033e714ca
commit 47debc5a6f
1 file changed
+32 -9
+32 -9
View File
@@ -1119,6 +1119,9 @@ func (s *Server) accept(ctx context.Context, nc derp.Conn, brw *bufio.ReadWriter
return c.run(ctx)
}
// debugLogf logs only when the server has debug logging enabled.
// Callers on the per-packet path must check s.debug themselves first,
// since Go evaluates and boxes the arguments before the call.
func (s *Server) debugLogf(format string, v ...any) {
if s.debug {
s.logf(format, v...)
@@ -1155,7 +1158,9 @@ func (c *sclient) run(ctx context.Context) error {
for {
ft, fl, err := derp.ReadFrameHeader(c.br)
c.debugLogf("read frame type %d len %d err %v", ft, fl, err)
if c.debug {
c.debugLogf("read frame type %d len %d err %v", ft, fl, err)
}
if err != nil {
if errors.Is(err, io.EOF) {
c.debugLogf("read EOF")
@@ -1315,7 +1320,9 @@ func (c *sclient) handleFrameForwardPacket(_ derp.FrameType, fl uint32) error {
return nil
}
dst.debugLogf("received forwarded packet from %s via %s", srcKey.ShortString(), c.key.ShortString())
if dst.debug {
dst.debugLogf("received forwarded packet from %s via %s", srcKey.ShortString(), c.key.ShortString())
}
return c.sendPkt(dst, pkt{
bs: contents,
@@ -1373,7 +1380,9 @@ func (c *sclient) handleFrameSendPacket(_ derp.FrameType, fl uint32) error {
if fwd != nil {
s.packetsForwardedOut.Add(1)
err := fwd.ForwardPacket(c.key, dstKey, contents)
c.debugLogf("SendPacket for %s, forwarding via %s: %v", dstKey.ShortString(), fwd, err)
if c.debug {
c.debugLogf("SendPacket for %s, forwarding via %s: %v", dstKey.ShortString(), fwd, err)
}
if err != nil {
// TODO:
return nil
@@ -1387,10 +1396,14 @@ func (c *sclient) handleFrameSendPacket(_ derp.FrameType, fl uint32) error {
c.requestPeerGoneWriteLimited(dstKey, contents, derp.PeerGoneReasonNotHere)
}
s.recordDrop(contents, c.key, dstKey, reason)
c.debugLogf("SendPacket for %s, dropping with reason=%s", dstKey.ShortString(), reason)
if c.debug {
c.debugLogf("SendPacket for %s, dropping with reason=%s", dstKey.ShortString(), reason)
}
return nil
}
c.debugLogf("SendPacket for %s, sending directly", dstKey.ShortString())
if c.debug {
c.debugLogf("SendPacket for %s, sending directly", dstKey.ShortString())
}
p := pkt{
bs: contents,
@@ -1474,6 +1487,9 @@ func (c *sclient) rateLimit(n int) error {
return nil
}
// debugLogf logs only when the client has debug logging enabled.
// Callers on the per-packet path must check c.debug themselves first,
// since Go evaluates and boxes the arguments before the call.
func (c *sclient) debugLogf(format string, v ...any) {
if c.debug {
c.logf(format, v...)
@@ -1519,7 +1535,9 @@ func (s *Server) recordDrop(packetBytes []byte, srcKey, dstKey key.NodePublic, r
msg := fmt.Sprintf("drop (%s) %s -> %s", srcKey.ShortString(), reason, dstKey.ShortString())
s.limitedLogf(msg)
}
s.debugLogf("dropping packet reason=%s dst=%s disco=%v", reason, dstKey, looksDisco)
if s.debug {
s.debugLogf("dropping packet reason=%s dst=%s disco=%v", reason, dstKey, looksDisco)
}
}
func (c *sclient) sendPkt(dst *sclient, p pkt) error {
@@ -1539,7 +1557,9 @@ func (c *sclient) sendPkt(dst *sclient, p pkt) error {
reason = dropReasonQueueTail
}
s.recordDrop(p.bs, c.key, dstKey, reason)
dst.debugLogf("sendPkt dropped, reason=%s", reason)
if dst.debug {
dst.debugLogf("sendPkt dropped, reason=%s", reason)
}
return nil
}
if dropped.bs != nil {
@@ -2372,7 +2392,9 @@ func (c *sclient) sendPacket(srcKey key.NodePublic, contents []byte) (err error)
c.s.packetsSent.Add(1)
c.s.bytesSent.Add(int64(len(contents)))
}
c.debugLogf("sendPacket from %s: %v", srcKey.ShortString(), err)
if c.debug {
c.debugLogf("sendPacket from %s: %v", srcKey.ShortString(), err)
}
}()
c.setWriteDeadline()
@@ -2384,7 +2406,8 @@ func (c *sclient) sendPacket(srcKey key.NodePublic, contents []byte) (err error)
c.noteSendFromSrc(srcKey)
if c.senderCardinality != nil {
c.senderCardinalityMu.Lock()
c.senderCardinality.Insert(srcKey.AppendTo(nil))
var raw [key.NodePublicRawLen]byte
c.senderCardinality.Insert(srcKey.AppendTo(raw[:0]))
c.senderCardinalityMu.Unlock()
}
}