Reuse DD writer scaffolding to cut per-packet marshal allocations

perfloop/livekit · ALLOCATION HOT LOOP

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

Verdict

VERIFIED · settled 2026-08-17

What happened: The paired measurements met the required improvement.

Hypothesis

On the per-packet per-subscriber video forwarding path (DownTrack.getTranslationParamsVideo -> videolayerselector.DependencyDescriptor.Select -> ddExtension.Marshal), MarshalWithActiveChains allocates a *DependencyDescriptorWriter, two *BitStreamWriter structs, and a returned []byte = 4 allocs/call for AV1/VP9 dependency-descriptor streams. Three of the four are pure internal scaffolding re-created on every packet; only the returned []byte must escape. Reusing the scaffolding measured 4->1 allocs/op on the per-packet forwarding workload.

Change to test: Reuse one DependencyDescriptorWriter and BitStreamWriter struct per selector in place under the forwarder lock, eliminating the 3 scaffolding allocations; keep the returned []byte a fresh allocation because it escapes into the forward result.

Where it lives

perfloop/livekit · pkg/sfu/receiver_base.go

Evidence

captured dependency-descriptor marshal with all active chains and runtime-varied frame numbers · 10 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 4 1 −75% (−3) −3 to −3 < −2 PASSED
B/op 163 3 −98.2% (−160) −160 to −160 ≤ 0 PASSED
ns/op 810.7 682.6 −15% (−121.6) −173.1 to −103.3 ≤ 0 PASSED

steady-state AV1 dependency-descriptor subscriber forwarding with active decode targets · 10 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 8 5 −37.5% (−3) −3 to −3 < −2 PASSED
B/op 288 128 −55.6% (−160) −160 to −160 ≤ 0 PASSED
ns/op 1477 1331 −9% (−133) −171 to −115 ≤ 0 PASSED

steady-state VP9 dependency-descriptor subscriber forwarding with active decode targets · 10 sample pairs

metric baseline candidate paired median change confidence range required result
allocs/op 8 5 −37.5% (−3) −3 to −3 < −2 PASSED
B/op 288 128 −55.6% (−160) −160 to −160 ≤ 0 PASSED
ns/op 1470 1339 −8.8% (−129) −174 to −116 ≤ 0 PASSED

Checks: 2 of 2 passed. Verification: no defect found.

Timeline