From 427a992897781bd0fbcd7085227fdd86788936c4 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 15 Aug 2023 20:48:41 +0200 Subject: [PATCH] minedmap/tile_renderer: keep HashSet of populated regions Rather than attemping to read the file metadata, it's more elegant to check whether a tile is supposed to be available first. --- src/bin/minedmap/common.rs | 2 +- src/bin/minedmap/region_group.rs | 23 +++++++++++------------ src/bin/minedmap/tile_renderer.rs | 7 ++++++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/bin/minedmap/common.rs b/src/bin/minedmap/common.rs index c0c4c8c..48cebf8 100644 --- a/src/bin/minedmap/common.rs +++ b/src/bin/minedmap/common.rs @@ -12,7 +12,7 @@ use minedmap::{io::fs::FileMetaVersion, resource::Biome, types::*, world::layer} // Increase to force regeneration of all output files pub const FILE_META_VERSION: FileMetaVersion = FileMetaVersion(0); -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct TileCoords { pub x: i32, pub z: i32, diff --git a/src/bin/minedmap/region_group.rs b/src/bin/minedmap/region_group.rs index 1f87b1c..be46511 100644 --- a/src/bin/minedmap/region_group.rs +++ b/src/bin/minedmap/region_group.rs @@ -16,25 +16,24 @@ pub struct RegionGroup { } impl RegionGroup { - pub fn new(f: F) -> Result + pub fn new(f: F) -> Self where - F: Fn(i8, i8) -> Result, + F: Fn(i8, i8) -> Option, { RegionGroup { - center: (0, 0), + center: f(0, 0).expect("Center element of RegionGroup must not be None"), neighs: [ - Some((-1, -1)), - Some((-1, 0)), - Some((-1, 1)), - Some((0, -1)), + f(-1, -1), + f(-1, 0), + f(-1, 1), + f(0, -1), None, - Some((0, 1)), - Some((1, -1)), - Some((1, 0)), - Some((1, 1)), + f(0, 1), + f(1, -1), + f(1, 0), + f(1, 1), ], } - .try_map(|(x, z)| f(x, z)) } pub fn center(&self) -> &T { diff --git a/src/bin/minedmap/tile_renderer.rs b/src/bin/minedmap/tile_renderer.rs index 048cf77..bc59b23 100644 --- a/src/bin/minedmap/tile_renderer.rs +++ b/src/bin/minedmap/tile_renderer.rs @@ -51,6 +51,7 @@ pub struct TileRenderer<'a> { config: &'a Config, rt: &'a tokio::runtime::Runtime, regions: &'a [TileCoords], + region_set: rustc_hash::FxHashSet, region_cache: Mutex>>>, } @@ -63,10 +64,12 @@ impl<'a> TileRenderer<'a> { let region_cache = Mutex::new(LruCache::new( NonZeroUsize::new(6 + 6 * config.num_threads).unwrap(), )); + let region_set = regions.iter().copied().collect(); TileRenderer { config, rt, regions, + region_set, region_cache, } } @@ -204,11 +207,13 @@ impl<'a> TileRenderer<'a> { fn processed_sources(&self, coords: TileCoords) -> Result<(RegionGroup, SystemTime)> { let sources = RegionGroup::new(|x, z| { - self.processed_source(TileCoords { + Some(TileCoords { x: coords.x + (x as i32), z: coords.z + (z as i32), }) + .filter(|entry| self.region_set.contains(entry)) }) + .try_map(|entry| self.processed_source(entry)) .with_context(|| format!("Region {:?} from previous step must exist", coords))?; let max_timestamp = *sources