forked from filecoin-project/ref-fvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhamt_benchmark.rs
More file actions
129 lines (112 loc) · 3.58 KB
/
Copy pathhamt_benchmark.rs
File metadata and controls
129 lines (112 loc) · 3.58 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2021-2023 Protocol Labs
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use std::hint::black_box;
use criterion::{Criterion, criterion_group, criterion_main};
use fvm_ipld_blockstore::{Blockstore, MemoryBlockstore};
use fvm_ipld_encoding::tuple::*;
use fvm_ipld_hamt::Hamt;
const BIT_WIDTH: u32 = 5;
const ITEM_COUNT: u8 = 40;
// Struct to simulate a reasonable amount of data per value into the amt
#[derive(Clone, Serialize_tuple, Deserialize_tuple, PartialEq)]
struct BenchData {
v1: Vec<u8>,
v2: Vec<u8>,
v3: Vec<u8>,
v: u64,
a: [u8; 32],
a2: [u8; 32],
}
impl BenchData {
fn new(val: u8) -> Self {
Self {
v1: vec![val; 8],
v2: vec![val; 20],
v3: vec![val; 10],
v: 8,
a: [val; 32],
a2: [val; 32],
}
}
}
fn insert(c: &mut Criterion) {
c.bench_function("HAMT bulk insert (no flush)", |b| {
b.iter(|| {
let db = MemoryBlockstore::default();
let mut a = Hamt::<_, _>::new_with_bit_width(&db, BIT_WIDTH);
for i in 0..black_box(ITEM_COUNT) {
a.set(black_box(vec![i; 20].into()), black_box(BenchData::new(i)))
.unwrap();
}
})
});
}
fn insert_load_flush(c: &mut Criterion) {
c.bench_function("HAMT bulk insert with flushing and loading", |b| {
b.iter(|| {
let db = MemoryBlockstore::default();
let mut empt = Hamt::<_, ()>::new_with_bit_width(&db, BIT_WIDTH);
let mut cid = empt.flush().unwrap();
for i in 0..black_box(ITEM_COUNT) {
let mut a = Hamt::<_, _>::load_with_bit_width(&cid, &db, BIT_WIDTH).unwrap();
a.set(black_box(vec![i; 20].into()), black_box(BenchData::new(i)))
.unwrap();
cid = a.flush().unwrap();
}
})
});
}
fn delete(c: &mut Criterion) {
let db = MemoryBlockstore::default();
let mut a = setup_hamt(&db);
let cid = a.flush().unwrap();
c.bench_function("HAMT deleting all nodes", |b| {
b.iter(|| {
let mut a = Hamt::<_, BenchData>::load_with_bit_width(&cid, &db, BIT_WIDTH).unwrap();
for i in 0..black_box(ITEM_COUNT) {
a.delete(black_box([i; 20].as_ref())).unwrap();
}
})
});
}
fn for_each(c: &mut Criterion) {
let db = MemoryBlockstore::default();
let mut a = setup_hamt(&db);
let cid = a.flush().unwrap();
c.bench_function("HAMT for_each function", |b| {
b.iter(|| {
let a = Hamt::<_, _>::load_with_bit_width(&cid, &db, BIT_WIDTH).unwrap();
black_box(a).for_each(|_k, _v: &BenchData| Ok(())).unwrap();
})
});
}
fn for_each_cacheless(c: &mut Criterion) {
let db = MemoryBlockstore::default();
let mut a = setup_hamt(&db);
let cid = a.flush().unwrap();
c.bench_function("HAMT for_each_cacheless function", |b| {
b.iter(|| {
let a = Hamt::<_, _>::load_with_bit_width(&cid, &db, BIT_WIDTH).unwrap();
black_box(a)
.for_each_cacheless(|_k, _v: &BenchData| Ok(()))
.unwrap();
})
});
}
fn setup_hamt<BS: Blockstore>(db: &BS) -> Hamt<&BS, BenchData> {
let mut a = Hamt::<_, _>::new_with_bit_width(db, BIT_WIDTH);
for i in 0..ITEM_COUNT {
a.set(vec![i; 20].into(), BenchData::new(i)).unwrap();
}
a
}
criterion_group!(
benches,
insert,
insert_load_flush,
delete,
for_each,
for_each_cacheless
);
criterion_main!(benches);