From 4cac0a13cd8cebdad0640c0107b2d2f6d4a14f76 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 15 Oct 2026 00:01:00 +0100 Subject: [PATCH] perf(client): track structural chunk membership revisions --- crates/horizon-client/src/chunk_map.rs | 122 +++++++++++++++++++++++++ crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/world.rs | 2 +- 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 crates/horizon-client/src/chunk_map.rs diff --git a/crates/horizon-client/src/chunk_map.rs b/crates/horizon-client/src/chunk_map.rs new file mode 100644 index 000000000..81e06f020 --- /dev/null +++ b/crates/horizon-client/src/chunk_map.rs @@ -0,0 +1,122 @@ +//! Resident chunk membership, with a token for consumers that cache its keys. +use crate::{chunk::Chunk, fast_hash::FxHashMap}; +use std::{ + collections::hash_map::{Iter, ValuesMut}, + ops::Deref, + sync::{ + Arc, + atomic::{AtomicU64, Ordering}, + }, +}; + +type Key = (i32, i32); +type Columns = FxHashMap>; +static NEXT_REVISION: AtomicU64 = AtomicU64::new(1); +fn next_revision() -> u64 { + NEXT_REVISION.fetch_add(1, Ordering::Relaxed) +} + +/// Read access behaves like a map. Structural writes are explicit so edits to +/// blocks or light don't invalidate consumers of the loaded chunk key set. +/// Tokens are process-wide: replacing the entire map also invalidates caches. +pub struct ChunkMap { + columns: Columns, + revision: u64, +} +impl Default for ChunkMap { + fn default() -> Self { + Self { + columns: Columns::default(), + revision: next_revision(), + } + } +} +impl ChunkMap { + pub fn membership_revision(&self) -> u64 { + self.revision + } + pub fn insert(&mut self, key: Key, chunk: Arc) -> Option> { + let previous = self.columns.insert(key, chunk); + if previous.is_none() { + self.revision = next_revision(); + } + previous + } + pub fn remove(&mut self, key: &Key) -> Option> { + let previous = self.columns.remove(key); + if previous.is_some() { + self.revision = next_revision(); + } + previous + } + pub fn clear(&mut self) { + if !self.columns.is_empty() { + self.columns.clear(); + self.revision = next_revision(); + } + } + pub fn get_mut(&mut self, key: &Key) -> Option<&mut Arc> { + self.columns.get_mut(key) + } + pub fn values_mut(&mut self) -> ValuesMut<'_, Key, Arc> { + self.columns.values_mut() + } +} +impl Deref for ChunkMap { + type Target = Columns; + fn deref(&self) -> &Columns { + &self.columns + } +} +impl<'a> IntoIterator for &'a ChunkMap { + type Item = (&'a Key, &'a Arc); + type IntoIter = Iter<'a, Key, Arc>; + fn into_iter(self) -> Self::IntoIter { + self.columns.iter() + } +} +impl crate::light::LightWorld for ChunkMap { + fn state(&self, x: i32, y: i32, z: i32) -> Option { + self.columns.state(x, y, z) + } + fn light(&self, x: i32, y: i32, z: i32) -> Option { + self.columns.light(x, y, z) + } + fn set_light(&mut self, x: i32, y: i32, z: i32, packed: u8) { + self.columns.set_light(x, y, z, packed); + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn membership_tokens_track_keys_and_map_replacement_only() { + let mut map = ChunkMap::default(); + let empty = map.membership_revision(); + map.clear(); + map.remove(&(0, 0)); + assert_eq!(empty, map.membership_revision()); + let chunk = Arc::new(Chunk::empty(0, 0)); + map.insert((0, 0), chunk.clone()); + let inserted = map.membership_revision(); + assert_ne!(empty, inserted); + map.insert((0, 0), chunk.clone()); + Arc::make_mut(map.get_mut(&(0, 0)).unwrap()).lit = true; + for column in map.values_mut() { + Arc::make_mut(column).light_loaded = true; + } + crate::light::LightWorld::set_light(&mut map, 0, 0, 0, 0); + assert_eq!(inserted, map.membership_revision()); + map.remove(&(0, 0)); + map.insert((1, 0), chunk); + assert_eq!(map.len(), 1); + assert_ne!(inserted, map.membership_revision()); + let replaced = map.membership_revision(); + map.clear(); + assert_ne!(replaced, map.membership_revision()); + let cleared = map.membership_revision(); + map = ChunkMap::default(); + assert_ne!(cleared, map.membership_revision()); + } +} diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 07ba1af2a..315d8ede9 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -27,6 +27,7 @@ pub mod camera; pub mod cauldron; pub mod chat; pub mod chunk; +pub mod chunk_map; pub mod clipboard; pub mod clock; pub mod clouds; diff --git a/crates/horizon-client/src/world.rs b/crates/horizon-client/src/world.rs index 1b589041d..2d8213905 100644 --- a/crates/horizon-client/src/world.rs +++ b/crates/horizon-client/src/world.rs @@ -10,7 +10,7 @@ use std::{ pub struct World { pub fast_lava: bool, - pub chunks: crate::fast_hash::FxHashMap<(i32, i32), Arc>, + pub chunks: crate::chunk_map::ChunkMap, pub random: LegacyRandom, pub revision: u64, pub health_rules: crate::health::HealthRules,