From 42cb342749891d38e8207a09329be54740218d7e Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 10 May 2023 00:48:53 +0200 Subject: [PATCH] world/layer: avoid iproduct-based iterator in inner loop iproduct iterators are fairly slow. As the iterator implementation is now unused, it is removed as well. --- src/types.rs | 14 -------------- src/world/layer.rs | 30 +++++++++++++++++------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/types.rs b/src/types.rs index 06e01f9..18045bb 100644 --- a/src/types.rs +++ b/src/types.rs @@ -73,20 +73,6 @@ impl LayerBlockCoords { #[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)] pub struct LayerBlockArray(pub [[T; BLOCKS_PER_CHUNK]; BLOCKS_PER_CHUNK]); -impl LayerBlockArray { - pub fn keys() -> impl Iterator + Clone + Debug { - iproduct!(BlockZ::iter(), BlockX::iter()).map(|(z, x)| LayerBlockCoords { x, z }) - } - - pub fn values(&self) -> impl Iterator + Clone + Debug { - Self::keys().map(|k| &self[k]) - } - - pub fn iter(&self) -> impl Iterator + Clone + Debug { - Self::keys().map(|k| (k, &self[k])) - } -} - impl Index for LayerBlockArray { type Output = T; diff --git a/src/world/layer.rs b/src/world/layer.rs index 440c137..4b23f26 100644 --- a/src/world/layer.rs +++ b/src/world/layer.rs @@ -111,21 +111,25 @@ pub fn top_layer(chunk: &Chunk) -> Result> { for section in chunk.sections().rev() { for y in BlockY::iter().rev() { - for xz in LayerBlockArray::<()>::keys() { - let mut entry = ret.entry(xz); - if entry.done() { - continue; - } + for z in BlockZ::iter() { + for x in BlockX::iter() { + let xz = LayerBlockCoords { x, z }; - let coords = SectionBlockCoords { xz, y }; - if !entry.fill(section, coords)? { - continue; - } + let mut entry = ret.entry(xz); + if entry.done() { + continue; + } - assert!(entry.done()); - done += 1; - if done == N * N { - break; + let coords = SectionBlockCoords { xz, y }; + if !entry.fill(section, coords)? { + continue; + } + + assert!(entry.done()); + done += 1; + if done == N * N { + break; + } } } }