From 411412df7f20ff90bf28ec3f8cf259ceabc3384c Mon Sep 17 00:00:00 2001 From: ypothoma Date: Tue, 24 Oct 2023 01:34:54 -0700 Subject: [PATCH] Reducing unnecessary allocations --- pkg/flexfec/flexfec_coverage.go | 9 +++++++++ pkg/flexfec/flexfec_encoder.go | 23 +++++++++++++++-------- pkg/flexfec/util/bitarray.go | 8 ++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/pkg/flexfec/flexfec_coverage.go b/pkg/flexfec/flexfec_coverage.go index d3d27299..6fc6eb63 100644 --- a/pkg/flexfec/flexfec_coverage.go +++ b/pkg/flexfec/flexfec_coverage.go @@ -57,6 +57,15 @@ func NewCoverage(mediaPackets []rtp.Packet, numFecPackets uint32) *ProtectionCov } } +func (p *ProtectionCoverage) ResetCoverage() { + for i := uint32(0); i < MaxFecPackets; i++ { + for j := uint32(0); j < MaxMediaPackets; j++ { + p.packetMasks[i].SetBit(j, 0) + } + } + +} + func (p *ProtectionCoverage) GetCoveredBy(fecPacketIndex uint32) *util.MediaPacketIterator { coverage := make([]uint32, 0, p.numMediaPackets) for mediaPacketIndex := uint32(0); mediaPacketIndex < p.numMediaPackets; mediaPacketIndex++ { diff --git a/pkg/flexfec/flexfec_encoder.go b/pkg/flexfec/flexfec_encoder.go index 3a784655..24c19048 100644 --- a/pkg/flexfec/flexfec_encoder.go +++ b/pkg/flexfec/flexfec_encoder.go @@ -14,6 +14,7 @@ type FlexFecEncoder struct { baseSN uint16 payloadType uint8 ssrc uint32 + coverage *ProtectionCoverage } func NewFlexFecEncoder(baseSN uint16, payloadType uint8, ssrc uint32) *FlexFecEncoder { @@ -21,6 +22,7 @@ func NewFlexFecEncoder(baseSN uint16, payloadType uint8, ssrc uint32) *FlexFecEn baseSN: baseSN, payloadType: payloadType, ssrc: ssrc, + coverage: nil, } } @@ -28,28 +30,33 @@ func NewFlexFecEncoder(baseSN uint16, payloadType uint8, ssrc uint32) *FlexFecEn // Encoding will be skipped if there are missing Media packets. func (flex *FlexFecEncoder) EncodeFec(mediaPackets []rtp.Packet, numFecPackets uint32) []rtp.Packet { // Start by defining which FEC packets cover which media packets - coverage := NewCoverage(mediaPackets, numFecPackets) + // We transform mediaPackets array into a slice to avoid as many copies as possible. + if flex.coverage == nil { + flex.coverage = NewCoverage(mediaPackets[:], numFecPackets) + } else { + flex.coverage.ResetCoverage() + } - if coverage == nil { + if flex.coverage == nil { return nil } // Generate FEC payloads fecPackets := make([]rtp.Packet, numFecPackets) for fecPacketIndex := uint32(0); fecPacketIndex < numFecPackets; fecPacketIndex++ { - fecPackets[fecPacketIndex] = flex.encodeFlexFecPacket(fecPacketIndex, coverage) + fecPackets[fecPacketIndex] = flex.encodeFlexFecPacket(fecPacketIndex) } return fecPackets } -func (flex *FlexFecEncoder) encodeFlexFecPacket(fecPacketIndex uint32, coverage *ProtectionCoverage) rtp.Packet { - mediaPacketsIt := coverage.GetCoveredBy(fecPacketIndex) +func (flex *FlexFecEncoder) encodeFlexFecPacket(fecPacketIndex uint32) rtp.Packet { + mediaPacketsIt := flex.coverage.GetCoveredBy(fecPacketIndex) flexFecHeader := flex.encodeFlexFecHeader( mediaPacketsIt, - coverage.ExtractMask1(fecPacketIndex), - coverage.ExtractMask2(fecPacketIndex), - coverage.ExtractMask3(fecPacketIndex), + flex.coverage.ExtractMask1(fecPacketIndex), + flex.coverage.ExtractMask2(fecPacketIndex), + flex.coverage.ExtractMask3(fecPacketIndex), ) flexFecRepairPayload := flex.encodeFlexFecRepairPayload(mediaPacketsIt.Reset()) diff --git a/pkg/flexfec/util/bitarray.go b/pkg/flexfec/util/bitarray.go index 4a4ee265..d0c82e31 100644 --- a/pkg/flexfec/util/bitarray.go +++ b/pkg/flexfec/util/bitarray.go @@ -20,7 +20,15 @@ func NewBitArray(sizeBits uint32) BitArray { } func (b *BitArray) SetBit(bitIndex uint32, bitValue uint32) { + byteIndex := bitIndex / 8 + bitOffset := uint(bitIndex % 8) + // Set the specific bit to 1 using bitwise OR + if bitValue == 1 { + b.bytes[byteIndex] |= 1 << bitOffset + } else { + b.bytes[byteIndex] |= 0 << bitOffset + } } func (b *BitArray) GetBit(bitIndex uint32) uint8 {