Repository navigation
Hold each connection's Noise session and handshake driver inline - #230
Merged
Merged
Conversation
7 tasks done
kahrendt
force-pushed
the
alloc-6-per-connection
branch
from
November 3, 2026 14:38
2693446 to
f9c426b
Compare
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
force-pushed
the
alloc-6-per-connection
branch
from
November 3, 2026 15:15
f9c426b to
54f3e27
Compare
kahrendt
marked this pull request as ready for review
November 3, 2026 15:33
Contributor
There was a problem hiding this comment.
🟢 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
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.
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
NoiseTransportholds its session in astd::optional<NoiseSession>, which a re-handshake move-assigns;is_active()reads the optional instead of a separate flag.NoiseHandshakeResultcarries the session by value.SendspinConnectionholds itsNoiseHandshakein astd::optional, emplaced byinit_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 anassertof that invariant. The result carries the admitting PSK's psk_id and category instead of the wholeResolvedPsk: the PSK is mixed into the session's keys before the result is built, so its bytes never leaverun_msg1_core().emplace, or a prvalue in the re-handshake, which GCC would not elide from a named optional). ESP32 frames:handle_msg1480 / 496 -> 304 B (-Os / -Og),run_rehandshake_msg1528 -> 272 B. No task stack bound changes.Testing
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_ptrslot.