@@ -33,6 +33,16 @@ const LOAD_FACTOR_THRESHOLD: f32 = 0.625;
33
33
// The displacement threshold should be high enough so that even with the maximal load factor,
34
34
// it's very rarely exceeded.
35
35
// As the load approaches 90%, displacements larger than ~ 32 are much more probable.
36
+ // On the other hand, the threshold should be low enough so that the same number of hashes
37
+ // easily fits in the cache and takes a reasonable time to iterate through.
38
+
39
+ // The load factor threshold should be relatively low, but high enough so that its half is not very
40
+ // low (~ 20%). We choose 62.5%, because it's a simple fraction (5/8), and its half is 31.25%.
41
+ // (When a map is grown, the load factor is halved.)
42
+
43
+ // TODO: add one-shot hashing for String, str, arrays and other types.
44
+ // TODO: consider adding a limit for the number of fully equal hashes in a probe sequence.
45
+ // Fully equal hashes cause key comparison, which might be a problem for large string keys.
36
46
37
47
// Avoid problems with private types in public interfaces.
38
48
pub type InternalEntryMut < ' a , K : ' a , V : ' a > = InternalEntry < K , V , & ' a mut RawTable < K , V > > ;
@@ -65,7 +75,7 @@ impl<K, V> SafeguardedSearch<K, V> for HashMap<K, V, AdaptiveState>
65
75
let entry = search_hashed ( DerefMapToTable ( self ) , hash, |k| k == key) ;
66
76
match entry {
67
77
InternalEntry :: Occupied { elem } => {
68
- // This should compile down to a no-op .
78
+ // This should compile down to a simple copy .
69
79
InternalEntry :: Occupied { elem : elem. convert_table ( ) }
70
80
}
71
81
InternalEntry :: TableIsEmpty => {
@@ -102,12 +112,13 @@ fn safeguard_vacant_entry<'a, K, V>(
102
112
} ;
103
113
// Copied from FullBucket::displacement.
104
114
let displacement = index. wrapping_sub ( hash. inspect ( ) as usize ) & ( table_capacity - 1 ) ;
115
+ // Check displacement.
105
116
if displacement > DISPLACEMENT_THRESHOLD {
106
117
// Probe sequence is too long.
107
118
// This branch is very unlikely.
108
119
maybe_adapt_to_safe_hashing ( elem, key, hash)
109
120
} else {
110
- // This should compile down to a no-op .
121
+ // This should compile down to a simple copy .
111
122
match elem {
112
123
NeqElem ( bucket, ib) => {
113
124
InternalEntry :: Vacant {
@@ -125,7 +136,7 @@ fn safeguard_vacant_entry<'a, K, V>(
125
136
}
126
137
}
127
138
128
- // Adapt to safe hashing if desirable.
139
+ // Adapt to safe hashing, if desirable.
129
140
#[ cold]
130
141
fn maybe_adapt_to_safe_hashing < ' a , K , V > (
131
142
elem : VacantEntryState < K , V , DerefMapToTable < ' a , K , V , AdaptiveState > > ,
0 commit comments