Skip to content

perf(tcp_socket): add std::span receive callback to remove per-read allocations#321

Merged
kcenon merged 2 commits into
mainfrom
feature/316-tcp-socket-span-callback
Dec 18, 2025
Merged

perf(tcp_socket): add std::span receive callback to remove per-read allocations#321
kcenon merged 2 commits into
mainfrom
feature/316-tcp-socket-span-callback

Conversation

@kcenon

@kcenon kcenon commented Dec 18, 2025

Copy link
Copy Markdown
Owner

Summary

  • Add set_receive_callback_view(std::span<const uint8_t>) API for zero-copy receive path
  • Implement lock-free callback storage using shared_ptr + atomic_load/store
  • Update do_read() to prefer span callback (no vector allocation) when set
  • Maintain backward compatibility with legacy vector callback

Changes

New API

  • tcp_socket::set_receive_callback_view(std::function<void(std::span<const uint8_t>)>)
    • Zero-copy: span points directly into internal read buffer
    • Lifetime contract: span valid only during callback invocation
    • Priority: if both callbacks set, span callback takes precedence

Performance Improvements

  • Eliminate per-read std::vector allocation/copy when using span callback
  • Remove mutex contention from receive hot path via lock-free callback access
  • Reduce allocator overhead and cache misses under high TPS

Files Changed

  • src/internal/tcp_socket.h - Add span callback API and lock-free storage
  • src/internal/tcp_socket.cpp - Implement new functionality
  • include/kcenon/network/internal/tcp_socket.h - Public header sync
  • tests/CMakeLists.txt - Add tcp_socket_test target
  • tests/unit/tcp_socket_test.cpp - Comprehensive unit tests
  • docs/CHANGELOG.md, docs/CHANGELOG_KO.md - Documentation

Test Plan

  • New unit tests for span callback functionality
  • Legacy vector callback continues to work
  • Span callback takes precedence when both set
  • Error callback works correctly
  • Concurrent callback registration stress test
  • Full test suite passes

Run tests:

cmake --build build --target network_tcp_socket_test
./build/bin/network_tcp_socket_test

Closes #316
Part of Epic #315

…llocations

- Add set_receive_callback_view() for zero-copy receive path
- Implement lock-free callback storage using shared_ptr + atomic operations
- Update do_read() to prefer span callback when set (no vector allocation)
- Keep legacy vector callback for backward compatibility
- Add unit tests for new functionality

Closes #316
- Add performance section for TCP socket zero-allocation receive path
- Document new set_receive_callback_view API and lock-free callback storage
- Update both English and Korean changelog files
@github-actions

Copy link
Copy Markdown
Contributor

Performance Comparison

Base Branch Results

No base results

PR Branch Results

No PR results

@kcenon kcenon merged commit 1a70a37 into main Dec 18, 2025
44 checks passed
@kcenon kcenon deleted the feature/316-tcp-socket-span-callback branch December 18, 2025 14:21
kcenon added a commit that referenced this pull request Apr 13, 2026
…llocations (#321)

* perf(tcp_socket): add std::span receive callback to remove per-read allocations

- Add set_receive_callback_view() for zero-copy receive path
- Implement lock-free callback storage using shared_ptr + atomic operations
- Update do_read() to prefer span callback when set (no vector allocation)
- Keep legacy vector callback for backward compatibility
- Add unit tests for new functionality

Closes #316

* docs: update changelog with tcp_socket span callback feature

- Add performance section for TCP socket zero-allocation receive path
- Document new set_receive_callback_view API and lock-free callback storage
- Update both English and Korean changelog files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(tcp_socket): add std::span receive callback to remove per-read allocations

1 participant