refactor: convert IP addresses to text without allocating - #69
Merged
Conversation
The formatter went through ToString, which allocated a string per formatted address -- including IPv4, since it sized the buffer to 16 and stepped past the small-string capacity. ToChars now writes into a caller-provided buffer and returns a view of it; ToString keeps the owning form for callers that need one. raw_socket passed ToString into log calls whose formatter already handles IpAddress, so those pass the address itself now. IPv6 prints bracketed there as a result, matching every other site.
Three changes have now rested on formatting staying off the heap with nothing enforcing it. ScopedAllocationCounter counts allocations through the sanitizer's malloc hooks -- counting each call, not watching allocated bytes, because a string built and freed inside the window nets to zero and is exactly what should fail. Sanitized builds only, so the test is guarded to match; the Debug gate always qualifies.
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.
IpAddress's formatter went throughToString, allocating a string per formatted address — including IPv4, which sized its buffer to 16 and stepped just past libstdc++'s small-string capacity.ToCharswrites into a caller-provided buffer and returns a view of it;ToStringstays for the one caller that needs an owning string.Nine
raw_socketlog sites passedToStringinto a formatter that already handlesIpAddress, so they pass the address itself now — the rest of the codebase, including the test util, already did. Behaviour note: IPv6 prints bracketed in those nine lines as a result ([ff02::c]), matching every other site.MacAddressneeded no change — it has noToStringand its formatter always wrote straight to the output iterator.Enforcing it
Three changes now rest on formatting staying off the heap, with nothing checking it.
ScopedAllocationCountercounts allocations via the sanitizer's malloc hooks. Two earlier designs were discarded for concrete reasons worth recording:operator newcollided with ASan's ownnothrow/aligned overloads (alloc-dealloc-mismatch, whole suite down);__sanitizer_get_current_allocated_bytesis a net figure, so a string built and freed inside the window reads as zero — it passed with the regression deliberately reinstated.The hook version catches it: 1 allocation with
ToStringrestored in the formatter, 0 withToChars. It needs a long IPv6 literal, sinceff02::cfits libc++'s SSO and never allocated either way.Sanitizer-gated, so the test is
#if-guarded to match.Native (893), docker Debug (880) and docker Release (879, test correctly excluded) all green.