Refactor SampleBuffer to use value semantics#8324
Draft
sakertooth wants to merge 14 commits into
Draft
Conversation
Avoids deep copies
We use an aliasing constructor for std::shared_ptr to handle moving a std::vector of audio data into a new SampleBuffer instance. This is to provide convience for users in the code that still should preferably work with std::vector<SampleFrame> (and not the clunky std::unique_ptr<SampleFrame[]> + frame count information), while also giving us performance by preventing the double indirection when accessing samples in the case of a std::shared_ptr<std::vector<SampleFrame>>. Note that you could use std::shared_ptr<std::vector<SampleFrame>> and store raw pointers and other state directly to solve the double indirection issue, but now there is duplicated state (like the size), some in the vector, some in the SampleBuffer.
rubiefawn
reviewed
May 2, 2026
Comment on lines
+109
to
+110
| inline static const auto emptyBuffer = std::make_shared<SampleFrame[]>(0); | ||
| std::shared_ptr<SampleFrame[]> m_data = emptyBuffer; |
Contributor
There was a problem hiding this comment.
If this is intended to be thread-safe, shouldn't these be using atomic shared pointers to prevent a data race on the reference count?
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.
This PR changes
SampleBufferto use value semantics. Helps with improving the usability and ergonomics of this class (e.g. not having to wrap each use ofSampleBufferinside astd::shared_ptr<const SampleBuffer>).SampleBuffernow shares the sample data internally instead of forcing the callers to do it. This should help with the effort in #7497.