Summary
Eliminate per-read heap allocations and avoidable data copies in the TCP receive hot path by introducing std::span-based view callbacks and migrating major consumers.
Background
tcp_socket::do_read() currently:
- Allocates + copies into
std::vector<uint8_t> on every read (src/internal/tcp_socket.cpp:116)
- Takes
callback_mutex_ on every read to copy callbacks (src/internal/tcp_socket.cpp:120)
Under high TPS, this amplifies allocator contention, heap fragmentation, cache misses, and context-switch overhead.
Decision
Standardize on view-based receive callbacks:
- Type:
std::span<const uint8_t>
- Lifetime: valid only until the callback returns (do not store/capture across async boundaries)
- Compatibility: keep legacy
std::vector<uint8_t> callbacks working, but make the span path the preferred fast path
Work Breakdown (Split into <1–2 day issues)
Acceptance Criteria
Summary
Eliminate per-read heap allocations and avoidable data copies in the TCP receive hot path by introducing
std::span-based view callbacks and migrating major consumers.Background
tcp_socket::do_read()currently:std::vector<uint8_t>on every read (src/internal/tcp_socket.cpp:116)callback_mutex_on every read to copy callbacks (src/internal/tcp_socket.cpp:120)Under high TPS, this amplifies allocator contention, heap fragmentation, cache misses, and context-switch overhead.
Decision
Standardize on view-based receive callbacks:
std::span<const uint8_t>std::vector<uint8_t>callbacks working, but make the span path the preferred fast pathWork Breakdown (Split into <1–2 day issues)
perf(tcp_socket): add std::span receive callback to remove per-read allocationsperf(secure_tcp_socket): add std::span receive callback to remove per-read allocationsrefactor(websocket): migrate TCP receive path to std::span and update protocol API(depends on perf(tcp_socket): add std::span receive callback to remove per-read allocations #316)refactor(messaging): consume tcp_socket std::span receive callback internally (remove one copy)(depends on perf(tcp_socket): add std::span receive callback to remove per-read allocations #316)feat(benchmark): add TCP receive dispatch benchmark (std::span vs std::vector)Acceptance Criteria
tcp_socketandsecure_tcp_socketprovidestd::spanreceive callbacks with explicit lifetime rulesstd::vectorallocation when span callback is usedstd::vectorjust to hand off to protocol parsing