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:
Raja Subramanian
2025-11-14 16:13:23 +05:30
committed by GitHub
parent f8b994d491
commit c3964ba2eb
8 changed files with 125 additions and 30 deletions
+21 -1
View File
@@ -46,6 +46,16 @@ import (
"github.com/livekit/protocol/utils/mono"
)
var (
ExtPacketFactory = &sync.Pool{
New: func() any {
return &ExtPacket{}
},
}
)
// --------------------------------------
const (
ReportDelta = 1e9
@@ -543,6 +553,15 @@ func (b *Buffer) ReadExtended(buf []byte) (*ExtPacket, error) {
}
}
func (b *Buffer) ReleaseExtPacket(extPkt *ExtPacket) {
if b.ddParser != nil {
b.ddParser.ReleaseExtDependencyDescriptor(extPkt.DependencyDescriptor)
}
*extPkt = ExtPacket{}
ExtPacketFactory.Put(extPkt)
}
func (b *Buffer) Close() error {
b.closeOnce.Do(func() {
b.closed.Store(true)
@@ -903,7 +922,8 @@ func (b *Buffer) processHeaderExtensions(p *rtp.Packet, arrivalTime int64, isRTX
}
func (b *Buffer) getExtPacket(rtpPacket *rtp.Packet, arrivalTime int64, isBuffered bool, flowState rtpstats.RTPFlowState) *ExtPacket {
ep := &ExtPacket{
ep := ExtPacketFactory.Get().(*ExtPacket)
*ep = ExtPacket{
Arrival: arrivalTime,
ExtSequenceNumber: flowState.ExtSequenceNumber,
ExtTimestamp: flowState.ExtTimestamp,
+9
View File
@@ -433,3 +433,12 @@ func BenchmarkMemcpu(b *testing.B) {
copy(buf2, buf)
}
}
func BenchmarkExtPacketFactory(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
extPkt := ExtPacketFactory.Get().(*ExtPacket)
*extPkt = ExtPacket{}
ExtPacketFactory.Put(extPkt)
}
}
+22 -1
View File
@@ -17,6 +17,7 @@ package buffer
import (
"fmt"
"sort"
"sync"
"time"
"github.com/pion/rtp"
@@ -28,6 +29,16 @@ import (
"github.com/livekit/protocol/logger"
)
var (
ExtDependencyDescriptorFactory = &sync.Pool{
New: func() any {
return &ExtDependencyDescriptor{}
},
}
)
// --------------------------------------
const (
ddRestartThreshold = 30 * time.Second
@@ -156,7 +167,8 @@ func (r *DependencyDescriptorParser) Parse(pkt *rtp.Packet) (*ExtDependencyDescr
r.frameChecker.AddPacket(extSeq, extFN, &ddVal)
extDD := &ExtDependencyDescriptor{
extDD := ExtDependencyDescriptorFactory.Get().(*ExtDependencyDescriptor)
*extDD = ExtDependencyDescriptor{
Descriptor: &ddVal,
ExtFrameNum: extFN,
Integrity: r.frameChecker.FrameIntegrity(extFN),
@@ -235,6 +247,15 @@ func (r *DependencyDescriptorParser) Parse(pkt *rtp.Packet) (*ExtDependencyDescr
return extDD, videoLayer, nil
}
func (r *DependencyDescriptorParser) ReleaseExtDependencyDescriptor(extDD *ExtDependencyDescriptor) {
if extDD == nil {
return
}
*extDD = ExtDependencyDescriptor{}
ExtDependencyDescriptorFactory.Put(extDD)
}
func (r *DependencyDescriptorParser) restart() {
r.frameChecker = NewFrameIntegrityChecker(integrityCheckFrame, integrityCheckPkt)
r.structure = nil