From 35fa918df13a0fcc6e73c938f4a6d2d48f0bec8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Sun, 27 May 2018 15:58:52 +0200 Subject: [PATCH 1/3] Hash up to 8 bytes at once with FxHasher --- Cargo.lock | 12 +++++++++++- Cargo.toml | 1 + src/lib.rs | 32 ++++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0c64114..90e6500 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,4 +1,14 @@ +[[package]] +name = "byteorder" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rustc-hash" -version = "0.1.0" +version = "1.0.0" +dependencies = [ + "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] +[metadata] +"checksum byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "74c0b906e9446b0a2e4f760cdb3fa4b2c48cdc6db8766a845c54b6ff063fd2e9" diff --git a/Cargo.toml b/Cargo.toml index 8b24709..02bbd95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ keywords = ["hash", "fxhash", "rustc"] repository = "https://github.com/rust-lang-nursery/rustc-hash" [dependencies] +byteorder = "1.1" diff --git a/src/lib.rs b/src/lib.rs index ec592f9..b3875cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,10 +18,15 @@ //! map.insert(22, 44); //! ``` +extern crate byteorder; + use std::collections::{HashMap, HashSet}; use std::default::Default; use std::hash::{Hasher, BuildHasherDefault}; use std::ops::BitXor; +use std::mem::size_of; + +use byteorder::{ByteOrder, NativeEndian}; /// Type alias for a hashmap using the `fx` hash algorithm. pub type FxHashMap = HashMap>; @@ -65,11 +70,30 @@ impl FxHasher { impl Hasher for FxHasher { #[inline] - fn write(&mut self, bytes: &[u8]) { - for byte in bytes { - let i = *byte; - self.add_to_hash(i as usize); + fn write(&mut self, mut bytes: &[u8]) { + #[cfg(target_pointer_width = "32")] + let read_usize = |bytes| NativeEndian::read_u32(bytes); + #[cfg(target_pointer_width = "64")] + let read_usize = |bytes| NativeEndian::read_u64(bytes); + + let mut hash = FxHasher { hash: self.hash }; + assert!(size_of::() <= 8); + while bytes.len() >= size_of::() { + hash.add_to_hash(read_usize(bytes) as usize); + bytes = &bytes[size_of::()..]; + } + if (size_of::() > 4) && (bytes.len() >= 4) { + hash.add_to_hash(NativeEndian::read_u32(bytes) as usize); + bytes = &bytes[4..]; + } + if (size_of::() > 2) && bytes.len() >= 2 { + hash.add_to_hash(NativeEndian::read_u16(bytes) as usize); + bytes = &bytes[2..]; + } + if (size_of::() > 1) && bytes.len() >= 1 { + hash.add_to_hash(bytes[0] as usize); } + self.hash = hash.hash; } #[inline] From 8640781fc73a0e931f854825b42f12ba9e72afb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Mon, 28 May 2018 16:33:54 +0200 Subject: [PATCH 2/3] Remove Cargo.lock --- .gitignore | 1 + Cargo.lock | 14 -------------- 2 files changed, 1 insertion(+), 14 deletions(-) delete mode 100644 Cargo.lock diff --git a/.gitignore b/.gitignore index 53eaa21..84c47ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target **/*.rs.bk +/Cargo.lock diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 90e6500..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,14 +0,0 @@ -[[package]] -name = "byteorder" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "rustc-hash" -version = "1.0.0" -dependencies = [ - "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[metadata] -"checksum byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "74c0b906e9446b0a2e4f760cdb3fa4b2c48cdc6db8766a845c54b6ff063fd2e9" From 5c4b6e92b4829589449bde3b27adf964d5b43d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Mon, 28 May 2018 16:34:05 +0200 Subject: [PATCH 3/3] Bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 02bbd95..9eb26f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustc-hash" -version = "1.0.0" +version = "1.0.1" authors = ["The Rust Project Developers"] description = "speed, non-cryptographic hash used in rustc" license = "Apache-2.0/MIT"