@@ -12,6 +12,7 @@ use std::collections::{HashMap, HashSet};
12
12
use std:: default:: Default ;
13
13
use std:: hash:: { Hasher , Hash , BuildHasherDefault } ;
14
14
use std:: ops:: BitXor ;
15
+ use std:: mem:: size_of;
15
16
16
17
pub type FxHashMap < K , V > = HashMap < K , V , BuildHasherDefault < FxHasher > > ;
17
18
pub type FxHashSet < V > = HashSet < V , BuildHasherDefault < FxHasher > > ;
@@ -62,10 +63,24 @@ impl FxHasher {
62
63
63
64
impl Hasher for FxHasher {
64
65
#[ 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
+ }
69
84
}
70
85
}
71
86
0 commit comments