Skip to content

epic(performance): TCP receive std::span callback migration (zero-allocation hot path) #315

Description

@kcenon

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

  • tcp_socket and secure_tcp_socket provide std::span receive callbacks with explicit lifetime rules
  • TCP receive hot path performs no per-read std::vector allocation when span callback is used
  • WebSocket receive pipeline no longer requires per-read std::vector just to hand off to protocol parsing
  • Messaging pipeline removes at least one copy/allocation per read vs baseline
  • Unit/integration/performance test suites pass in CI

Metadata

Metadata

Assignees

Labels

asyncAsynchronous operationsepicEpic issue tracking multiple sub-tasksperformancePerformance improvementspriority:highHigh priority issuerefactoringCode refactoring and improvements

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions