Skip to content

feat(benchmark): add TCP receive dispatch benchmark (std::span vs std::vector) #320

Description

@kcenon

Parent Issue

Part of #315 (TCP receive std::span callback migration)

Summary

Add a focused benchmark that quantifies the overhead difference between:

  • span-based receive dispatch (no per-iteration allocation)
  • legacy vector-based receive dispatch (per-iteration allocation + copy)

This provides a concrete baseline for the TCP receive hot-path migration and helps detect regressions in CI.

Background

After #316, tcp_socket can deliver received bytes as std::span<const uint8_t> without allocating/copying into a temporary vector. We should add a micro benchmark to validate the expected performance gap and keep it visible over time.

Proposed Benchmarks

Add a new benchmark file (or extend an existing one):

  • benchmarks/tcp_receive_bench.cpp (preferred) or extend benchmarks/message_throughput_bench.cpp

Benchmarks:

  1. BM_TcpReceive_Dispatch_Span

    • Create a fixed std::array<uint8_t, N>
    • Create a std::span<const uint8_t> view
    • Invoke a trivial callback that touches bytes (to prevent dead-code elimination)
    • No vector construction
  2. BM_TcpReceive_Dispatch_VectorFallback

    • Same fixed input
    • Construct std::vector<uint8_t> each iteration (simulating legacy adapter path)
    • Invoke the same callback signature

Optional:

  • Run with --benchmark_min_time=5.0 for stable results in CI.

Files to Update

  • benchmarks/CMakeLists.txt
  • Add benchmarks/tcp_receive_bench.cpp (or modify existing benchmark sources)

Acceptance Criteria

  • Benchmarks compile under -DNETWORK_BUILD_BENCHMARKS=ON
  • Benchmark binary includes the new TCP receive dispatch benchmarks
  • CI benchmark workflow runs successfully and publishes results
  • Results clearly show span path outperforming vector fallback for small payloads

Test Plan

Local:

cmake -B build -S . -DNETWORK_BUILD_BENCHMARKS=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/benchmarks/network_benchmarks --benchmark_filter=TcpReceive --benchmark_min_time=3.0

Notes

This benchmark intentionally isolates dispatch/copy costs; it does not attempt to benchmark kernel/network I/O.

Metadata

Metadata

Assignees

Labels

asyncAsynchronous operationsci-cdCI/CD and build automationenhancementNew feature or requestperformancePerformance improvements

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions