Skip to content

Hold the server's id and name inline - #233

Merged
kahrendt merged 2 commits into
mainfrom
server-info-inline
Nov 3, 2026
Merged

kahrendt merged 2 commits into
mainfrom
server-info-inline

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Removes the server-identity heap allocations between accept and admission. The connection kept the server's id and name as a ServerInformationObject (two std::strings), and the published slot copied it. A 43-character id is too long for either standard library's inline string storage, and under libstdc++ so is a name over 15 characters. Admission made 3 such allocations on libc++ and 5 on libstdc++ (ESP-IDF); it now makes 1 on both.

Depends on Sendspin/spec#339, which caps server/hello's name and group/update's group_name at 64 bytes in UTF-8.

Change

  • InlineText<N> (src/inline_text.h) holds bounded text inside the object; IdText is now InlineText<PEER_ID_SIZE>. utf8_truncate() cuts text at a character boundary.
  • The connection holds the server id as an IdText and the name as a 64-byte ServerName. get_server_id() returns const IdText&, and the helpers that took the id as a std::string take a std::string_view.
  • The published slot holds the same inline copies. get_server_information() builds the public ServerInformationObject on the caller's thread, so the public API is unchanged.
  • read_friendly_name() truncates server/hello's name and group/update's group_name to 64 bytes with one warning log. group_name stays a std::string in the public GroupUpdateObject.
  • tools/stack_usage/README.md is re-derived from fresh ESP32 builds:
    • protocol task bound at -Og: 6,656 → 6,704 (ServerHelloMessage now holds the name inline);
    • reachable figure at -Og: 6,400 → 6,368;
    • the 7,168 default does not change.

Tests

  • test_inline_text.cpp: a table of truncation cases (at the cap, before a lead byte, inside 2-, 3- and 4-byte characters, malformed continuation runs, only continuation bytes before the cut), plus InlineText::assign() refusing text over its capacity.
  • Protocol.FriendlyNamesAreTruncatedToTheirCap: a name at the cap is kept whole, and one with a character straddling the cap is truncated before it, for both fields.
  • JunkProbeDoesNotBlockRealServer now also checks the published name through get_server_information(). No test checked it before, so a mutant that dropped it passed.
  • ADMISSION_ALLOCATIONS is lowered to 1, checked under clang / libc++ and GCC 16 / libstdc++. The one left is the trust-change note on_handshake_complete() queues.
  • Mutants killed: no truncation in read_friendly_name(), unbounded back-off, the len > 0 guard removed (ASan out-of-bounds read), and the published name dropped.

The connection kept the server's identity as a ServerInformationObject,
two std::strings, and the published slot copied it: a 43-character id
outgrows both standard libraries' inline strings, and libstdc++'s also
the name, so admission made 3 (libc++) or 5 (libstdc++) heap allocations
for it.

InlineText<N> holds bounded text in the object (IdText is now one), and
the connection and the published slot keep the id and a 64-byte name in
it. get_server_information() builds the public strings on the caller's
thread. server/hello's name and group/update's group_name are truncated
to 64 UTF-8 bytes at a character boundary, the cap the spec is adding.
Admission now allocates once on both libraries: the trust note.
@kahrendt
kahrendt marked this pull request as ready for review November 3, 2026 16:49
@kahrendt
kahrendt requested a balanced review from Copilot November 3, 2026 16:49

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.

🟡 Changes recommended

Outstanding findings leave name handoff, the 64-byte limit, and long-name allocation coverage incomplete, and the integration guide lacks truncation documentation.

4 open issues
What changed in this PR

This PR stores server identity inline through connection admission and publication, reducing heap allocations while preserving the public server-information API.

Changes:

  • Adds reusable bounded text storage and UTF-8-safe truncation for server and group names.
  • Replaces internal identity strings with inline values and string views.
  • Updates tests, lowers the admission allocation budget to one, and refreshes stack measurements without changing the default stack size.
File Description
tools/​stack_usage/​README.md Updates protocol-task stack measurements.
tests/​test_protocol.cpp Tests friendly-name truncation for both fields.
tests/​test_inline_text.cpp Tests truncation boundaries and capacity rejection.
tests/​test_connection_lifecycle.cpp Checks the published server name.
tests/​test_allocations.cpp Lowers the admission allocation budget to one.
tests/​CMakeLists.txt Registers inline-text tests.
src/​record_store.h Declares string-view server-ID parameters.
src/​record_store.cpp Uses string views in server-ID helpers.
src/​protocol.cpp Applies bounded friendly-name parsing.
src/​protocol_messages.h Defines the name limit and inline name type.
src/​inline_text.h Adds bounded text storage and UTF-8 truncation.
src/​crypto/​keys.h Reuses InlineText for IDs.
src/​connection.h Stores server identity inline.
src/​connection.cpp Uses inline identity during handshakes and logging.
src/​connection_manager.h Defines inline published identity storage.
src/​connection_manager.cpp Publishes inline values and builds public strings after unlocking.
src/​connection_manager_pairing.cpp Adapts pairing paths to inline IDs.
src/​client.cpp Accepts string views when queuing pairing notifications.
src/​client_dispatch.cpp Transfers parsed inline names to connections.
src/​admission.h Accepts server IDs as string views.
include/​sendspin/​types.h Documents friendly-name truncation.
include/​sendspin/​client.h Updates private pairing-notification signatures.

Review effort: Balanced

Comment thread include/sendspin/types.h Resolved
Comment thread src/connection_manager.cpp Resolved
Comment thread src/protocol.cpp Resolved
Comment thread tests/test_protocol.cpp Resolved
The friendly-name test built its inputs from MAX_FRIENDLY_NAME_BYTES, so
a cap drifting off the spec's 64 still passed; it now uses a literal
63-byte prefix. The fake server's hello name was 21 bytes, inside
libc++'s inline string, so the allocation test could not see a heap copy
of it on macOS; it is now 32 bytes, named FAKE_SERVER_HELLO_NAME.
@kahrendt
kahrendt merged commit 67a9be6 into main Nov 3, 2026
16 checks passed
@kahrendt
kahrendt deleted the server-info-inline branch November 3, 2026 17:10
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