Skip to content

perf(inlet): avoid redundant list rebuild in pull_sample#112

Merged
cboulay merged 1 commit into
labstreaminglayer:mainfrom
sappelhoff:perf/pull-sample-list
Jun 16, 2026
Merged

perf(inlet): avoid redundant list rebuild in pull_sample#112
cboulay merged 1 commit into
labstreaminglayer:mainfrom
sappelhoff:perf/pull-sample-list

Conversation

@sappelhoff

Copy link
Copy Markdown
Contributor

(PR and content generated with the help of Claude)

What

Avoid a redundant list rebuild in StreamInlet.pull_sample without changing its output.

Why

The old code always built an intermediate list with a comprehension, then for string streams re-iterated it a second time to decode:

sample = [v for v in self.sample]
if self.channel_format == cf_string:
    sample = [v.decode("utf-8") for v in sample]

Now the numeric path uses list(self.sample) (faster than a comprehension), and the string path decodes directly from the ctypes sample in a single pass with one fewer allocation:

if self.channel_format == cf_string:
    sample = [v.decode("utf-8") for v in self.sample]
else:
    sample = list(self.sample)

Impact

Measured on the extraction step alone (output verified identical via assert old() == new()):

channels numeric old/new string old/new
8 0.69 / 0.62 us (1.12x) 1.33 / 1.17 us (1.14x)
32 2.02 / 1.77 us (1.14x) 4.23 / 3.89 us (1.09x)
64 3.88 / 3.39 us (1.15x) 8.23 / 7.51 us (1.10x)

A modest ~1.1-1.2x per call; negligible at 1 channel. pull_sample is the most-called function in per-sample loops, so it adds up there, and the string path also drops one throwaway allocation per call.

Behavior

Identical output: same list of values, same string decoding, unchanged legacy assign_to and (None, None) paths. Verified with a live localhost round-trip (numeric, string, legacy assign_to); existing test suite passes.

Use list() for the common numeric path (faster than a comprehension),
and decode strings directly from the ctypes sample instead of first
materializing an intermediate list and then re-iterating it to decode.
The string path now does a single pass and one fewer allocation.

Output is unchanged.
@cboulay

cboulay commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

LGTM. Thanks!

@cboulay cboulay merged commit 28ef5fe into labstreaminglayer:main Jun 16, 2026
19 checks passed
@sappelhoff sappelhoff deleted the perf/pull-sample-list branch June 16, 2026 07:11
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.

2 participants