From 3257598913c73fb1eb570b2c7800b72fe39acbf6 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 18 Mar 2020 19:16:15 +0100 Subject: [PATCH] library: render: scene: use rayon scope parallelism The implementation was very easy to write, but is not yet optimal. It would be better to use `join` to divide into tasks directly on the stack. And it would be better to iterate over entire blocks of rows instead of giving a row per-thread: this would lead to better cache-line behaviour with the fully linear memory accesses. --- src/render/scene.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/render/scene.rs b/src/render/scene.rs index 9930c59..14fc81e 100644 --- a/src/render/scene.rs +++ b/src/render/scene.rs @@ -49,9 +49,17 @@ impl Scene { } else { Self::pixel }; - for (x, y, pixel) in image.enumerate_pixels_mut() { - *pixel = pixel_func(&self, x as f32, y as f32).into() - } + rayon::scope(|s| { + // FIXME(Bruno): it would go even faster to cut the image in blocks of rows, leading to + // better cache-line behaviour... + for (_, row) in image.enumerate_rows_mut() { + s.spawn(|_| { + for (x, y, pixel) in row { + *pixel = pixel_func(&self, x as f32, y as f32).into() + } + }) + } + }); image }