Skip to content

perf(h1): render a keep-alive connection's forwarding values once - #1643

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
perf/h1-forwarding-cache
Sep 28, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
perf/h1-forwarding-cache

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

What

X-Forwarded-For, Forwarded, X-Real-IP and X-Forwarded-Port depend only on the protocol, the public address and the peer address — all connection-scoped — yet every request of a keep-alive connection rendered them again. HttpContext now keeps a ForwardingHop (lib/src/protocol/kawa_h1/editor.rs):

  • rendered by the first request of the connection, shared by the next ones through kawa::Store::Shared (a reference-count bump, no copy);
  • keyed on its three inputs and rendered again by HttpContext::forwarding_hop whenever one differs (reset keeps it; a changed session_address, public_address or protocol invalidates it);
  • X-Real-IP shares the X-Forwarded-For rendering.

Each rendered hop starts with ", ": the same bytes serve an appended hop and, past the separator (the Shared start offset), a synthesised value. A client-supplied X-Forwarded-For or Forwarded chain is request-scoped and never cached: the rendered hop is appended to it in one exact-size copy (extended_chain), so the shared scratch buffer is gone too. The request id (per request since #1635) is untouched.

Measurements

Release, --no-default-features --features crypto-aws-lc-rs (glibc malloc), one curl keep-alive connection of 20 GETs behind a Python http.server, heap operations per request from an LD_PRELOAD malloc counter (malloc + calloc + realloc + memalign), two interleaved runs each, base = 6eef0ba1:

Scenario Before After Δ
H1 plaintext keep-alive 11.45 / 11.50 7.65 / 7.70 −3.80
H1 over TLS keep-alive 27.55 / 27.55 23.75 / 23.75 −3.80

−3.80 = 4 per request (scratch, X-Forwarded-For, Forwarded, X-Forwarded-Port) on 19 of 20 requests; the first renders the hop. curl sends no forwarding header and the listener does not set send_x_real_ip, so the X-Real-IP share does not show here.

Unit (crate::test_allocations, header editing only):

Request Before After
bare, first of the connection 7 7
bare, later, send_x_real_ip 8 3
bare, later 7 3
client X-Forwarded-For + Forwarded, later 8 6

(+1 under opentelemetry for the synthesised traceparent, SYNTHESISED_TRACEPARENT.)

Syscalls (intentrace, jemalloc build): the same syscall classes, and identical getsockopt/epoll_ctl/accept4/close/connect counts, on every pass; a plaintext pair traced to the same 210 syscalls. The residual difference between passes is the epoll_wait/writev/recvfrom triplet moving together with timing (base alone spans 210–222 plaintext, 216–228 TLS). No syscall added.

Tests (seen red before the change)

  • keep_alive_requests_reuse_the_connection_forwarding_hop — red on 6eef0ba1: left: 8, right: 7; mutation "rebuild the hop on every call": left: 7, right: 3.
  • a_client_chain_costs_one_exact_copy_per_extended_header — red on 6eef0ba1: left: 8, right: 6; mutation "shared 128-byte scratch again": left: 7, right: 6.
  • a_client_chain_never_leaks_into_the_next_request — byte-exact, IPv6 peer and public address over HTTPS, send_x_real_ip, bare → client chains → bare twice across reset: a client chain is extended, never inherited by the next request.
  • a_changed_forwarding_input_renders_the_hop_again — byte-exact after changing the peer alone, then the public address alone, then the protocol alone (HTTP → HTTPS); mutation "drop protocol from the ForwardingHop key" goes red on the last step (stale Forwarded: proto=http).
  • header_editing_output_is_byte_exact_across_keep_alive_requests (existing) unchanged and green.
  • a_bare_request_costs_one_scratch_and_one_copy_per_forwarding_header now documents the first-request cost; its red recipes are updated.

All pass in the --no-default-features --features <crypto>,opentelemetry,splice,simd cells.

Scope and conflicts

  • lib/src/protocol/mux/{answers,h1,h2}.rs: one line each, forwarding_hop: None, in the HttpContext struct literals of their test helpers — required by the new field.
  • lib/src/protocol/kawa_h1/LIFECYCLE.md: §2 describes the hop, invariant 4 lists it as preserved by reset; the editor.rs:NNN citations of the TE guard and the pinned block in doc/h2_mux_internals.md are refreshed.
  • Independent of the sibling PR perf(h1): capture a standard reason phrase without a copy, which touches the same struct region, CHANGELOG.md and the same citations: whichever merges second needs a rebase and its editor.rs:NNN citations refreshed.
  • No dependency, no async fn, no syscall added.

Commands run

  • cargo build --locked, cargo build --all-features --locked
  • cargo clippy --all-targets --locked -- -D warnings and with --no-default-features --features crypto-ring,opentelemetry,splice,simd
  • cargo +nightly fmt --all -- --check
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked
  • cargo test -p sozu-lib --lib --locked --no-default-features --features <crypto>,opentelemetry,splice,simd for crypto-ring, crypto-aws-lc-rs, crypto-openssl, fips
  • cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8
  • the four check_doc_citations.py runs (self-test, broken fixture exits 1, --base origin/main, plain)

Signed-off-by: Florentin Dubois [email protected]

X-Forwarded-For, Forwarded, X-Real-IP and X-Forwarded-Port depend only on
the protocol, the public address and the peer address, all connection-
scoped, yet every request of a keep-alive connection rendered them again.
HttpContext now keeps them in a ForwardingHop rendered by the first
request, shared by the next ones through kawa::Store::Shared, and rendered
again whenever one of its inputs differs. X-Real-IP shares the
X-Forwarded-For rendering. A client-supplied X-Forwarded-For or Forwarded
chain is request-scoped and never cached: the rendered hop is appended to
it in one exact-size copy. Each rendered hop starts with ", " so one
rendering serves both an appended hop and, past the separator, a
synthesised value; the forwarded bytes are unchanged.

Header editing of a bare request falls from 7 to 3 heap operations from
the second request of a connection on (8 to 3 with send_x_real_ip), and a
request carrying both chains from 8 to 6; the first request stays at 7.
Measured on a release build (crypto-aws-lc-rs, no jemalloc), 20 keep-alive
requests: plaintext H1 11.45 -> 7.65 heap operations per request, H1 over
TLS 27.55 -> 23.75, no syscall class or count added.

The three HttpContext struct literals of the mux test helpers gain the new
field.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit a3cb11a into main Sep 28, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the perf/h1-forwarding-cache branch September 28, 2026 04:43
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