Skip to content

Cache stream channel count in hot read/write path#68

Open
gretel wants to merge 1 commit into
pothosware:masterfrom
gretel:pr/perf-streamer
Open

Cache stream channel count in hot read/write path#68
gretel wants to merge 1 commit into
pothosware:masterfrom
gretel:pr/perf-streamer

Conversation

@gretel
Copy link
Copy Markdown

@gretel gretel commented May 1, 2026

Cache stream channel count in hot read/write path

Changed

  • Cache get_num_channels() once at setupStream() into a _nchan member; use the cached value in readStream() / writeStream().

Why

get_num_channels() is virtual; called once per recv / send. Caching skips the per-call dispatch in the hot path.

readStream() and writeStream() are the per-packet hot path. Each call
constructs a uhd::rx_streamer::buffs_type / tx_streamer::buffs_type
which needs the channel count:

  uhd::rx_streamer::buffs_type stream_buffs(buffs, stream->get_num_channels());
                                                   ^^^^^^^^^^^^^^^^^^^^^^^^

uhd::rx_streamer / tx_streamer are abstract base classes; get_num_channels
is a virtual call going through the vtable to the device-specific
streamer implementation. The channel count is established at
get_rx_stream() / get_tx_stream() time and never changes for the
lifetime of the streamer, so making the call once per readStream()
invocation is wasteful.

Cache it in SoapyUHDStream::num_channels at setupStream() time.

  struct SoapyUHDStream
  {
      uhd::rx_streamer::sptr rx;
      uhd::tx_streamer::sptr tx;
      size_t num_channels = 0;   // <-- new
  };

Cost: one size_t per stream object. Benefit: one fewer indirect call
per packet on the read/write hot path, where SoapyUHD typically sees
hundreds to thousands of calls per second at high sample rates.

Build verified clean against UHD 4.10.0.0 (uhd-oc 4.10.0.0_1 keg).
No behavior change.

Signed-off-by: Tom Hensel <code@jitter.eu>
@gretel gretel marked this pull request as ready for review May 1, 2026 21:55
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.

1 participant