Repository navigation
Decode and encode base64url without heap copies on the handshake path - #226
Merged
Merged
Conversation
7 tasks done
Between a connection's accept and its admission the base64url conversions made 13 operator new allocations. Peer ids now decode and re-encode through stack buffers (public_key_from_peer_id, the client_id in client/init), and the variable-length Noise messages decode and encode through a PlatformBuffer, whose failure aborts the handshake instead of the program. b64url_encode_into() and b64url_decode_into() write to a caller's buffer; decoding takes each four-character group on the stack rather than copying the input. Accept to admission: 57 -> 44 allocations under libc++, 63 -> 50 under libstdc++.
kahrendt
force-pushed
the
alloc-2-base64
branch
from
November 3, 2026 14:38
89bd248 to
736576c
Compare
kahrendt
marked this pull request as ready for review
November 3, 2026 14:40
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block approval of the buffer-based base64url and handshake changes.
No open issues 🎉
What changed in this PR
This PR reduces handshake-path heap allocations by moving base64url conversions to caller-owned buffers. It preserves the existing string/vector helpers while using stack storage for peer IDs and fallible PlatformBuffer allocations for Noise messages.
Changes:
- Adds buffer-based base64url encoding, decoding, and sizing helpers.
- Updates handshake and re-handshake paths to use buffers and spans, propagating scratch-buffer allocation failures.
- Adds buffer-capacity and invalid-input tests; lowers expected admission allocations by 13.
| File | Description |
|---|---|
| tests/test_noise_rehandshake.cpp | Adapts the payload helper to buffer-backed decoded bytes. |
| tests/test_crypto.cpp | Tests buffer capacities and consolidates invalid-input cases. |
| tests/test_allocations.cpp | Updates admission allocation expectations to 44/50. |
| src/platform/base64.h | Adds allocation-free buffer forms and group-wise decoding. |
| src/noise_handshake.h | Declares buffer-backed payloads and span-based interfaces. |
| src/noise_handshake.cpp | Uses caller buffers and handles scratch-allocation failures. |
| src/crypto/keys.h | Declares fixed peer-ID storage and a buffer-based overload. |
| src/crypto/keys.cpp | Decodes and encodes peer IDs through stack buffers. |
| src/connection.h | Changes the re-handshake input interface to a span. |
| src/connection.cpp | Uses span input and propagates message-building failure. |
| src/client_dispatch.cpp | Passes buffer-backed payloads into re-handshake processing. |
Review effort: Balanced
read_noise_handshake_data() reads b64url_decoded_size() into a plain size, so nothing past its zero check dereferences an optional (bugprone-unchecked-optional-access). The decoder's character test is written in its simplified form (readability-simplify-boolean-expr). No behavior change.
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 2 of 7 (base:
alloc-1-counter). Accept to admission: 57 -> 44 allocations under libc++, 63 -> 50 under libstdc++.Summary
The base64url conversions on the handshake path made 13
operator newallocations per connection. They now work through caller buffers.Changes
b64url_encode_into()andb64url_decode_into()write to a caller's buffer, withb64url_encode_buffer_size()/b64url_decoded_size()to size it; decoding takes each four-character group on the stack instead of copying the input.public_key_from_peer_id()takes astring_view, andIdentity::peer_id(PeerIdText&)fills a fixed array forclient/init.PlatformBuffer(read_noise_handshake_data(),build_noise_handshake_message()), so an allocation failure aborts the handshake instead of the program.Testing
b64url_decode()refuses on both platforms, with a row added for a lone final character, which the host's platform decoder alone would turn into zero bytes.