spicylobstergames / astratomic

Survival game inspired by Noita and Starbound, fueled by Rust and Bevy.
Other
91 stars 6 forks source link

No Need to Spawn Thread in a `par_iter()` #1

Closed zicklag closed 7 months ago

zicklag commented 8 months ago

https://github.com/Zac8668/democritus/blob/2f4e1bf5b0387b8dbcd72d3937ea77fdcc5615d9/src/grid.rs#L163-L165

When you use rayon to do a par_iter(), you don't actually need to spawn any threads yourself, because rayon automatically runs your for_each() closure on it's own thread pool.

Spawning threads actually takes some time, because it has to start a new OS process, so what rayon does is create one thread for every CPU core that you have, and then it re-uses those threads to do all of it's work, automatically running your code on it's pre-created threads, so that you don't have to think about it.

The par_iter() won't return until all the jobs are finished, either, so that removes the need to wait for any thread handles or anything like that. That's what's nifty about rayon, you hardly have to think about the threads, you just change iter to par_iter for the most part.


PS: I'm going to be looking at doing the direct write to GPU textures, so I'll have to change this code in a PR if I get to it, so you may not want to change it right away.

zicklag commented 7 months ago

Closing as we're not doing this anymore.