rust-gpu icon indicating copy to clipboard operation
rust-gpu copied to clipboard

Large array variable initialization (`let arr = [0; 32 * 1024];`) compiles very slowly.

Open pyranota opened this issue 1 year ago • 2 comments

EDIT(@eddyb): while there are reasons to avoid such arrays, the slowdown is the immediate Rust-GPU bug - I've left unchanged the original text, but used ~~strikethrough~~ for parts that don't apply, IMO.


~~User should not be able to create large sized arrays in rust-gpu shaders.~~ ~~Stack is limited, and large sized data-structures should be moved in buffers.~~ Plus it makes compilation incredibly slow (I assume this syntax [0; SIZE] translates to spirv's [0, 0, 0, 0, 0, ... SIZE])

Expected Behaviour

~~spirv-builder should throw an error, when user creates large array in stack~~

Example & Steps To Reproduce

  1. Setup basic rust-gpu shader
  2. let arr = [0; 32 * 1024];
  3. cargo build

System Info

  • Rust: rustc 1.75.0 (82e1608df 2023-12-21)
  • OS: Pop!_OS
  • GPU: nvidia 1060
  • SPIR-V: SPIRV-Tools v2022.2-dev unknown hash, 2022-02-16T16:37:15

pyranota avatar Feb 11 '24 13:02 pyranota

I suspect let mut array = [0; 32 * 1024]; is worse than something like:

let mut array = MaybeUninit::<[_; 32 * 1024]>::zeroed().assume_init();
// or `core::mem::zeroed`?

I think we have an optimization for e.g. memset(0), but not [expr; N]. (haven't checked the code though, but it could be as simple as using the better approach in more places)

eddyb avatar Feb 20 '24 12:02 eddyb

Thank you for correction, @eddyb

pyranota avatar Feb 20 '24 12:02 pyranota