From 23e982f8f28e5fb816e803e66b3a3ed1aa6d0506 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 4 Jan 2026 22:41:27 +1100 Subject: slightly faster hash impl for pathfinder RelBlockPos --- azalea/src/pathfinder/rel_block_pos.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/azalea/src/pathfinder/rel_block_pos.rs b/azalea/src/pathfinder/rel_block_pos.rs index 6b5fa69a..b8e96041 100644 --- a/azalea/src/pathfinder/rel_block_pos.rs +++ b/azalea/src/pathfinder/rel_block_pos.rs @@ -1,4 +1,8 @@ -use std::ops::{Add, Mul}; +use std::{ + hash::{Hash, Hasher}, + mem::transmute, + ops::{Add, Mul}, +}; use azalea_core::position::BlockPos; @@ -7,7 +11,7 @@ use azalea_core::position::BlockPos; /// This fits in 64 bits, so it's more efficient than a BlockPos in some cases. /// /// The X and Z are limited to ±32k. -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[repr(C)] pub struct RelBlockPos { pub x: i16, @@ -89,6 +93,12 @@ impl RelBlockPos { z: self.z, } } + + #[inline] + pub fn as_u64(self) -> u64 { + // SAFETY: RelBlockPos can be represented as a u64 + unsafe { transmute::(self) } + } } impl Add for RelBlockPos { @@ -113,3 +123,9 @@ impl Mul for RelBlockPos { } } } + +impl Hash for RelBlockPos { + fn hash(&self, state: &mut H) { + self.as_u64().hash(state); + } +} -- cgit v1.2.3