1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #![allow(non_snake_case)]
6 
7 extern crate fnv;
8 extern crate fxhash;
9 
10 use fnv::FnvHashSet;
11 use fxhash::FxHashSet;
12 use std::collections::HashSet;
13 use std::os::raw::{c_char, c_void};
14 use std::slice;
15 
16 /// Keep this in sync with Params in Bench.cpp.
17 #[derive(Debug)]
18 #[repr(C)]
19 pub struct Params {
20     config_name: *const c_char,
21     num_inserts: usize,
22     num_successful_lookups: usize,
23     num_failing_lookups: usize,
24     num_iterations: usize,
25     remove_inserts: bool,
26 }
27 
28 #[no_mangle]
Bench_Rust_HashSet( params: *const Params, vals: *const *const c_void, len: usize, )29 pub extern "C" fn Bench_Rust_HashSet(
30     params: *const Params,
31     vals: *const *const c_void,
32     len: usize,
33 ) {
34     let hs: HashSet<_> = std::collections::HashSet::default();
35     Bench_Rust(hs, params, vals, len);
36 }
37 
38 #[no_mangle]
Bench_Rust_FnvHashSet( params: *const Params, vals: *const *const c_void, len: usize, )39 pub extern "C" fn Bench_Rust_FnvHashSet(
40     params: *const Params,
41     vals: *const *const c_void,
42     len: usize,
43 ) {
44     let hs = FnvHashSet::default();
45     Bench_Rust(hs, params, vals, len);
46 }
47 
48 #[no_mangle]
Bench_Rust_FxHashSet( params: *const Params, vals: *const *const c_void, len: usize, )49 pub extern "C" fn Bench_Rust_FxHashSet(
50     params: *const Params,
51     vals: *const *const c_void,
52     len: usize,
53 ) {
54     let hs = FxHashSet::default();
55     Bench_Rust(hs, params, vals, len);
56 }
57 
58 // Keep this in sync with all the other Bench_*() functions.
Bench_Rust<H: std::hash::BuildHasher>( mut hs: HashSet<*const c_void, H>, params: *const Params, vals: *const *const c_void, len: usize, )59 fn Bench_Rust<H: std::hash::BuildHasher>(
60     mut hs: HashSet<*const c_void, H>,
61     params: *const Params,
62     vals: *const *const c_void,
63     len: usize,
64 ) {
65     let params = unsafe { &*params };
66     let vals = unsafe { slice::from_raw_parts(vals, len) };
67 
68     for j in 0..params.num_inserts {
69         hs.insert(vals[j]);
70     }
71 
72     for _i in 0..params.num_successful_lookups {
73         for j in 0..params.num_inserts {
74             assert!(hs.contains(&vals[j]));
75         }
76     }
77 
78     for _i in 0..params.num_failing_lookups {
79         for j in params.num_inserts..params.num_inserts * 2 {
80             assert!(!hs.contains(&vals[j]));
81         }
82     }
83 
84     for _i in 0..params.num_iterations {
85         let mut n = 0;
86         for _ in hs.iter() {
87             n += 1;
88         }
89         assert!(params.num_inserts == n);
90         assert!(hs.len() == n);
91     }
92 
93     if params.remove_inserts {
94         for j in 0..params.num_inserts {
95             assert!(hs.remove(&vals[j]));
96         }
97         assert!(hs.len() == 0);
98     } else {
99         assert!(hs.len() == params.num_inserts);
100     }
101 }
102