From e5c38434423e3f0e936c6b9718063866b3e3347c Mon Sep 17 00:00:00 2001 From: iximeow Date: Fri, 3 Jan 2020 12:09:00 -0800 Subject: custom hasher for regspec for hashmaps with heavy traffic keyed on RegSpec, this can be a significant time savings --- src/lib.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 81bc4aa..2427f4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,18 +14,27 @@ use std::hint::unreachable_unchecked; use yaxpeax_arch::{Arch, Decoder, LengthedInstruction}; #[cfg(feature="use-serde")] -#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct RegSpec { pub num: u8, pub bank: RegisterBank } #[cfg(not(feature="use-serde"))] -#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct RegSpec { pub num: u8, pub bank: RegisterBank } +use std::hash::Hash; +use std::hash::Hasher; +impl Hash for RegSpec { + fn hash(&self, state: &mut H) { + let code = ((self.bank as u16) << 8) | (self.num as u16); + code.hash(state); + } +} + // This is only to select alternate opcode maps for the 0f escape byte. // This often could be treated as a size prefix but in some cases selects // an entirely different operation. -- cgit v1.1