diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2026-08-15 20:44:10 -0700 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2026-08-15 20:44:10 -0700 |
| commit | 05135beb87bbf0edace544b80ba40e327d9a89ac (patch) | |
| tree | 1ec3fc0d3cf041c57273e92ecbe751756c616f67 /src/sim | |
| parent | 43cae9ac71f2d6933ba3ea25641585bb88ae60ad (diff) | |
factor code out of main, optimizations
Diffstat (limited to 'src/sim')
| -rw-r--r-- | src/sim/sim.rs | 14 | ||||
| -rw-r--r-- | src/sim/world.rs | 8 |
2 files changed, 14 insertions, 8 deletions
diff --git a/src/sim/sim.rs b/src/sim/sim.rs index ef2fc80..4c6d908 100644 --- a/src/sim/sim.rs +++ b/src/sim/sim.rs @@ -1,5 +1,6 @@ -use std::{collections::HashMap, marker::PhantomData}; +use std::marker::PhantomData; +use fxhash::FxHashMap; use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use crate::{ @@ -73,11 +74,13 @@ pub fn set_cell(chunks: &mut [Option<&mut Chunk>; 9], x: i32, y: i32, cell: Cell if let Some(chunk) = &mut chunks[(dcx + 1 + (dcy + 1) * 3) as usize] { chunk.set_cell_at_local_position(nc_x, nc_y, cell); chunk.needs_texture_update = true; + chunk.sleeping = false; } } else { if let Some(target) = &mut chunks[4] { target.set_cell_at_local_position(x as u8, y as u8, cell); target.needs_texture_update = true; + target.sleeping = false; } } } @@ -90,15 +93,18 @@ impl UpdateCtx<'_, '_, '_> { } fn set_cell(&mut self, dx: i32, dy: i32, cell: Cell) { + // cannot move out of the neighbourhood, but also cannot move to the edge of the neighbourhood + // as this would wake a chunk outside of the neighbourhood + debug_assert!(dx > -15 && dx < 15); + debug_assert!(dy > -15 && dy < 15); let x = self.x + dx; let y = self.y + dy; set_cell(self.chunks, x, y, cell); - // wake all the chunks self.chunks.iter_mut().for_each(|c| { if let Some(chunk) = c { chunk.sleeping = false; } - }); + }) } pub fn candidates_swap(&mut self, candidates: &[(i32, i32)]) -> bool { @@ -170,7 +176,7 @@ const NEIGHBORHOOD_OFFSETS: [(i32, i32); 9] = [ pub fn sim_tick(world: &mut World, seqno: u64, use_threading: bool) { puffin::profile_function!(); - let mut columns: HashMap<i32, Vec<i32>> = HashMap::new(); + let mut columns: FxHashMap<i32, Vec<i32>> = FxHashMap::default(); for &(cx, cy) in world.chunk_position_to_chunk_idx.keys() { columns.entry(cx).or_default().push(cy); } diff --git a/src/sim/world.rs b/src/sim/world.rs index bdb1286..6645d4e 100644 --- a/src/sim/world.rs +++ b/src/sim/world.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use fxhash::FxHashMap; use crate::{ config::CHUNK_SIZE, @@ -7,8 +7,8 @@ use crate::{ pub struct World { pub chunks: Vec<Chunk>, - // TODO FxHashMap? - pub chunk_position_to_chunk_idx: HashMap<(i32, i32), usize>, + // TODO FxFxHashMap? + pub chunk_position_to_chunk_idx: FxHashMap<(i32, i32), usize>, } impl World { @@ -60,7 +60,7 @@ impl World { pub fn from_default_size() -> Self { let mut world = World { chunks: Vec::new(), - chunk_position_to_chunk_idx: HashMap::new(), + chunk_position_to_chunk_idx: FxHashMap::default(), }; for y in -10..10 { |
