From 04bc3e5d53050cb861117ce4403a751bb08dea12 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 2 Mar 2023 00:37:25 +0100 Subject: [PATCH] world/section: perform palette lookups early Look up block types for all palette entries once during section construction, rather than on block_at(), reducing the number of HashMap lookups by ~1000. --- src/world/section.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/world/section.rs b/src/world/section.rs index 471f5d0..8c98a68 100644 --- a/src/world/section.rs +++ b/src/world/section.rs @@ -67,8 +67,7 @@ impl<'a> BiomesV18<'a> { #[derive(Debug)] pub struct SectionV1_13<'a> { block_states: Option<&'a fastnbt::LongArray>, - palette: &'a Vec, - block_types: &'a BlockTypes, + palette: Vec>, bits: u8, aligned_blocks: bool, } @@ -97,10 +96,20 @@ impl<'a> SectionV1_13<'a> { } } + let palette_types = palette + .iter() + .map(|entry| { + let block_type = block_types.get(&entry.name); + if block_type.is_none() { + eprintln!("Unknown block type: {}", entry.name); + } + block_type + }) + .collect(); + Ok(Self { block_states, - palette, - block_types, + palette: palette_types, bits, aligned_blocks, }) @@ -139,16 +148,10 @@ impl<'a> SectionV1_13<'a> { impl<'a> Section for SectionV1_13<'a> { fn block_at(&self, coords: SectionBlockCoords) -> Result> { let index = self.palette_index_at(coords); - let entry = self + Ok(*self .palette .get(index) - .context("Palette index out of bounds")?; - - let block_type = self.block_types.get(&entry.name); - if block_type.is_none() { - eprintln!("Unknown block type: {}", entry.name); - } - Ok(block_type) + .context("Palette index out of bounds")?) } }