Avoid heap allocation of packetInfo in TWCC feedback processing loop

perfloop/livekit · ALLOCATION HOT LOOP

https://perfloop.ai/t/oss/case_5b02c9h363

Verdict

VERIFIED · settled 2026-06-24

What happened: Passing the small read-only packetInfo struct by value instead of pointer in the TWCC feedback processing loop avoids heap escape analysis moving it to the heap on every packet status processed. Measurements prove that this completely eliminates 100 heap allocations per feedback report (reducing allocs/op by 90.9% from 110 to 10) and accelerates processing time by 38.7% (from 11.7us to 7.2us).

Hypothesis

During TWCC feedback processing in 'HandleTWCCFeedback', the local 'packetInfo' variable (returned by value from 'RecordPacketIndicationFromRemote') has its address taken and is passed by pointer to 'trackPacketGroup', which subsequently passes it to 'packetGroup.Add' and 'updateCTRTrend'. Go escape analysis ('go build -gcflags=-m') confirms that taking the address of 'pi' causes it to move/escape to the heap on every packet status symbol processed. Since 'packetInfo' is read-only in these receivers, passing it by value avoids the heap escape entirely, eliminating garbage collection pressure and CPU overhead on this high-frequency media routing hot path. This anchor reaches the hot path across an interface dispatch with 3 runtime implementations; the index assumes, but does not prove, that runtime binding reaches this implementation.

Change to test: Pass packetInfo by value instead of pointer in the packet tracker, packet group, and trend update functions.

Where it lives

perfloop/livekit · pkg/sfu/pacer/no_queue.go

Evidence

Timeline