Skip to content

ICE: avoid heap allocation per TURN Data Indication - #478

Closed
smhussa22 wants to merge 1 commit into
ValveSoftware:masterfrom
smhussa22:ice-avoid-heap-alloc-stun-attrs
Closed

smhussa22 wants to merge 1 commit into
ValveSoftware:masterfrom
smhussa22:ice-avoid-heap-alloc-stun-attrs

Conversation

@smhussa22

Copy link
Copy Markdown

WriteGenericSTUNAttribute's caller on the TURN Data Indication path built a CUtlVector<STUNAttribute> just to find the XOR-PEER-ADDRESS and DATA attributes, heap-allocating on every relayed packet. This walks the attributes directly instead, capturing the first match of each, and stops at the same early-out points ParseSTUNAttributes uses when called without a key (bad fingerprint, or any message-integrity attribute). The accept/reject behavior is unchanged; other vector-based call sites in this file are untouched.

Verification

Correctness — test_crypto and test_p2p.py pass, including the TURN-relay-specific cases (symmetric NAT (requires TURN relay), relay via LAN TURN, TURN allocation refresh), run 3x clean with no flakes under ASan+UBSan.

The actual fix — measured directly, not just asserted. I added a scratch benchmark hook that calls the real OnPacketReceived in-process with one synthetic TURN Data Indication packet, N times, under a process-wide malloc/calloc/realloc counter, built identically against master and against this branch:

Build Iterations Heap allocations Per call
master 20,000 / 100,000 20,000 / 100,000 1.0000
this branch 20,000 / 100,000 0 / 0 0.0000

Exactly 1 allocation/call on master (the vector growing from empty), exactly 0 on this branch, at both scales.

The TURN Data Indication handler built a CUtlVector of every STUN
attribute just to find the XOR-PEER-ADDRESS and DATA attributes, which
allocates on every relayed packet. Walk the attributes directly instead,
capturing the first match of each, and stop at the same early-out points
ParseSTUNAttributes uses when called without a key (bad fingerprint, or
any message-integrity attribute). The result is unchanged; the other
vector-based call sites are untouched.
@smhussa22 smhussa22 closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant