From ed3ac3a84d5779cf31cb5f29e9033d951f3c7030 Mon Sep 17 00:00:00 2001 From: Lucas Manning Date: Fri, 12 Nov 2021 12:18:18 -0800 Subject: [PATCH] Move `defer pkt.DecRef()` to outside of the dispatch loop. This avoids unnecessary allocations. PiperOrigin-RevId: 409473262 --- pkg/tcpip/link/fdbased/packet_dispatchers.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/tcpip/link/fdbased/packet_dispatchers.go b/pkg/tcpip/link/fdbased/packet_dispatchers.go index c22bba3b5..f3a135441 100644 --- a/pkg/tcpip/link/fdbased/packet_dispatchers.go +++ b/pkg/tcpip/link/fdbased/packet_dispatchers.go @@ -284,13 +284,16 @@ func (d *recvMMsgDispatcher) dispatch() (bool, tcpip.Error) { return false, err } // Process each of received packets. + // Keep a list of packets so we can DecRef outside of the loop. + var pkts stack.PacketBufferList + + defer func() { pkts.DecRef() }() for k := 0; k < nMsgs; k++ { n := int(d.msgHdrs[k].Len) - pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ Data: d.bufs[k].pullViews(n), }) - defer pkt.DecRef() + pkts.PushBack(pkt) // Mark that this iovec has been processed. d.msgHdrs[k].Msg.Iovlen = 0