@@ -2,7 +2,7 @@ use std::hash::Hash;
2
2
3
3
use crate :: data_structures:: { HashMap , HashSet } ;
4
4
5
- const CACHE_CUTOFF : u32 = 16 ;
5
+ const CACHE_CUTOFF : u32 = 32 ;
6
6
7
7
/// A hashmap which only starts hashing after ignoring the first few inputs.
8
8
///
@@ -22,18 +22,35 @@ impl<K, V> Default for DelayedMap<K, V> {
22
22
}
23
23
24
24
impl < K : Hash + Eq , V > DelayedMap < K , V > {
25
- #[ inline]
25
+ #[ inline( always ) ]
26
26
pub fn insert ( & mut self , key : K , value : V ) -> bool {
27
27
if self . count >= CACHE_CUTOFF {
28
- self . cache . insert ( key, value) . is_none ( )
28
+ self . cold_insert ( key, value)
29
29
} else {
30
30
self . count += 1 ;
31
31
true
32
32
}
33
33
}
34
34
35
- #[ inline]
35
+ #[ cold]
36
+ #[ inline( never) ]
37
+ fn cold_insert ( & mut self , key : K , value : V ) -> bool {
38
+ self . cache . insert ( key, value) . is_none ( )
39
+ }
40
+
41
+
42
+ #[ inline( always) ]
36
43
pub fn get ( & self , key : & K ) -> Option < & V > {
44
+ if self . cache . is_empty ( ) {
45
+ None
46
+ } else {
47
+ self . cold_get ( key)
48
+ }
49
+ }
50
+
51
+ #[ cold]
52
+ #[ inline( never) ]
53
+ fn cold_get ( & self , key : & K ) -> Option < & V > {
37
54
self . cache . get ( key)
38
55
}
39
56
}
@@ -51,18 +68,30 @@ impl<T> Default for DelayedSet<T> {
51
68
}
52
69
53
70
impl < T : Hash + Eq > DelayedSet < T > {
54
- #[ inline]
71
+ #[ inline( always ) ]
55
72
pub fn insert ( & mut self , value : T ) -> bool {
56
73
if self . count >= CACHE_CUTOFF {
57
- self . cache . insert ( value)
74
+ self . cold_insert ( value)
58
75
} else {
59
76
self . count += 1 ;
60
77
true
61
78
}
62
79
}
63
80
64
- #[ inline]
81
+ #[ cold]
82
+ #[ inline( never) ]
83
+ fn cold_insert ( & mut self , value : T ) -> bool {
84
+ self . cache . insert ( value)
85
+ }
86
+
87
+ #[ inline( always) ]
65
88
pub fn contains ( & self , value : & T ) -> bool {
89
+ !self . cache . is_empty ( ) && self . cold_contains ( value)
90
+ }
91
+
92
+ #[ cold]
93
+ #[ inline( never) ]
94
+ fn cold_contains ( & self , value : & T ) -> bool {
66
95
self . cache . contains ( value)
67
96
}
68
97
}
0 commit comments