Skip to content

Decode and encode base64url without heap copies on the handshake path - #226

Merged
kahrendt merged 2 commits into
alloc-1-counterfrom
alloc-2-base64
Nov 3, 2026
Merged

kahrendt merged 2 commits into
alloc-1-counterfrom
alloc-2-base64

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

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 new allocations per connection. They now work through caller buffers.

Changes

  • b64url_encode_into() and b64url_decode_into() write to a caller's buffer, with b64url_encode_buffer_size() / b64url_decoded_size() to size it; decoding takes each four-character group on the stack instead of copying the input.
  • Peer ids decode and re-encode through stack buffers: public_key_from_peer_id() takes a string_view, and Identity::peer_id(PeerIdText&) fills a fixed array for client/init.
  • The variable-length Noise messages decode and encode through a PlatformBuffer (read_noise_handshake_data(), build_noise_handshake_message()), so an allocation failure aborts the handshake instead of the program.

Testing

  • New: the buffer forms need their full buffer (one byte short refuses, an exact fit succeeds).
  • The invalid-input decode tests become one table of what 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.

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
kahrendt marked this pull request as ready for review November 3, 2026 14:40
@kahrendt
kahrendt requested a balanced review from Copilot November 3, 2026 14:40

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 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.
@kahrendt
kahrendt merged commit f4717d4 into main Nov 3, 2026
16 checks passed
@kahrendt
kahrendt deleted the alloc-2-base64 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