summaryrefslogtreecommitdiff
path: root/crates/hashx/bench/benches/hashx_cachegrind.rs
blob: b352c4af2d81bf4acde3e2d3c35f283a9cdee44e (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
102
103
104
105
106
107
108
109
110
111
112
//! This is a low-level cachegrind microbenchmark for the C and Rust
//! implementations of HashX, using the Iai framework.
//!
//! Requires valgrind to be installed.
//! Requires the HashX compiler is supported. (aarch64, x86_64)
//!
//! This only includes a small subset of the tests available in hashx_bench,
//! and it only runs a small number of iterations. The focus here is on using
//! cachegrind to measure low-level cache miss behavior and instruction counts.
//! Use hashx_bench to measure real-world performance using wallclock time.

use iai::black_box;

/// Bind Rust `HashBuilder` whose `RuntimeOption` is `$runtime_option` to `$builder`
//
// This and mk_c_equix are macros rather than a function because it avoids us
// having to import RuntimeOption::*, etc. or clutter the calls with a local alias.
macro_rules! mk_rust { { $builder:ident = $runtime_option:ident } => {
    let mut $builder = hashx::HashXBuilder::new();
    $builder.runtime(hashx::RuntimeOption::$runtime_option);
} }

/// Bind a C `HashX` whose `HashXType` is `$hashx_type` to `$ctx`
macro_rules! mk_c_equix { { $ctx:ident = $hashx_type:ident } => {
    let mut $ctx = tor_c_equix::HashX::new(tor_c_equix::HashXType::$hashx_type);
} }

/// Evaluate `$eval` binding `$loopvar` to `0..$max`
///
/// Applies `black_box` to inputs and outputs.
///
/// If `$loopvar_map` is supplied, it is a function applied to $loopvar
/// to convert from the integer loop variable, to whatever more useful type is needed.
//
// We expect $loopvar_map:ident even though a closure would be suitable, mostly because
// we expect the actual benchmark cases to want to use a short alias like `u32be`
macro_rules! bench_loop { {
    $loopvar:ident, $max:expr $(, $loopvar_map:ident )? => $eval:expr
} => {
    for $loopvar in 0..$max {
        $(
            let $loopvar = $loopvar_map($loopvar);
        )?
        let $loopvar = black_box($loopvar);
        let _ = black_box($eval);
    }
} }

/// Convenience alias to reduce clutter in actual benchmarks
const C_HASHX_OK: tor_c_equix::ffi::hashx_result = tor_c_equix::HashXResult::HASHX_OK;

/// Helper, alias for `u32::to_be_bytes`
//
// Unfortunately, we can't just `use u32::to_be_bytes`.
fn u32be(s: u32) -> [u8; 4] {
    s.to_be_bytes()
}

fn generate_interp_1000x() {
    mk_rust!(builder = InterpretOnly);
    bench_loop! { s, 1000_u32, u32be => builder.build(&s) }
}

fn generate_interp_1000x_c() {
    mk_c_equix!(ctx = HASHX_TYPE_INTERPRETED);
    bench_loop! { s, 1000_u32, u32be => ctx.make(&s) }
}

fn generate_compiled_1000x() {
    mk_rust!(builder = CompileOnly);
    bench_loop! { s, 1000_u32, u32be => builder.build(&s) }
}

fn generate_compiled_1000x_c() {
    mk_c_equix!(ctx = HASHX_TYPE_COMPILED);
    bench_loop! { s, 1000_u32, u32be => ctx.make(&s) }
}

fn interp_u64_hash_1000x() {
    mk_rust!(builder = InterpretOnly);
    let hashx = builder.build(b"abc").unwrap();
    bench_loop! { i, 1000_u64 => hashx.hash_to_u64(i) }
}

fn interp_8b_hash_1000x_c() {
    mk_c_equix!(ctx = HASHX_TYPE_INTERPRETED);
    assert_eq!(ctx.make(b"abc"), C_HASHX_OK);
    bench_loop! { i, 1000_u64 => ctx.exec(i) }
}

fn compiled_u64_hash_100000x() {
    mk_rust!(builder = CompileOnly);
    let hashx = builder.build(b"abc").unwrap();
    bench_loop! { i, 100000_u64 => hashx.hash_to_u64(i) }
}

fn compiled_8b_hash_100000x_c() {
    mk_c_equix!(ctx = HASHX_TYPE_COMPILED);
    assert_eq!(ctx.make(b"abc"), C_HASHX_OK);
    bench_loop! { i, 100000_u64 => ctx.exec(i) }
}

iai::main!(
    generate_interp_1000x,
    generate_interp_1000x_c,
    generate_compiled_1000x,
    generate_compiled_1000x_c,
    interp_u64_hash_1000x,
    interp_8b_hash_1000x_c,
    compiled_u64_hash_100000x,
    compiled_8b_hash_100000x_c,
);