@@ -12,6 +12,7 @@ use std::collections::{HashMap, HashSet};
1212use std:: default:: Default ;
1313use std:: hash:: { Hasher , Hash , BuildHasherDefault } ;
1414use std:: ops:: BitXor ;
15+ use std:: mem:: size_of;
1516
1617pub type FxHashMap < K , V > = HashMap < K , V , BuildHasherDefault < FxHasher > > ;
1718pub type FxHashSet < V > = HashSet < V , BuildHasherDefault < FxHasher > > ;
@@ -62,10 +63,24 @@ impl FxHasher {
6263
6364impl Hasher for FxHasher {
6465 #[ inline]
65- fn write ( & mut self , bytes : & [ u8 ] ) {
66- for byte in bytes {
67- let i = * byte;
68- self . add_to_hash ( i as usize ) ;
66+ fn write ( & mut self , mut bytes : & [ u8 ] ) {
67+ unsafe {
68+ assert ! ( size_of:: <usize >( ) <= 8 ) ;
69+ while bytes. len ( ) >= size_of :: < usize > ( ) {
70+ self . add_to_hash ( * ( bytes. as_ptr ( ) as * const usize ) ) ;
71+ bytes = & bytes[ size_of :: < usize > ( ) ..] ;
72+ }
73+ if ( size_of :: < usize > ( ) > 4 ) && ( bytes. len ( ) >= 4 ) {
74+ self . add_to_hash ( * ( bytes. as_ptr ( ) as * const u32 ) as usize ) ;
75+ bytes = & bytes[ 4 ..] ;
76+ }
77+ if ( size_of :: < usize > ( ) > 2 ) && bytes. len ( ) >= 2 {
78+ self . add_to_hash ( * ( bytes. as_ptr ( ) as * const u16 ) as usize ) ;
79+ bytes = & bytes[ 2 ..] ;
80+ }
81+ if ( size_of :: < usize > ( ) > 1 ) && bytes. len ( ) >= 1 {
82+ self . add_to_hash ( bytes[ 0 ] as usize ) ;
83+ }
6984 }
7085 }
7186
0 commit comments