Skip to content

Commit

Permalink
Use safe methods, which somehow speed up grid advancing by ~10%
Browse files Browse the repository at this point in the history
Signed-off-by: lloydmeta <[email protected]>
  • Loading branch information
lloydmeta committed Nov 17, 2023
1 parent 299b9b7 commit 53d1d65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
41 changes: 26 additions & 15 deletions src/data/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rand::Rng;
use rayon::prelude::*;
use std::mem;

pub const PAR_THRESHOLD_AREA: usize = 250_000;
const PAR_THRESHOLD_AREA: usize = 250_000;

/// Used for indexing into the grid
#[allow(clippy::module_name_repetitions)]
Expand All @@ -28,6 +28,7 @@ pub struct Grid {
max_i: usize,
max_j: usize,
area: usize,
area_requires_bool: bool,
// Cache of where the neighbours are for each point
neighbours: Vec<[GridIdx; 8]>,
}
Expand Down Expand Up @@ -66,12 +67,14 @@ impl Grid {
let cells: Vec<Cell> = grid.into_iter().flatten().collect();
let scratchpad_cells = cells.clone();
let area = width * height;
let area_requires_bool = area >= PAR_THRESHOLD_AREA;
Self {
cells,
scratchpad_cells,
max_i,
max_j,
area,
area_requires_bool,
neighbours,
}
}
Expand All @@ -81,11 +84,7 @@ impl Grid {
///
/// TODO: is using iter faster or slower than just doing the checks?
pub fn get_idx(&self, &GridIdx(idx): &GridIdx) -> Option<&Cell> {
if idx < self.cells.len() {
Some(&self.cells[idx])
} else {
None
}
self.cells.get(idx)
}

// TODO delete if not used
Expand Down Expand Up @@ -124,22 +123,34 @@ impl Grid {
self.area
}

pub const fn area_requires_bool(&self) -> bool {
self.area_requires_bool
}

pub fn advance(&mut self) {
{
let neighbours = &self.neighbours;
let last_gen = &self.cells;
let area_requires_par = self.area() >= PAR_THRESHOLD_AREA;
let area_requires_par = self.area_requires_bool();
let cells = &mut self.scratchpad_cells;
let cell_op = |(i, cell): (usize, &mut Cell)| {
let alives = neighbours[i].iter().fold(0, |acc, &GridIdx(idx)| {
if last_gen[idx].0 == Status::Alive {
acc + 1
} else {
acc
if let Some(neighbours_vec) = neighbours.get(i) {
let alives = neighbours_vec.iter().fold(0, |acc, &GridIdx(idx)| {
if let Some(last_gen_status) = last_gen.get(idx) {
if last_gen_status.0 == Status::Alive {
acc + 1
} else {
acc
}
} else {
acc
}
});
if let Some(last_gen_cell) = last_gen.get(i) {
let next_status = last_gen_cell.next_status(alives);
cell.update(next_status);
}
});
let next_status = last_gen[i].next_status(alives);
cell.update(next_status);
}
};
if area_requires_par {
cells.par_iter_mut().enumerate().for_each(cell_op);
Expand Down
4 changes: 2 additions & 2 deletions src/rendering/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::data::{Grid, GridIdx, PAR_THRESHOLD_AREA};
use super::data::{Grid, GridIdx};
use gfx;
use gfx::traits::FactoryExt;
use gfx::Device;
Expand Down Expand Up @@ -222,7 +222,7 @@ impl App {
inst.colour = colour;
}
};
if grid.area() >= PAR_THRESHOLD_AREA {
if grid.area_requires_bool() {
self.instances.par_iter_mut().enumerate().for_each(op);
} else {
for (idx, inst) in self.instances.iter_mut().enumerate() {
Expand Down

0 comments on commit 53d1d65

Please sign in to comment.