Skip to content

perf: replace format!() allocations in on_headers hot path#1200

Open
FlorentinDUBOIS wants to merge 3 commits intomainfrom
perf/on-headers-optimization
Open

perf: replace format!() allocations in on_headers hot path#1200
FlorentinDUBOIS wants to merge 3 commits intomainfrom
perf/on-headers-optimization

Conversation

@FlorentinDUBOIS
Copy link
Collaborator

Summary

  • Replace 7 format!() allocations with itoa::Buffer, write!() into Vec<u8>, and extend_from_slice
  • Add itoa workspace dependency (zero-dep, stack-based integer formatting)
  • Extract write_forwarded_for_by and write_forwarded_suffix helpers to reduce code duplication

Review fixes applied

  • Removed unnecessary unsafe { from_utf8_unchecked(...) }.as_bytes() roundtrip — now uses &[u8] directly
  • Moved Vec::with_capacity(128) allocation into conditional scope (was allocating on every request even without session_address)
  • Fixed misleading "reusable buffer" comment

Closes #1140

Test plan

  • cargo test --workspace passes
  • cargo clippy --workspace clean
  • E2e tests pass (20/20)

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>
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.

Performance optimisation

1 participant