sfu: return VP8 munged header by value to avoid per-packet allocation (#4871)

CodecMunger.UpdateAndGet returned a freshly allocated slice for every
forwarded packet on every down track. Return the header in a fixed array
with a length instead and marshal into it with MarshalTo. TranslationParams
carries the array; WriteRTP slices it locally.

UpdateAndGet now allocates zero times per call (was one). Escape analysis
confirms TranslationParams stays on the stack.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Raja Subramanian
2026-09-15 21:34:10 +05:30
committed by GitHub
co-authored by Claude Fable 5.1
parent 08c5433449
commit 0392ca90b4
7 changed files with 66 additions and 40 deletions
+9 -2
View File
@@ -201,12 +201,18 @@ type TranslationParams struct {
rtp TranslationParamsRTP
ddBytes []byte
incomingHeaderSize int
codecBytes []byte
codecBytes [codecmunger.MaxHeaderSize]byte
codecBytesLen int
marker bool
// end of the svc spatial layer frame
isEndOfLayerFrame bool
}
// codecHeader returns the munged codec header to prepend to the payload.
func (tp *TranslationParams) codecHeader() []byte {
return tp.codecBytes[:tp.codecBytesLen]
}
// -------------------------------------------------------------------
type refInfo struct {
@@ -2223,7 +2229,7 @@ func (f *Forwarder) getTranslationParamsVideo(extPkt *buffer.ExtPacket, layer in
func (f *Forwarder) translateCodecHeader(extPkt *buffer.ExtPacket, tp *TranslationParams) error {
// codec specific forwarding check and any needed packet munging
tl := f.vls.SelectTemporal(extPkt)
inputSize, codecBytes, err := f.codecMunger.UpdateAndGet(
inputSize, codecBytes, codecBytesLen, err := f.codecMunger.UpdateAndGet(
extPkt,
tp.rtp.snOrdering == SequenceNumberOrderingOutOfOrder,
tp.rtp.snOrdering == SequenceNumberOrderingGap,
@@ -2243,6 +2249,7 @@ func (f *Forwarder) translateCodecHeader(extPkt *buffer.ExtPacket, tp *Translati
}
tp.incomingHeaderSize = inputSize
tp.codecBytes = codecBytes
tp.codecBytesLen = codecBytesLen
return nil
}