Skip to content

Add AlignedVec and use it to improve ShaderBuffer#24938

Open
beicause wants to merge 8 commits into
bevyengine:mainfrom
beicause:shader-buffer-improve
Open

Add AlignedVec and use it to improve ShaderBuffer#24938
beicause wants to merge 8 commits into
bevyengine:mainfrom
beicause:shader-buffer-improve

Conversation

@beicause

@beicause beicause commented Jul 10, 2026

Copy link
Copy Markdown
Member

Objective

Alternative to #24915.

Accessing data in ShaderBuffer is inconvenient. Because Vec<u8> may not aligned to the type of element of storage buffer so you have to use bytemuck::pod_read_unaligned/copy_from_slice which incurs extra copies and may be slow if you read/write in a loop. This happends in #24922 (comment)

Solution

Add AlignedVec to bevy_platform based on https://github.com/rkyv/rkyv/blob/main/rkyv/src/util/alloc/aligned_vec.rs but the alignment can be set at runtime.

By dynamically allocating an aligned byte array, we can directly index and read/write elements using bytemuck::cast_slice / cast_slice_mut, and success is guaranteed as long as you always use the same type T. (Previously, casting Vec<u8> could also success because the memory allocation might satisfy the larger alignment, but this is not guaranteed)

I also included the ShaderBufferData improvement from #24922 and removed methods that take ShaderType.

Testing

cargo test -p bevy_platform --all-features
cargo r --example fps_overlay --features bevy_dev_tools
cargo r --example storage_buffer

@beicause beicause force-pushed the shader-buffer-improve branch from 7297dd3 to 1ed30a7 Compare July 10, 2026 08:25
@beicause beicause added A-Rendering Drawing game state to the screen C-Performance A change motivated by improving speed, memory usage or compile times A-Utils Utility functions and types D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 10, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in Rendering Jul 10, 2026
@beicause beicause force-pushed the shader-buffer-improve branch from 4ae500b to c3efcc5 Compare July 10, 2026 08:48
Co-authored-by: Patrick Walton <pcwalton@mimiga.net>
@beicause beicause force-pushed the shader-buffer-improve branch from c3efcc5 to 39b006d Compare July 10, 2026 09:12
@beicause beicause force-pushed the shader-buffer-improve branch from d46b172 to b007b76 Compare July 10, 2026 16:13
@beicause

beicause commented Jul 10, 2026

Copy link
Copy Markdown
Member Author

Benchmark results:

old
 cargo bench -p benches --bench platform
    Finished `bench` profile [optimized] target(s) in 0.35s
     Running benches/bevy_platform/main.rs (target/release/deps/platform-c40939b9272e6ab3)
Gnuplot not found, using plotters backend
platform::aligned_vec::size1_align1/read_10000_elements_unaligned
                        time:   [19.016 µs 19.023 µs 19.031 µs]
Found 3 outliers among 100 measurements (3.00%)
  1 (1.00%) high mild
  2 (2.00%) high severe
platform::aligned_vec::size1_align1/read_10000_elements_aligned
                        time:   [19.048 µs 19.054 µs 19.062 µs]
Found 10 outliers among 100 measurements (10.00%)
  7 (7.00%) high mild
  3 (3.00%) high severe
platform::aligned_vec::size1_align1/write_10000_elements_unaligned
                        time:   [4.8379 µs 4.8442 µs 4.8524 µs]
Found 18 outliers among 100 measurements (18.00%)
  2 (2.00%) low mild
  6 (6.00%) high mild
  10 (10.00%) high severe
platform::aligned_vec::size1_align1/write_10000_elements_aligned
                        time:   [4.8068 µs 4.8118 µs 4.8181 µs]
Found 9 outliers among 100 measurements (9.00%)
  5 (5.00%) high mild
  4 (4.00%) high severe

platform::aligned_vec::size64_align16/read_10000_elements_unaligned
                        time:   [103.81 µs 104.66 µs 105.45 µs]
platform::aligned_vec::size64_align16/read_10000_elements_aligned
                        time:   [103.97 µs 104.79 µs 105.57 µs]
platform::aligned_vec::size64_align16/write_10000_elements_unaligned
                        time:   [27.913 µs 27.942 µs 27.979 µs]
Found 8 outliers among 100 measurements (8.00%)
  2 (2.00%) high mild
  6 (6.00%) high severe
platform::aligned_vec::size64_align16/write_10000_elements_aligned
                        time:   [13.309 µs 13.335 µs 13.391 µs]
Found 9 outliers among 100 measurements (9.00%)
  2 (2.00%) low mild
  2 (2.00%) high mild
  5 (5.00%) high severe

platform::aligned_vec::size128_align128/read_10000_elements_unaligned
                        time:   [113.92 µs 114.26 µs 114.63 µs]
Found 13 outliers among 100 measurements (13.00%)
  8 (8.00%) high mild
  5 (5.00%) high severe
platform::aligned_vec::size128_align128/read_10000_elements_aligned
                        time:   [111.57 µs 111.65 µs 111.74 µs]
Found 6 outliers among 100 measurements (6.00%)
  3 (3.00%) high mild
  3 (3.00%) high severe
