@@ -19,8 +19,9 @@ extern crate criterion;
1919extern crate rand;
2020
2121use concread:: hashmap:: * ;
22- use criterion:: { black_box, criterion_group, criterion_main, BatchSize , Criterion } ;
23- use rand:: { thread_rng, Rng } ;
22+ use criterion:: { criterion_group, criterion_main, BatchSize , Criterion } ;
23+ use rand:: { rng, Rng } ;
24+ use std:: hint:: black_box;
2425
2526// ranges of counts for different benchmarks (MINs are inclusive, MAXes exclusive):
2627const INSERT_COUNT_MIN : usize = 120 ;
@@ -168,7 +169,7 @@ criterion_main!(insert, remove, search);
168169fn insert_vec < V : Clone + Sync + Send + ' static > (
169170 map : & mut HashMap < u32 , V > ,
170171 list : Vec < ( u32 , V ) > ,
171- ) -> HashMapWriteTxn < u32 , V > {
172+ ) -> HashMapWriteTxn < ' _ , u32 , V > {
172173 let mut write_txn = map. write ( ) ;
173174 for ( key, val) in list. into_iter ( ) {
174175 write_txn. insert ( key, val) ;
@@ -178,7 +179,7 @@ fn insert_vec<V: Clone + Sync + Send + 'static>(
178179
179180fn remove_vec < ' a , V : Clone + Sync + Send + ' static > (
180181 map : & ' a mut HashMap < u32 , V > ,
181- list : & Vec < u32 > ,
182+ list : & [ u32 ] ,
182183) -> HashMapWriteTxn < ' a , u32 , V > {
183184 let mut write_txn = map. write ( ) ;
184185 for i in list. iter ( ) {
@@ -187,7 +188,7 @@ fn remove_vec<'a, V: Clone + Sync + Send + 'static>(
187188 write_txn
188189}
189190
190- fn search_vec < V : Clone + Sync + Send + ' static > ( map : & HashMap < u32 , V > , list : & Vec < u32 > ) {
191+ fn search_vec < V : Clone + Sync + Send + ' static > ( map : & HashMap < u32 , V > , list : & [ u32 ] ) {
191192 let read_txn = map. read ( ) ;
192193 for i in list. iter ( ) {
193194 read_txn. get ( black_box ( i) ) ;
@@ -242,23 +243,23 @@ struct Struct {
242243}
243244
244245fn prepare_insert < V : Clone + Sync + Send + ' static > ( value : V ) -> ( HashMap < u32 , V > , Vec < ( u32 , V ) > ) {
245- let mut rng = thread_rng ( ) ;
246- let count = rng. gen_range ( INSERT_COUNT_MIN ..INSERT_COUNT_MAX ) ;
246+ let mut rng = rng ( ) ;
247+ let count = rng. random_range ( INSERT_COUNT_MIN ..INSERT_COUNT_MAX ) ;
247248 let mut list = Vec :: with_capacity ( count) ;
248249 for _ in 0 ..count {
249250 list. push ( (
250- rng. gen_range ( 0 ..INSERT_COUNT_MAX << 8 ) as u32 ,
251- value. clone ( ) ,
251+ rng. random_range ( 0 ..INSERT_COUNT_MAX << 8 ) as u32 ,
252+ value. to_owned ( ) ,
252253 ) ) ;
253254 }
254255 ( HashMap :: new ( ) , list)
255256}
256257
257258/// Prepares a remove benchmark with values in the HashMap being clones of the 'value' parameter
258259fn prepare_remove < V : Clone + Sync + Send + ' static > ( value : V ) -> ( HashMap < u32 , V > , Vec < u32 > ) {
259- let mut rng = thread_rng ( ) ;
260- let insert_count = rng. gen_range ( INSERT_COUNT_FOR_REMOVE_MIN ..INSERT_COUNT_FOR_REMOVE_MAX ) ;
261- let remove_count = rng. gen_range ( REMOVE_COUNT_MIN ..REMOVE_COUNT_MAX ) ;
260+ let mut rng = rng ( ) ;
261+ let insert_count = rng. random_range ( INSERT_COUNT_FOR_REMOVE_MIN ..INSERT_COUNT_FOR_REMOVE_MAX ) ;
262+ let remove_count = rng. random_range ( REMOVE_COUNT_MIN ..REMOVE_COUNT_MAX ) ;
262263 let map = HashMap :: new ( ) ;
263264 let mut write_txn = map. write ( ) ;
264265 for i in random_order ( insert_count, insert_count) . iter ( ) {
@@ -271,10 +272,10 @@ fn prepare_remove<V: Clone + Sync + Send + 'static>(value: V) -> (HashMap<u32, V
271272}
272273
273274fn prepare_search < V : Clone + Sync + Send + ' static > ( value : V ) -> ( HashMap < u32 , V > , Vec < u32 > ) {
274- let mut rng = thread_rng ( ) ;
275- let insert_count = rng. gen_range ( INSERT_COUNT_FOR_SEARCH_MIN ..INSERT_COUNT_FOR_SEARCH_MAX ) ;
275+ let mut rng = rng ( ) ;
276+ let insert_count = rng. random_range ( INSERT_COUNT_FOR_SEARCH_MIN ..INSERT_COUNT_FOR_SEARCH_MAX ) ;
276277 let search_limit = insert_count * SEARCH_SIZE_NUMERATOR / SEARCH_SIZE_DENOMINATOR ;
277- let search_count = rng. gen_range ( SEARCH_COUNT_MIN ..SEARCH_COUNT_MAX ) ;
278+ let search_count = rng. random_range ( SEARCH_COUNT_MIN ..SEARCH_COUNT_MAX ) ;
278279
279280 // Create a HashMap with elements 0 through insert_count(-1)
280281 let map = HashMap :: new ( ) ;
@@ -287,28 +288,28 @@ fn prepare_search<V: Clone + Sync + Send + 'static>(value: V) -> (HashMap<u32, V
287288 // Choose 'search_count' numbers from [0,search_limit) randomly to be searched in the created map.
288289 let mut list = Vec :: with_capacity ( search_count) ;
289290 for _ in 0 ..search_count {
290- list. push ( rng. gen_range ( 0 ..search_limit as u32 ) ) ;
291+ list. push ( rng. random_range ( 0 ..search_limit as u32 ) ) ;
291292 }
292293 ( map, list)
293294}
294295
295296/// Returns a Vec of n elements from the range [0,up_to) in random order without repetition
296297fn random_order ( up_to : usize , n : usize ) -> Vec < u32 > {
297- let mut rng = thread_rng ( ) ;
298+ let mut rng = rng ( ) ;
298299 let mut order = Vec :: with_capacity ( n) ;
299300 let mut generated = vec ! [ false ; up_to] ;
300301 let mut remaining = n;
301302 let mut remaining_elems = up_to;
302303 while remaining > 0 {
303- let mut r = rng. gen_range ( 0 ..remaining_elems) ;
304+ let mut r = rng. random_range ( 0 ..remaining_elems) ;
304305 // find the r-th yet nongenerated number:
305- for i in 0 .. up_to {
306- if generated [ i ] {
306+ for ( i , value ) in generated . iter_mut ( ) . enumerate ( ) . take ( up_to) {
307+ if * value {
307308 continue ;
308309 }
309310 if r == 0 {
310311 order. push ( i as u32 ) ;
311- generated [ i ] = true ;
312+ * value = true ;
312313 break ;
313314 }
314315 r -= 1 ;
0 commit comments