Move defer pkt.DecRef() to outside of the dispatch loop.

This avoids unnecessary allocations.

PiperOrigin-RevId: 409473262
This commit is contained in:
Lucas Manning
2021-11-12 12:20:56 -08:00
committed by gVisor bot
parent 49e80a1440
commit ed3ac3a84d
+5 -2
View File
@@ -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