perf: replace format!() allocations in on_headers hot path#1200
Open
FlorentinDUBOIS wants to merge 3 commits intomainfrom
Open
perf: replace format!() allocations in on_headers hot path#1200FlorentinDUBOIS wants to merge 3 commits intomainfrom
FlorentinDUBOIS wants to merge 3 commits intomainfrom
Conversation
Replace heap-allocating format!() calls with write!() into reusable Vec<u8> buffers and itoa for stack-based integer formatting. This eliminates up to 7 String allocations per HTTP request in the header editing callback that profiling showed taking up to 75% of readable(). Changes: - Add itoa dependency for stack-based u16 port formatting - Add write_forwarded_for_by() and write_forwarded_suffix() helpers that build Forwarded header values into a Vec<u8> without format!() - Replace format!() in X-Forwarded-For, Forwarded, Set-Cookie with write!() into reusable hdr_buf + Store::from_vec(take(&mut buf)) - Replace public_port.to_string() with itoa::Buffer for X-Forwarded-Port - Replace val.to_string() with ToOwned::to_owned for sticky cookie - Keep self.id.to_string() for Sozu-Id (Ulid encoding is already optimized with pre-allocated crockford base32 buffer)
- Remove `unsafe { from_utf8_unchecked(...) }.as_bytes()` round-trip
when extending hdr_buf with existing header data — the data is
already `&[u8]`, so the unsafe conversion to `&str` and back was
pointless and introduced gratuitous unsafety.
- Move `hdr_buf` allocation inside `if let Some(peer_addr)` block to
avoid a 128-byte heap allocation when `session_address` is `None`.
- Fix misleading "Reusable buffer" comment — the buffer's ownership
is transferred via `take`, so it is not actually reused.
- Remove unused `from_utf8_unchecked` import.
Signed-off-by: Florentin Dubois <florentin.dubois@clever.cloud>
Align itoa dependency version with workspace convention: "^x.y.z" instead of "^x.y". Signed-off-by: Florentin Dubois <florentin.dubois@clever.cloud>
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.
Summary
format!()allocations withitoa::Buffer,write!()intoVec<u8>, andextend_from_sliceitoaworkspace dependency (zero-dep, stack-based integer formatting)write_forwarded_for_byandwrite_forwarded_suffixhelpers to reduce code duplicationReview fixes applied
unsafe { from_utf8_unchecked(...) }.as_bytes()roundtrip — now uses&[u8]directlyVec::with_capacity(128)allocation into conditional scope (was allocating on every request even withoutsession_address)Closes #1140
Test plan
cargo test --workspacepassescargo clippy --workspaceclean