From b78d61e93a8546675d97d953bf9b24a8f44e29b3 Mon Sep 17 00:00:00 2001 From: arjunshajitech Date: Fri, 26 Dec 2025 22:30:57 +0530 Subject: [PATCH] Avoid RTPBuffer allocation Previously, NewInterceptor validated the RTP buffer size by calling rtpbuffer.NewRTPBuffer this resulted in unnecessary memory allocations during interceptor construction, even though the buffer itself was not used at that stage. --- internal/rtpbuffer/rtpbuffer.go | 33 +++++++++++++++++-------------- pkg/nack/generator_interceptor.go | 3 ++- pkg/nack/receive_log.go | 16 ++------------- pkg/nack/responder_interceptor.go | 2 +- 4 files changed, 23 insertions(+), 31 deletions(-) diff --git a/internal/rtpbuffer/rtpbuffer.go b/internal/rtpbuffer/rtpbuffer.go index 94c7adfe..a2f8eb29 100644 --- a/internal/rtpbuffer/rtpbuffer.go +++ b/internal/rtpbuffer/rtpbuffer.go @@ -4,9 +4,7 @@ // Package rtpbuffer provides a buffer for storing RTP packets package rtpbuffer -import ( - "fmt" -) +import "fmt" const ( // Uint16SizeHalf is half of a math.Uint16. @@ -26,25 +24,30 @@ type RTPBuffer struct { // NewRTPBuffer constructs a new RTPBuffer. func NewRTPBuffer(size uint16) (*RTPBuffer, error) { - allowedSizes := make([]uint16, 0) - correctSize := false + if err := IsBufferSizeValid(size); err != nil { + return nil, err + } + + return &RTPBuffer{ + packets: make([]*RetainablePacket, size), + size: size, + }, nil +} + +func IsBufferSizeValid(size uint16) error { for i := 0; i < 16; i++ { if size == 1<