ShaderBuffer changes to reduce allocation and change set_data to take an iterator#24915
Open
beicause wants to merge 9 commits into
Open
ShaderBuffer changes to reduce allocation and change set_data to take an iterator#24915beicause wants to merge 9 commits into
ShaderBuffer changes to reduce allocation and change set_data to take an iterator#24915beicause wants to merge 9 commits into
Conversation
a783668 to
f763fc1
Compare
f763fc1 to
2add9b3
Compare
ShaderBuffer::set_data and change it to take an iteratorShaderBuffer changes to reduce allocation and change set_data to take an iterator
c6c0f43 to
b5f491c
Compare
IceSentry
approved these changes
Jul 8, 2026
beicause
commented
Jul 9, 2026
Comment on lines
+135
to
+140
| if values.peek().is_none() { | ||
| let size = size_of::<T>(); | ||
| self.extend_raw(core::iter::repeat_n(0u8, size)); | ||
| } else { | ||
| self.extend_raw(values); | ||
| } |
Member
Author
There was a problem hiding this comment.
Thinking again, I perfer to remove this behavior. Automatically inserting zeros is a bit surprising. I'd rather let the caller handle empty iterators themselves.
Member
Author
|
Note: #24938 is an alternative and conflicts with this. |
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.
Objective
Some unnecessary allocations about
ShaderBuffer:ShaderBuffer::set_dataallocates a newVeceach time and doesn't reuse existing data.ShaderBuffer::set_datatakesT, which usually causes unnecessarybuffer.set_data(iter.collect::<Vec<_>>()).ShaderBuffer::from(T)allocates aVecthenShaderBuffer::newallocates a duplicatedVec. In addition, ifTisVec, theShaderBuffer::fromcan't reuseT.Solution
ShaderBuffer::newnow acceptsVec<u8>instead of&[u8].ShaderBuffer::fromnow acceptsVec<T>whereT: bytemuck::NoUninit.From<T: ShaderType + WriteInto>is replaced byShaderBuffer::from_value.ShaderBuffer::set_datanow acceptsimpl Iterator<Item = T>instead of a singleTvalue.Note that previous if vec is empty
buffer.set_data(vec)will insert at least one zeroed element. I reserved this behavior by peeking iterator and extending zeros.Testing
cargo r --example fps_overlay --features bevy_dev_toolsandcargo r --example storage_buffer