perf(core): add state_str() to UDP DPI enums, drop per-call heap allocs in Connection::state()#418
Closed
obchain wants to merge 1 commit into
Closed
perf(core): add state_str() to UDP DPI enums, drop per-call heap allocs in Connection::state()#418obchain wants to merge 1 commit into
obchain wants to merge 1 commit into
Conversation
This was referenced Jun 25, 2026
Owner
|
Thanks for the detailed write-up and tests. For |
…cs in Connection::state()
Five branches in Connection::state() built their return value with
Cow::Owned(format!("PREFIX_{}", info.field)), allocating on the heap
every time state() was called — i.e. every render frame for each
DHCP, SNMP, SSDP, NetBIOS, or STUN connection.
All five enums have a finite, closed set of known variants whose
prefixed state strings are compile-time constants. Add state_str()
methods that return those constants directly:
DhcpMessageType::state_str() -> Cow<'static, str>
Known variants: Cow::Borrowed("DHCP_DISCOVER") etc.
Unknown(v): Cow::Owned(format!("DHCP_UNKNOWN({})", v))
SnmpPduType::state_str() -> &'static str (9 static variants)
SsdpMethod::state_str() -> &'static str (3 static variants)
NetBiosService::state_str() -> &'static str (2 static variants)
StunMessageClass::state_str() -> &'static str (4 static variants)
Connection::state() now calls these methods and wraps in
Cow::Borrowed where possible, eliminating the format! call entirely
for all known variants.
Five new tests verify that known variants produce the correct
prefixed strings and, for DhcpMessageType, that they are Borrowed;
Unknown(42) produces Owned("DHCP_UNKNOWN(42)").
f5166ad to
fd26c28
Compare
Contributor
Author
|
I profiled this under live traffic (samply, 60s). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #417
What
Connection::state()is called every render frame for every visible connection. Five branches were building their return value withCow::Owned(format!("PREFIX_{}", info.field)), allocating aStringon the heap each time:All five enums have a small, closed set of known variants. This PR adds
state_str()methods that return the full prefixed string as a compile-time constant, then updatesConnection::state()to useCow::Borrowedinstead:New methods
DhcpMessageTypeCow<'static, str>Unknown(u8)"DHCP_DISCOVER"SnmpPduType&'static str"SNMP_GET"SsdpMethod&'static str"SSDP_M-SEARCH"NetBiosService&'static str"NETBIOS_NS"StunMessageClass&'static str"STUN_Request"DhcpMessageType::Unknown(u8)retains aCow::Ownedallocation since the byte value is runtime data — all other variants areCow::Borrowed.Impact
For each frame with DHCP/SNMP/SSDP/NetBIOS/STUN connections visible: −1 heap alloc per connection per
state()call. In a busy network with 50 such connections at 30 fps that is 1,500 allocations/second eliminated.Tests
Five new unit tests:
dhcp_state_str_known_variants_are_borrowed— assertsCow::Borrowedfor all 8 known variants andCow::OwnedforUnknown(42)snmp_state_str_matches_prefixed_display— each variant matchesformat!("SNMP_{}", variant)ssdp_state_str_matches_prefixed_displaynetbios_state_str_matches_prefixed_displaystun_state_str_matches_prefixed_displayVerification