From 203d178dab15206a6c19b7e8063f9f397373b118 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 4 Jan 2026 20:48:08 +0930 Subject: small pathfinder optimization by improving cache locality for cached_mining_costs --- azalea/src/pathfinder/world.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'azalea/src') diff --git a/azalea/src/pathfinder/world.rs b/azalea/src/pathfinder/world.rs index 980cda9a..819e4254 100644 --- a/azalea/src/pathfinder/world.rs +++ b/azalea/src/pathfinder/world.rs @@ -314,10 +314,7 @@ impl CachedWorld { let cached_mining_costs = unsafe { &mut *self.cached_mining_costs.get() }; // 20 bits total: // 8 bits for x, 4 bits for y, 8 bits for z - let hash_index = ((pos.x as usize & 0xff) << 12) - | ((pos.y as usize & 0xf) << 8) - | (pos.z as usize & 0xff); - debug_assert!(hash_index < 1048576); + let hash_index = Self::calculate_cached_mining_costs_index(pos); let &(cached_pos, potential_cost) = unsafe { cached_mining_costs.get_unchecked(hash_index) }; if cached_pos == pos { @@ -332,6 +329,16 @@ impl CachedWorld { cost } + fn calculate_cached_mining_costs_index(pos: RelBlockPos) -> usize { + // 20 bits total: + // 8 bits for x, 8 bits for z, 4 bits for y + let hash_index = ((pos.x as usize & 0xff) << 12) + | ((pos.z as usize & 0xff) << 4) + | (pos.y as usize & 0xf); + debug_assert!(hash_index < 1048576); + hash_index + } + fn uncached_cost_for_breaking_block( &self, pos: RelBlockPos, -- cgit v1.2.3