Repository navigation
perf(h1): render a keep-alive connection's forwarding values once - #1643
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 28, 2026 02:08
FlorentinDUBOIS
force-pushed
the
perf/h1-forwarding-cache
branch
from
September 28, 2026 02:16
f47c0b6 to
c7d513a
Compare
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
force-pushed
the
perf/h1-forwarding-cache
branch
from
September 28, 2026 03:39
c7d513a to
69bb6d2
Compare
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.
What
X-Forwarded-For,Forwarded,X-Real-IPandX-Forwarded-Portdepend 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.HttpContextnow keeps aForwardingHop(lib/src/protocol/kawa_h1/editor.rs):kawa::Store::Shared(a reference-count bump, no copy);HttpContext::forwarding_hopwhenever one differs (resetkeeps it; a changedsession_address,public_addressorprotocolinvalidates it);X-Real-IPshares theX-Forwarded-Forrendering.Each rendered hop starts with
", ": the same bytes serve an appended hop and, past the separator (theSharedstart offset), a synthesised value. A client-suppliedX-Forwarded-FororForwardedchain 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 20GETs behind a Pythonhttp.server, heap operations per request from anLD_PRELOADmalloc counter (malloc + calloc + realloc + memalign), two interleaved runs each, base =6eef0ba1:−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 setsend_x_real_ip, so theX-Real-IPshare does not show here.Unit (
crate::test_allocations, header editing only):send_x_real_ipX-Forwarded-For+Forwarded, later(+1 under
opentelemetryfor the synthesisedtraceparent,SYNTHESISED_TRACEPARENT.)Syscalls (intentrace, jemalloc build): the same syscall classes, and identical
getsockopt/epoll_ctl/accept4/close/connectcounts, on every pass; a plaintext pair traced to the same 210 syscalls. The residual difference between passes is theepoll_wait/writev/recvfromtriplet 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 on6eef0ba1: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 on6eef0ba1: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 acrossreset: 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 "dropprotocolfrom theForwardingHopkey" goes red on the last step (staleForwarded: 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_headernow documents the first-request cost; its red recipes are updated.All pass in the
--no-default-features --features <crypto>,opentelemetry,splice,simdcells.Scope and conflicts
lib/src/protocol/mux/{answers,h1,h2}.rs: one line each,forwarding_hop: None,in theHttpContextstruct 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 byreset; theeditor.rs:NNNcitations of the TE guard and the pinned block indoc/h2_mux_internals.mdare refreshed.perf(h1): capture a standard reason phrase without a copy, which touches the same struct region,CHANGELOG.mdand the same citations: whichever merges second needs a rebase and itseditor.rs:NNNcitations refreshed.async fn, no syscall added.Commands run
cargo build --locked,cargo build --all-features --lockedcargo clippy --all-targets --locked -- -D warningsand with--no-default-features --features crypto-ring,opentelemetry,splice,simdcargo +nightly fmt --all -- --checkRUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --lockedcargo test -p sozu-lib --lib --locked --no-default-features --features <crypto>,opentelemetry,splice,simdforcrypto-ring,crypto-aws-lc-rs,crypto-openssl,fipscargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8check_doc_citations.pyruns (self-test, broken fixture exits 1,--base origin/main, plain)Signed-off-by: Florentin Dubois [email protected]