platform::aligned_vec::size128_align128/write_10000_elements_unaligned
                        time:   [21.317 µs 21.331 µs 21.346 µs]
Found 18 outliers among 100 measurements (18.00%)
  7 (7.00%) low mild
  4 (4.00%) high mild
  7 (7.00%) high severe
platform::aligned_vec::size128_align128/write_10000_elements_aligned
                        time:   [21.335 µs 21.382 µs 21.436 µs]
Found 8 outliers among 100 measurements (8.00%)
  5 (5.00%) high mild
  3 (3.00%) high severe

size64_align16/write_10000_elements_aligned is 2x faster than size64_align16/write_10000_elements_unaligned, but there is no difference in the other results.

cargo bench -p benches --bench platform
   Compiling benches v0.0.0 (/mnt/external/git_repos/bevy/benches)
    Finished `bench` profile [optimized] target(s) in 1.74s
     Running benches/bevy_platform/main.rs (target/release/deps/platform-c40939b9272e6ab3)
Gnuplot not found, using plotters backend
platform::aligned_vec::size1_align1/read_100000_elements_unaligned
                        time:   [51.760 µs 51.847 µs 51.936 µs]
Found 4 outliers among 100 measurements (4.00%)
  1 (1.00%) low mild
  3 (3.00%) high severe
platform::aligned_vec::size1_align1/read_100000_elements_aligned
                        time:   [48.298 µs 48.428 µs 48.584 µs]
Found 18 outliers among 100 measurements (18.00%)
  3 (3.00%) low mild
  2 (2.00%) high mild
  13 (13.00%) high severe
platform::aligned_vec::size1_align1/write_100000_elements_unaligned
                        time:   [52.677 µs 52.699 µs 52.724 µs]
Found 7 outliers among 100 measurements (7.00%)
  4 (4.00%) high mild
  3 (3.00%) high severe
platform::aligned_vec::size1_align1/write_100000_elements_aligned
                        time:   [48.478 µs 48.542 µs 48.615 µs]
Found 8 outliers among 100 measurements (8.00%)
  4 (4.00%) high mild
  4 (4.00%) high severe

platform::aligned_vec::size64_align16/read_100000_elements_unaligned
                        time:   [337.77 µs 347.15 µs 355.81 µs]
Found 9 outliers among 100 measurements (9.00%)
  4 (4.00%) high mild
  5 (5.00%) high severe
platform::aligned_vec::size64_align16/read_100000_elements_aligned
                        time:   [307.68 µs 311.73 µs 316.85 µs]
Found 9 outliers among 100 measurements (9.00%)
  5 (5.00%) high mild
  4 (4.00%) high severe
Benchmarking platform::aligned_vec::size64_align16/write_100000_elements_unaligned: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 8.2s, enable flat sampling, or reduce sample count to 50.
platform::aligned_vec::size64_align16/write_100000_elements_unaligned
                        time:   [1.5979 ms 1.6000 ms 1.6023 ms]
Found 13 outliers among 100 measurements (13.00%)
  3 (3.00%) high mild
  10 (10.00%) high severe
Benchmarking platform::aligned_vec::size64_align16/write_100000_elements_aligned: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 8.2s, enable flat sampling, or reduce sample count to 50.
platform::aligned_vec::size64_align16/write_100000_elements_aligned
                        time:   [1.6165 ms 1.6203 ms 1.6246 ms]
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high severe

platform::aligned_vec::size128_align128/read_100000_elements_unaligned
                        time:   [736.88 µs 746.05 µs 757.56 µs]
Found 11 outliers among 100 measurements (11.00%)
  5 (5.00%) high mild
  6 (6.00%) high severe
platform::aligned_vec::size128_align128/read_100000_elements_aligned
                        time:   [651.98 µs 657.63 µs 664.06 µs]
Found 10 outliers among 100 measurements (10.00%)
  5 (5.00%) high mild
  5 (5.00%) high severe
Benchmarking platform::aligned_vec::size128_align128/write_100000_elements_unaligned: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 6.9s, enable flat sampling, or reduce sample count to 60.
platform::aligned_vec::size128_align128/write_100000_elements_unaligned
                        time:   [1.3268 ms 1.3445 ms 1.3649 ms]
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) low mild
Benchmarking platform::aligned_vec::size128_align128/write_100000_elements_aligned: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.2s, enable flat sampling, or reduce sample count to 50.
platform::aligned_vec::size128_align128/write_100000_elements_aligned
                        time:   [1.3964 ms 1.4066 ms 1.4182 ms]
Found 12 outliers among 100 measurements (12.00%)
  1 (1.00%) low mild
  5 (5.00%) high mild
  6 (6.00%) high severe

@pcwalton

Copy link
Copy Markdown
Contributor

Seems fine to me.

@beicause

Copy link
Copy Markdown
Member Author

Actually, after replacing assert with black_box the differences in all results are small (I'm not sure why). So I think the main benefit may be easier access to elements in slice.

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 A-Utils Utility functions and types C-Performance A change motivated by improving speed, memory usage or compile times D-Unsafe Touches with unsafe code in some way 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