diff --git a/world/src/block/mod.rs b/world/src/block/mod.rs index bf2795383a..51428b6ba4 100644 --- a/world/src/block/mod.rs +++ b/world/src/block/mod.rs @@ -84,7 +84,7 @@ impl<'a> BlockGen<'a> { } = self; // Main sample - let sample = Self::sample_column(column_gen, column_cache, wpos)?; + let sample = column_gen.get(wpos)?; // Tree samples let mut structure_samples = [None, None, None, None, None, None, None, None, None]; diff --git a/world/src/util/small_cache.rs b/world/src/util/small_cache.rs index c2338f383c..53e6bb9fab 100644 --- a/world/src/util/small_cache.rs +++ b/world/src/util/small_cache.rs @@ -9,23 +9,25 @@ fn calc_idx(v: Vec2) -> usize { (x ^ y) as usize } +const CACHE_LEN: usize = 32; + pub struct SmallCache { - index: [Option>; 137], - data: [V; 137], + index: [Option>; CACHE_LEN + 9], + data: [V; CACHE_LEN + 9], random: u32, } impl Default for SmallCache { fn default() -> Self { Self { - index: arr![None; 137], - data: arr![V::default(); 137], + index: [None; CACHE_LEN + 9], + data: arr![V::default(); 41], // TODO: Use CACHE_LEN random: 1, } } } impl SmallCache { pub fn get) -> V>(&mut self, key: Vec2, f: F) -> &V { - let idx = calc_idx(key) % 128; + let idx = calc_idx(key) % CACHE_LEN; // Search if self.index[idx].as_ref().map(|k| k == &key).unwrap_or(false) { @@ -67,93 +69,3 @@ impl SmallCache { &self.data[idx] } } - -/*pub struct SmallCache { - index: [Option>; 128], - data: [V; 128], - random: u32, -} -impl Default for SmallCache { - fn default() -> Self { - Self { - index: arr![None; 128], - data: arr![V::default(); 128], - random: 1, - } - } -} -impl SmallCache { - pub fn get) -> V>(&mut self, key: Vec2, f: F) -> &V { - let idx = calc_idx(key) % 32 * 4; - - // Search - if self.index[idx].as_ref().map(|k| k == &key).unwrap_or(false) { - return &self.data[idx]; - } else if self.index[idx + 1] - .as_ref() - .map(|k| k == &key) - .unwrap_or(false) - { - return &self.data[idx + 1]; - } else if self.index[idx + 2] - .as_ref() - .map(|k| k == &key) - .unwrap_or(false) - { - return &self.data[idx + 2]; - } else if self.index[idx + 3] - .as_ref() - .map(|k| k == &key) - .unwrap_or(false) - { - return &self.data[idx + 3]; - } - // Not present so insert - for i in 0..4 { - let idx = idx + i; - if self.index[idx].is_none() { - self.index[idx] = Some(key.clone()); - self.data[idx] = f(key); - return &self.data[idx]; - } - } - let idx = idx + super::seed_expan::diffuse(self.random) as usize % 4; - self.random = self.random.wrapping_add(1); - self.index[idx] = Some(key.clone()); - self.data[idx] = f(key); - &self.data[idx] - } -}*/ - -/*const ZCACHE_SIZE: usize = 32; - -#[derive(Default)] -pub struct ZestSmallCache { - index: [Option; ZCACHE_SIZE], - cache: [V; ZCACHE_SIZE], - not_cached: V, -} - -impl ZestSmallCache { - pub fn get V>(&mut self, key: K, cache: bool, f: F) -> &V { - for i in 0..ZCACHE_SIZE { - if self.index[i].as_ref().map(|k| k == &key).unwrap_or(false) { - return &self.cache[i]; - } - } - if !cache { - self.not_cached = f(key); - return &self.not_cached; - } - for i in 0..ZCACHE_SIZE { - if self.index[i].is_none() { - self.index[i] = Some(key.clone()); - self.cache[i] = f(key.clone()); - return &self.cache[i]; - } - } - self.index[0] = Some(key.clone()); - self.cache[0] = f(key); - &self.cache[0] - } -}*/