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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user