You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At this point, the next lowest hanging fruit for speeding up Universe::tick is removing the allocation and free.
Rust does extra bound checks when indexing arrays. I think in simple cases the compiler can optimize it away (e.g. simple loop with a global .len() check) but that's unlikely with the convolution-like operator needed for GoL. Iterators might also lead to better memory access patterns although it's hard to verify.
I played a bit around and got universe.tick() to run ~2x faster than the version without the modulo. Caveat: measured with criterion, not as wasm, only measured for a 1024x768 grid.
I moved this logic to Cell:
implCell{fnevolution(self,live_neighbors:u8) -> Cell{match(self, live_neighbors){// Rule 1: Any live cell with fewer than two live neighbours// dies, as if caused by underpopulation.(Cell::Alive, x)if x < 2 => Cell::Dead,// Rule 2: Any live cell with two or three live neighbours// lives on to the next generation.(Cell::Alive,2) | (Cell::Alive,3) => Cell::Alive,// Rule 3: Any live cell with more than three live// neighbours dies, as if by overpopulation.(Cell::Alive, x)if x > 3 => Cell::Dead,// Rule 4: Any dead cell with exactly three live neighbours// becomes a live cell, as if by reproduction.(Cell::Dead,3) => Cell::Alive,// All other cells remain in the same state.(otherwise, _) => otherwise,}}}
Using iterators instead of indexing is another possible optimization. At the end of the speedup refactoring: https://rustwasm.github.io/docs/book/game-of-life/time-profiling.html
Rust does extra bound checks when indexing arrays. I think in simple cases the compiler can optimize it away (e.g. simple loop with a global
.len()
check) but that's unlikely with the convolution-like operator needed for GoL. Iterators might also lead to better memory access patterns although it's hard to verify.I played a bit around and got
universe.tick()
to run ~2x faster than the version without the modulo. Caveat: measured with criterion, not as wasm, only measured for a 1024x768 grid.I moved this logic to
Cell
:and replaced
tick
by:Granted it's not the most readable and there is probably a nicer way but it might be good to remind that Rust array indexing is not zero-cost.
(it's using
izip
from theitertools
crate but could nest calls to zip to avoid extra dependency)The text was updated successfully, but these errors were encountered: