Use sync.Pool for objects in packet path. (#4066)
* Use sync.Pool for objects in packet path. Seeing cases of forwarding latency spikes that aling with GC. This might be a bit overkill, but using sync.Pool for small + short-lived objects in packet path. Before this, all these were increasing in alloc_space heap profile samples over time. With these, there is no increase (actually the lines corresponding to geting from pool does not even show up in heap accounting when doing `list` in `pprof`) * merge * Paul feedback
This commit is contained in:
+13
-2
@@ -20,6 +20,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/bwe"
|
||||
"github.com/livekit/mediatransportutil"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils/mono"
|
||||
"github.com/pion/rtp"
|
||||
@@ -56,9 +57,17 @@ func (b *Base) TimeSinceLastSentPacket() time.Duration {
|
||||
|
||||
func (b *Base) SendPacket(p *Packet) (int, error) {
|
||||
defer func() {
|
||||
if p.HeaderPool != nil && p.Header != nil {
|
||||
*p.Header = rtp.Header{}
|
||||
p.HeaderPool.Put(p.Header)
|
||||
}
|
||||
|
||||
if p.Pool != nil && p.PoolEntity != nil {
|
||||
p.Pool.Put(p.PoolEntity)
|
||||
}
|
||||
|
||||
*p = Packet{}
|
||||
PacketFactory.Put(p)
|
||||
}()
|
||||
|
||||
err := b.patchRTPHeaderExtensions(p)
|
||||
@@ -83,8 +92,10 @@ func (b *Base) SendPacket(p *Packet) (int, error) {
|
||||
func (b *Base) patchRTPHeaderExtensions(p *Packet) error {
|
||||
sendingAt := mono.Now()
|
||||
if p.AbsSendTimeExtID != 0 {
|
||||
absSendTime := rtp.NewAbsSendTimeExtension(sendingAt)
|
||||
absSendTimeBytes, err := absSendTime.Marshal()
|
||||
absSendTimeExt := rtp.AbsSendTimeExtension{
|
||||
Timestamp: uint64(mediatransportutil.ToNtpTime(sendingAt) >> 14),
|
||||
}
|
||||
absSendTimeBytes, err := absSendTimeExt.Marshal()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -23,6 +23,16 @@ import (
|
||||
"github.com/pion/webrtc/v4"
|
||||
)
|
||||
|
||||
var (
|
||||
PacketFactory = &sync.Pool{
|
||||
New: func() any {
|
||||
return &Packet{}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// --------------------------------------
|
||||
|
||||
type PacerBehavior string
|
||||
|
||||
const (
|
||||
@@ -33,6 +43,7 @@ const (
|
||||
|
||||
type Packet struct {
|
||||
Header *rtp.Header
|
||||
HeaderPool *sync.Pool
|
||||
HeaderSize int
|
||||
Payload []byte
|
||||
IsRTX bool
|
||||
|
||||
Reference in New Issue
Block a user