Skip to content

Hold each connection's Noise session and handshake driver inline - #230

Merged
kahrendt merged 5 commits into
alloc-5-admission-tailfrom
alloc-6-per-connection
Nov 3, 2026
Merged

kahrendt merged 5 commits into
alloc-5-admission-tailfrom
alloc-6-per-connection

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Layer 6 of 7 (base: alloc-5-admission-tail). Accept to admission: 6 -> 4 allocations under libc++, 8 -> 6 under libstdc++.

Summary

Each connection allocated its Noise session and its handshake driver as separate heap blocks. Both now live inside the objects that own them. In their place the connection carries the session inline in its transport and about 115 B of driver storage on ESP32 for its whole life; the driver no longer keeps its result, which would have more than tripled it.

Changes

  • NoiseTransport holds its session in a std::optional<NoiseSession>, which a re-handshake move-assigns; is_active() reads the optional instead of a separate flag. NoiseHandshakeResult carries the session by value.
  • SendspinConnection holds its NoiseHandshake in a std::optional, emplaced by init_noise_handshake() and reset when the handshake completes or aborts.
  • on_text_frame() hands the result to the caller's optional when it returns COMPLETE; take_result() is gone, and the caller's runtime "COMPLETE but no result" check becomes an assert of that invariant. The result carries the admitting PSK's psk_id and category instead of the whole ResolvedPsk: the PSK is mixed into the session's keys before the result is built, so its bytes never leave run_msg1_core().
  • Both handshake paths build the result in place (emplace, or a prvalue in the re-handshake, which GCC would not elide from a named optional). ESP32 frames: handle_msg1 480 / 496 -> 304 B (-Os / -Og), run_rehandshake_msg1 528 -> 272 B. No task stack bound changes.

Testing

  • The re-handshake test now checks the result's psk_id: the positional construction would compile with server_id and psk_id swapped, and a swap at either site now fails tests.

Of the 4 left, layer 7 removes the time filter; the other 3 are the ones layer 5 lists. Outside the library count, each connection still allocates its connection object and, on ESP, the httpd session's shared_ptr slot.

NoiseHandshakeResult carries its NoiseSession by value and NoiseTransport
keeps it in a std::optional, which a re-handshake move-assigns, so a
handshake no longer heap-allocates its session. is_active() reads the
optional instead of a separate flag.

Accept to admission: 6 -> 5 allocations under libc++, 8 -> 7 under
libstdc++.
SendspinConnection keeps its NoiseHandshake in a std::optional, emplaced
by init_noise_handshake() and reset when the handshake completes or
aborts, instead of allocating it per connection. The connection carries
the driver's storage for its whole life.

Accept to admission: 5 -> 4 allocations under libc++, 7 -> 6 under
libstdc++.
NoiseHandshake::on_text_frame() writes the result into the caller's
optional when it returns COMPLETE, so the handshake no longer stores it
and take_result() goes. The result, like the handshake core's, carries
the admitting PSK's psk_id and category instead of a whole ResolvedPsk:
the PSK is mixed into the session's keys before the result is built, so
its bytes never leave run_msg1_core().

Every connection carries the handshake inline, which shrinks from 440 to
152 bytes on a 64-bit host (about 384 to 115 on ESP32). No task stack
bound changes.
handle_msg1() emplaces its result in the caller's optional, and
run_rehandshake_msg1() returns it as a prvalue, so neither frame holds a
result temporary; a named optional would not be elided by GCC, since
the re-handshake also returns nullopt. The re-handshake test now checks
the result's psk_id.

ESP32 frames: handle_msg1 480 -> 304 bytes (-Os), 496 -> 304 (-Og);
run_rehandshake_msg1 528 -> 272 at both. No task stack bound changes.
Both encrypt_in_place() callers refuse before it when no session is
installed, which clang-tidy cannot see now that the session is an
optional held by value (bugprone-unchecked-optional-access).
@kahrendt
kahrendt force-pushed the alloc-6-per-connection branch from f9c426b to 54f3e27 Compare November 3, 2026 15:15
@kahrendt
kahrendt marked this pull request as ready for review November 3, 2026 15:33
@kahrendt
kahrendt requested a balanced review from Copilot November 3, 2026 15:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the inline session and handshake storage changes.

No open issues 🎉

What changed in this PR

This allocation-reduction layer stores each connection’s Noise session and handshake driver inline, removing two heap allocations from connection admission.

Changes:

  • Replace separately allocated sessions and handshake drivers with inline optional storage.
  • Construct handshake results directly in caller-owned storage, carrying PSK identity and category without its secret bytes.
  • Adapt handshake tests, add a re-handshake PSK-ID assertion, and lower expected admission allocation counts.
File Description
tests/​test_noise_transport.cpp Adapt handshake and transport tests to value-owned sessions and output results.
tests/​test_noise_rehandshake.cpp Update session ownership and assert the resolved re-handshake PSK ID.
tests/​test_allocations.cpp Reduce expected admission allocations by two.
tests/​loopback_connection.h Adapt shared test helpers to inline sessions and caller-owned results.
src/​noise_transport.h Store the session inline and derive active state from its presence.
src/​noise_transport.cpp Install and replace sessions by value.
src/​noise_handshake.h Define value-owned results and remove driver-held result storage.
src/​noise_handshake.cpp Build results in place and keep PSK bytes within core handshake processing.
src/​connection.h Store the handshake driver inline.
src/​connection.cpp Emplace the driver and consume caller-owned handshake results.

Review effort: Balanced

@kahrendt
kahrendt merged commit dc5b330 into main Nov 3, 2026
17 of 33 checks passed
@kahrendt
kahrendt deleted the alloc-6-per-connection branch November 3, 2026 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants