summaryrefslogtreecommitdiffstats
path: root/xpcom/rust/gtest/bench-collections/bench.rs
blob: 1aba69f01f833c8ff33cfde410ee51027b4ad278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#![allow(non_snake_case)]

extern crate fnv;
extern crate fxhash;

use fnv::FnvHashSet;
use fxhash::FxHashSet;
use std::collections::HashSet;
use std::os::raw::{c_char, c_void};
use std::slice;

/// Keep this in sync with Params in Bench.cpp.
#[derive(Debug)]
#[repr(C)]
pub struct Params {
    config_name: *const c_char,
    num_inserts: usize,
    num_successful_lookups: usize,
    num_failing_lookups: usize,
    num_iterations: usize,
    remove_inserts: bool,
}

#[no_mangle]
pub extern "C" fn Bench_Rust_HashSet(
    params: *const Params,
    vals: *const *const c_void,
    len: usize,
) {
    let hs: HashSet<_> = std::collections::HashSet::default();
    Bench_Rust(hs, params, vals, len);
}

#[no_mangle]
pub extern "C" fn Bench_Rust_FnvHashSet(
    params: *const Params,
    vals: *const *const c_void,
    len: usize,
) {
    let hs = FnvHashSet::default();
    Bench_Rust(hs, params, vals, len);
}

#[no_mangle]
pub extern "C" fn Bench_Rust_FxHashSet(
    params: *const Params,
    vals: *const *const c_void,
    len: usize,
) {
    let hs = FxHashSet::default();
    Bench_Rust(hs, params, vals, len);
}

// Keep this in sync with all the other Bench_*() functions.
fn Bench_Rust<H: std::hash::BuildHasher>(
    mut hs: HashSet<*const c_void, H>,
    params: *const Params,
    vals: *const *const c_void,
    len: usize,
) {
    let params = unsafe { &*params };
    let vals = unsafe { slice::from_raw_parts(vals, len) };

    for j in 0..params.num_inserts {
        hs.insert(vals[j]);
    }

    for _i in 0..params.num_successful_lookups {
        for j in 0..params.num_inserts {
            assert!(hs.contains(&vals[j]));
        }
    }

    for _i in 0..params.num_failing_lookups {
        for j in params.num_inserts..params.num_inserts * 2 {
            assert!(!hs.contains(&vals[j]));
        }
    }

    for _i in 0..params.num_iterations {
        let mut n = 0;
        for _ in hs.iter() {
            n += 1;
        }
        assert!(params.num_inserts == n);
        assert!(hs.len() == n);
    }

    if params.remove_inserts {
        for j in 0..params.num_inserts {
            assert!(hs.remove(&vals[j]));
        }
        assert!(hs.is_empty());
    } else {
        assert!(hs.len() == params.num_inserts);
    }
}