BufferUtils: Zero-copy mono output when offsetSamples is 0#488
Open
codyjhsieh wants to merge 1 commit into
Open
BufferUtils: Zero-copy mono output when offsetSamples is 0#488codyjhsieh wants to merge 1 commit into
codyjhsieh wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves the TODO added in #85 at
BufferUtils.h:215.Problem
copyJuceBufferIntoPyArrayallocates a new NumPy array and copies all sample data on everyprocess()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. WhenoffsetSamples == 0andnumChannels == 1, move the JUCE buffer into a heap-allocatedpy::capsuleand 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
AudioBufferallocates 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.