From 3e353bcb63cd035940961c6363641457e491683b Mon Sep 17 00:00:00 2001 From: Purpzie <25022704+Purpzie@users.noreply.github.com> Date: Sat, 27 Aug 2022 15:36:06 -0500 Subject: [PATCH] perf: faster HashMap with TypeId keys --- src/lua.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index b6d37900..b151673d 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -1,6 +1,7 @@ use std::any::TypeId; use std::cell::RefCell; use std::collections::HashMap; +use std::hash::{BuildHasherDefault, Hasher}; use std::marker::PhantomData; #[cfg(rlua_lua54)] use std::os::raw::c_uint; @@ -453,9 +454,30 @@ impl Default for Lua { } } +/// TypeIds are already hashes from the compiler, so they don't need to be hashed again. +/// This dummy hasher simply returns the value given to it by TypeId. +#[derive(Default)] +pub(crate) struct TypeIdHasher(u64); + +impl Hasher for TypeIdHasher { + fn write(&mut self, _: &[u8]) { + unreachable!("TypeId calls write_u64"); + } + + #[inline] + fn write_u64(&mut self, type_id: u64) { + self.0 = type_id; + } + + #[inline] + fn finish(&self) -> u64 { + self.0 + } +} + // Data associated with the main lua_State via lua_getextraspace. pub(crate) struct ExtraData { - pub registered_userdata: HashMap, + pub(crate) registered_userdata: HashMap>, pub registry_unref_list: Arc>>>, pub ref_thread: *mut ffi::lua_State, @@ -526,7 +548,7 @@ unsafe fn create_lua(lua_mod_to_load: StdLib, init_flags: InitFlags) -> Lua { } let mut extra = Box::new(ExtraData { - registered_userdata: HashMap::new(), + registered_userdata: HashMap::default(), registry_unref_list: Arc::new(Mutex::new(Some(Vec::new()))), ref_thread: ptr::null_mut(), // We need 1 extra stack space to move values in and out of the ref stack.