@@ -27,10 +27,18 @@ use fnv::FnvHasher;
27
27
use twox_hash:: XxHash ;
28
28
use lru:: LruCache ;
29
29
30
+ use core:: default:: Default ;
31
+
30
32
use hashbrown:: HashMap ;
33
+ use sashstore_redleaf:: indexmap:: Index ;
34
+
35
+ use console:: println;
31
36
32
37
const TABLE_SIZE : usize = 65537 ;
33
- const CACHE_SIZE : usize = 1000 ;
38
+ const CACHE_SIZE : usize = 1 << 24 ;
39
+
40
+ static mut HIT_COUNT : usize = 0 ;
41
+ static mut HASHMAP_TOTAL : usize = 0 ;
34
42
35
43
type FnvHashFactory = BuildHasherDefault < FnvHasher > ;
36
44
type XxHashFactory = BuildHasherDefault < XxHash > ;
@@ -39,8 +47,9 @@ type XxHashFactory = BuildHasherDefault<XxHash>;
39
47
pub struct Maglev < N > {
40
48
pub nodes : Vec < N > ,
41
49
pub lookup : Vec < isize > ,
42
- pub cache : RefCell < HashMap < usize , usize > > ,
43
50
// pub cache: RefCell<LruCache<usize, usize>>, // hash -> backend
51
+ // pub cache: RefCell<HashMap<usize, usize, FnvHashFactory>>,
52
+ pub cache : RefCell < Index < usize , usize > > ,
44
53
}
45
54
46
55
impl < N : Hash + Eq > Maglev < N > {
@@ -49,7 +58,8 @@ impl<N: Hash + Eq> Maglev<N> {
49
58
let nodes = nodes. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
50
59
let lookup = Self :: populate ( & nodes) ;
51
60
// let cache = RefCell::new(LruCache::new(CACHE_SIZE));
52
- let cache = RefCell :: new ( HashMap :: with_capacity ( 3_000_000 ) ) ;
61
+ // let cache = RefCell::new(HashMap::with_capacity_and_hasher(CACHE_SIZE, Default::default()));
62
+ let cache = RefCell :: new ( Index :: with_capacity ( CACHE_SIZE ) ) ;
53
63
54
64
Maglev { nodes, lookup, cache }
55
65
}
@@ -134,18 +144,26 @@ impl<N: Hash + Eq> Maglev<N> {
134
144
#[ inline]
135
145
pub fn get_index_from_hash ( & self , hash : usize ) -> usize {
136
146
let mut cache = self . cache . borrow_mut ( ) ;
137
- match cache. get ( & hash) {
147
+ let mut set_cache = false ;
148
+ let x = match cache. get ( & hash) {
138
149
None => {
139
150
// Use lookup directly
140
- let idx = self . lookup [ hash % self . lookup . len ( ) ] as usize ;
141
- cache. insert ( hash, idx) ;
142
- idx
151
+ set_cache = true ;
152
+ self . lookup [ hash % self . lookup . len ( ) ] as usize
143
153
} ,
144
154
Some ( idx) => {
145
155
// Use cached backend
156
+ unsafe { HIT_COUNT += 1 ; }
146
157
* idx
147
158
} ,
159
+ } ;
160
+ unsafe { HASHMAP_TOTAL += 1 ; }
161
+
162
+ if set_cache {
163
+ cache. insert ( hash, x) ;
148
164
}
165
+
166
+ x
149
167
}
150
168
151
169
#[ inline]
@@ -155,4 +173,11 @@ impl<N: Hash + Eq> Maglev<N> {
155
173
{
156
174
& self . nodes [ self . get_index ( key) ]
157
175
}
176
+
177
+ pub fn dump_stats ( & self ) {
178
+ unsafe {
179
+ println ! ( "Hits: {}, total: {}" , HIT_COUNT , HASHMAP_TOTAL ) ;
180
+ }
181
+ sashstore_redleaf:: indexmap:: print_stats ( ) ;
182
+ }
158
183
}
0 commit comments