Skip to content

Refactor SampleBuffer to use value semantics#8324

Draft
sakertooth wants to merge 14 commits into
LMMS:masterfrom
sakertooth:refactor/samplebuffer-value-semantics
Draft

Refactor SampleBuffer to use value semantics#8324
sakertooth wants to merge 14 commits into
LMMS:masterfrom
sakertooth:refactor/samplebuffer-value-semantics

Conversation

@sakertooth
Copy link
Copy Markdown
Contributor

This PR changes SampleBuffer to use value semantics. Helps with improving the usability and ergonomics of this class (e.g. not having to wrap each use of SampleBuffer inside a std::shared_ptr<const SampleBuffer>). SampleBuffer now shares the sample data internally instead of forcing the callers to do it. This should help with the effort in #7497.

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.
Comment thread include/SampleBuffer.h
Comment on lines +109 to +110
inline static const auto emptyBuffer = std::make_shared<SampleFrame[]>(0);
std::shared_ptr<SampleFrame[]> m_data = emptyBuffer;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@sakertooth sakertooth marked this pull request as draft May 4, 2026 17:28
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