From 05135beb87bbf0edace544b80ba40e327d9a89ac Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Sat, 15 Aug 2026 20:44:10 -0700 Subject: factor code out of main, optimizations --- src/sim/sim.rs | 14 ++++++++++---- src/sim/world.rs | 8 ++++---- 2 files changed, 14 insertions(+), 8 deletions(-) (limited to 'src/sim') 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> = HashMap::new(); + let mut columns: FxHashMap> = 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, - // 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 { -- cgit v1.3.1