Skip to content

BufferUtils: Zero-copy mono output when offsetSamples is 0#488

Open
codyjhsieh wants to merge 1 commit into
spotify:masterfrom
codyjhsieh:zero-copy-mono-output
Open

BufferUtils: Zero-copy mono output when offsetSamples is 0#488
codyjhsieh wants to merge 1 commit into
spotify:masterfrom
codyjhsieh:zero-copy-mono-output

Conversation

@codyjhsieh
Copy link
Copy Markdown

Resolves the TODO added in #85 at BufferUtils.h:215.

Problem

copyJuceBufferIntoPyArray allocates a new NumPy array and copies all sample data on every process() call, even when the output could be returned directly. For mono audio with no latency trimming (offsetSamples == 0), this is a wasted allocation + memcpy on the hot path.

Solution

Change the function signature from const& to by-value, enabling move semantics. When offsetSamples == 0 and numChannels == 1, move the JUCE buffer into a heap-allocated py::capsule and return a NumPy array that points directly at its memory. Python's refcount frees the capsule when the array is garbage collected.

Multichannel still copies because JUCE's AudioBuffer allocates each channel as a separate heap block — NumPy requires contiguous memory across channels, so the data must be rearranged.

Result

Mono processing avoids a full buffer copy on every call. Stereo and latency-trimmed paths are unchanged. All existing tests pass.

Problem

copyJuceBufferIntoPyArray allocates a new NumPy array and copies all
sample data on every process() call, even when the output could be
returned directly. For mono audio with no latency trimming (the
common case for most plugins), this is a wasted allocation + memcpy.

Solution

Change the function signature from const& to by-value, enabling move
semantics. When offsetSamples is 0 and numChannels is 1, move the
JUCE buffer into a heap-allocated capsule and return a NumPy array
that points directly at its memory. Python's refcount frees the
capsule when the array is garbage collected.

Multichannel still copies because JUCE allocates each channel as a
separate heap block — NumPy needs contiguous memory.

Result

Mono processing avoids a full buffer copy on every call. Stereo and
latency-trimmed paths are unchanged.
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