Skip to content

ShaderBuffer changes to reduce allocation and change set_data to take an iterator#24915

Open
beicause wants to merge 9 commits into
bevyengine:mainfrom
beicause:optimize-shader-buffer-set-data
Open

ShaderBuffer changes to reduce allocation and change set_data to take an iterator#24915
beicause wants to merge 9 commits into
bevyengine:mainfrom
beicause:optimize-shader-buffer-set-data

Conversation

@beicause

@beicause beicause commented Jul 8, 2026

Copy link
Copy Markdown
Member

Objective

Some unnecessary allocations about ShaderBuffer:

  1. ShaderBuffer::set_data allocates a new Vec each time and doesn't reuse existing data.
  2. ShaderBuffer::set_data takes T, which usually causes unnecessary buffer.set_data(iter.collect::<Vec<_>>()).
  3. ShaderBuffer::from(T) allocates a Vec then ShaderBuffer::new allocates a duplicated Vec. In addition, if T is Vec, the ShaderBuffer::from can't reuse T.

Solution

  1. ShaderBuffer::new now accepts Vec<u8> instead of &[u8].
  2. ShaderBuffer::from now accepts Vec<T> where T: bytemuck::NoUninit. From<T: ShaderType + WriteInto> is replaced by ShaderBuffer::from_value.
  3. ShaderBuffer::set_data now accepts impl Iterator<Item = T> instead of a single T value.

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_tools and cargo r --example storage_buffer

@beicause beicause force-pushed the optimize-shader-buffer-set-data branch 2 times, most recently from a783668 to f763fc1 Compare July 8, 2026 08:13
@beicause beicause force-pushed the optimize-shader-buffer-set-data branch from f763fc1 to 2add9b3 Compare July 8, 2026 08:17
@beicause beicause changed the title Optimize ShaderBuffer::set_data and change it to take an iterator ShaderBuffer changes to reduce allocation and change set_data to take an iterator Jul 8, 2026
@beicause beicause force-pushed the optimize-shader-buffer-set-data branch from c6c0f43 to b5f491c Compare July 8, 2026 09:43
@IceSentry IceSentry added A-Rendering Drawing game state to the screen S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 8, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in Rendering Jul 8, 2026
Comment thread crates/bevy_render/src/storage.rs Outdated
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);
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@beicause beicause added the C-Performance A change motivated by improving speed, memory usage or compile times label Jul 9, 2026
@beicause

Copy link
Copy Markdown
Member Author

Note: #24938 is an alternative and conflicts with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen C-Performance A change motivated by improving speed, memory usage or compile times S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

2 participants