From edfd2290eb63ced7dd21a44d612705ae4ccfb0e1 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Mon, 13 May 2019 10:32:02 +0100 Subject: [PATCH] Massively sped up VolMap offset calculations Former-commit-id: 8f3cdf57a77691ca60c0921bc86a79c8cfe36539 --- common/src/state.rs | 2 +- common/src/volumes/vol_map.rs | 29 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/common/src/state.rs b/common/src/state.rs index 29cdb59f4a..509ca67ca7 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -110,7 +110,7 @@ impl State { ecs.add_resource(TimeOfDay(0.0)); ecs.add_resource(Time(0.0)); ecs.add_resource(DeltaTime(0.0)); - ecs.add_resource(TerrainMap::new()); + ecs.add_resource(TerrainMap::new().unwrap()); } /// Register a component with the state's ECS diff --git a/common/src/volumes/vol_map.rs b/common/src/volumes/vol_map.rs index d73ede7ee1..aa7c297428 100644 --- a/common/src/volumes/vol_map.rs +++ b/common/src/volumes/vol_map.rs @@ -19,6 +19,7 @@ pub enum VolMapErr { NoSuchChunk, ChunkErr(ChunkErr), DynaErr(DynaErr), + InvalidChunkSize, } // V = Voxel @@ -31,13 +32,20 @@ pub struct VolMap { impl VolMap { #[inline(always)] - fn chunk_key(pos: Vec3) -> Vec3 { - pos.map2(S::SIZE, |e, sz| e.div_euclid(sz as i32)) + pub fn chunk_key(pos: Vec3) -> Vec3 { + pos.map2(S::SIZE, |e, sz| { + // Horrid, but it's faster than a cheetah with a red bull blood transfusion + let log2 = (sz - 1).count_ones(); + ((((e as i64 + (1 << 32)) as u64) >> log2) - (1 << (32 - log2))) as i32 + }) } #[inline(always)] pub fn chunk_offs(pos: Vec3) -> Vec3 { - pos.map2(S::SIZE, |e, sz| e.rem_euclid(sz as i32)) + pos.map2(S::SIZE, |e, sz| { + // Horrid, but it's even faster than the aforementioned cheetah + (((e as i64 + (1 << 32)) as u64) & (sz - 1) as u64) as i32 + }) } } @@ -150,10 +158,17 @@ impl WriteVol for VolMap } } -impl VolMap { - pub fn new() -> Self { - Self { - chunks: HashMap::new(), +impl VolMap { + pub fn new() -> Result { + if Self::chunk_size() + .map(|e| e.is_power_of_two() && e > 0) + .reduce_and() + { + Ok(Self { + chunks: HashMap::new(), + }) + } else { + Err(VolMapErr::InvalidChunkSize) } }