From ce6440344929260c87a2ae9ec23d23bf0bf0b560 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Thu, 19 Mar 2020 20:54:20 +0100 Subject: [PATCH] library: render: split rows in chunks for perf --- src/render/scene.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/render/scene.rs b/src/render/scene.rs index 852d049..df40dac 100644 --- a/src/render/scene.rs +++ b/src/render/scene.rs @@ -61,14 +61,19 @@ impl Scene { Self::pixel }; + let mut rows: Vec<_> = image.enumerate_rows_mut().collect(); 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() { + let chunk_size = self.camera.film().height() as usize / rayon::current_num_threads(); + // `chunks_size + 1` to have exactly num_threads tasks spawned, even with rounding + for chunk in rows.chunks_mut(chunk_size + 1) { s.spawn(|_| { - for (x, y, pixel) in row { - *pixel = pixel_func(&self, x as f32, y as f32).into(); - pb.inc(1); + for (_, row) in chunk { + for (x, y, pixel) in row { + *pixel = pixel_func(&self, x as f32, y as f32).into(); + pb.inc(1); + } } }) }