Skip to content

Commit

Permalink
Use sync.Pool for candidate inbound buffer
Browse files Browse the repository at this point in the history
This commit reduces garbage collection pressure by re-using the buffer
used for reading inbound ICE traffic.

Fixes #737
  • Loading branch information
arjunshajitech authored and Sean-Der committed Oct 28, 2024
1 parent 854fdfd commit 75baa5c
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion candidate_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -212,6 +213,12 @@ func (c *candidateBase) start(a *Agent, conn net.PacketConn, initializedCh <-cha
go c.recvLoop(initializedCh)
}

var bufferPool = sync.Pool{ // nolint:gochecknoglobals
New: func() interface{} {
return make([]byte, receiveMTU)
},
}

func (c *candidateBase) recvLoop(initializedCh <-chan struct{}) {
a := c.agent()

Expand All @@ -223,7 +230,13 @@ func (c *candidateBase) recvLoop(initializedCh <-chan struct{}) {
return
}

buf := make([]byte, receiveMTU)
bufferPoolBuffer := bufferPool.Get()
defer bufferPool.Put(bufferPoolBuffer)
buf, ok := bufferPoolBuffer.([]byte)
if !ok {
return
}

for {
n, srcAddr, err := c.conn.ReadFrom(buf)
if err != nil {
Expand Down

0 comments on commit 75baa5c

Please sign in to comment.