bench_hashmap.rs 9.72 KB
Newer Older
1
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use criterion::{
    black_box,
    criterion_group,
    criterion_main,
    BatchSize,
    Criterion,
};

use ink_core::{
    env,
25
    storage::traits::{
26
27
        KeyPtr,
        SpreadLayout,
28
29
30
31
    },
};
use ink_primitives::Key;

32
33
34
35
36
37
38
39
40
41
42
43
44
45
#[cfg(test)]
macro_rules! gen_tests_for_backend {
    ( $backend:ty ) => {
        criterion_group!(
            populated_cache,
            bench_insert_populated_cache,
            bench_remove_populated_cache,
        );
        criterion_group!(
            empty_cache,
            bench_insert_empty_cache,
            bench_remove_empty_cache,
        );
        criterion_main!(populated_cache, empty_cache,);
46

47
48
49
        /// The number of `ENTIRES` denotes how many test values are put into
        /// the hashmap used in these benchmarks.
        const ENTRIES: i32 = 500;
50

51
52
53
54
55
56
57
        /// Returns some test values for use in benchmarks.
        fn test_values() -> Vec<(i32, i32)> {
            (0..ENTRIES)
                .into_iter()
                .map(|index| (index, index))
                .collect::<Vec<_>>()
        }
58

59
60
61
62
        /// Creates a hashmap from the given slice.
        fn hashmap_from_slice(slice: &[(i32, i32)]) -> $backend {
            slice.iter().copied().collect::<$backend>()
        }
63

64
65
66
67
68
        /// Creates a hashmap from `test_values()`.
        fn setup_hashmap() -> $backend {
            let test_values = test_values();
            hashmap_from_slice(&test_values[..])
        }
69

70
71
72
73
74
        /// Returns always the same `KeyPtr`.
        fn key_ptr() -> KeyPtr {
            let root_key = Key::from([0x42; 32]);
            KeyPtr::from(root_key)
        }
75

76
77
78
79
        /// Creates a hashmap and pushes it to the contract storage.
        fn push_storage_hashmap() {
            let hmap = setup_hashmap();
            SpreadLayout::push_spread(&hmap, &mut key_ptr());
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

        /// Pulls a lazily loading hashmap instance from the contract storage.
        fn pull_storage_hashmap() -> $backend {
            <$backend as SpreadLayout>::pull_spread(&mut key_ptr())
        }

        /// Iteratively checks if an entry is in the hashmap. If not, it
        /// is inserted. In either case it is incremented afterwards.
        fn insert_and_inc(hmap: &mut $backend) {
            for key in 0..ENTRIES * 2 {
                if !black_box(contains_key(hmap, &key)) {
                    black_box(insert(hmap, key, key));
                }
                *black_box(hmap.get_mut(&key)).unwrap() += 1;
            }
        }

        /// Iteratively checks if an entry is in the hashmap. If not, it
        /// is inserted. In either case it is incremented afterwards.
        ///
        /// Uses the Entry API.
        fn insert_and_inc_entry_api(hmap: &mut $backend) {
            for key in 0..ENTRIES * 2 {
                let v = black_box(hmap.entry(key).or_insert(key));
                *v += 1;
            }
        }

        /// Iteratively checks if an entry is in the hashmap. If yes, it
        /// is taken out.
        fn remove(hmap: &mut $backend) {
            for key in 0..ENTRIES * 2 {
                if black_box(contains_key(hmap, &key)) {
                    let _ = black_box(take(hmap, &key));
                }
            }
        }

        /// Iteratively checks if an entry is in the hashmap. If yes, it
        /// is taken out.
        ///
        /// Uses the Entry API.
        fn remove_entry_api(hmap: &mut $backend) {
            for key in 0..ENTRIES * 2 {
                if let Entry::Occupied(o) = black_box(hmap.entry(key)) {
                    o.remove();
                }
            }
        }

        fn bench_insert_populated_cache(c: &mut Criterion) {
            let mut group = c.benchmark_group(
                format!("{} Compare: `insert_and_inc` and `insert_and_inc_entry_api` (populated cache)", stringify!($backend))
            );
            group.bench_function("insert_and_inc", |b| {
                b.iter_batched_ref(
                    || setup_hashmap(),
                    |hmap| insert_and_inc(hmap),
                    BatchSize::SmallInput,
                )
            });
            group.bench_function("insert_and_inc_entry_api", |b| {
                b.iter_batched_ref(
                    || setup_hashmap(),
                    |hmap| insert_and_inc_entry_api(hmap),
                    BatchSize::SmallInput,
                )
            });
            group.finish();
        }

        fn bench_remove_populated_cache(c: &mut Criterion) {
            let _ = env::test::run_test::<env::DefaultEnvTypes, _>(|_| {
                let mut group = c.benchmark_group(
                    format!("{} Compare: `remove` and `remove_entry_api` (populated cache)", stringify!($backend))
                );
                group.bench_function("remove", |b| {
                    b.iter_batched_ref(
                        || setup_hashmap(),
                        |hmap| remove(hmap),
                        BatchSize::SmallInput,
                    )
                });
                group.bench_function("remove_entry_api", |b| {
                    b.iter_batched_ref(
                        || setup_hashmap(),
                        |hmap| remove_entry_api(hmap),
                        BatchSize::SmallInput,
                    )
                });
                group.finish();
                Ok(())
            })
            .unwrap();
        }

        fn bench_insert_empty_cache(c: &mut Criterion) {
            let _ = env::test::run_test::<env::DefaultEnvTypes, _>(|_| {
                let mut group = c.benchmark_group(
                    format!("{} Compare: `insert_and_inc` and `insert_and_inc_entry_api` (empty cache)", stringify!($backend))
                );
                group.bench_function("insert_and_inc", |b| {
                    b.iter_batched_ref(
                        || {
                            push_storage_hashmap();
                            pull_storage_hashmap()
                        },
                        |hmap| insert_and_inc(hmap),
                        BatchSize::SmallInput,
                    )
                });
                group.bench_function("insert_and_inc_entry_api", |b| {
                    b.iter_batched_ref(
                        || {
                            push_storage_hashmap();
                            pull_storage_hashmap()
                        },
                        |hmap| insert_and_inc_entry_api(hmap),
                        BatchSize::SmallInput,
                    )
                });
                group.finish();
                Ok(())
            })
            .unwrap();
        }

        fn bench_remove_empty_cache(c: &mut Criterion) {
            let _ = env::test::run_test::<env::DefaultEnvTypes, _>(|_| {
                let mut group =
                    c.benchmark_group(format!("{} Compare: `remove` and `remove_entry_api` (empty cache)", stringify!($backend)));
                group.bench_function("remove", |b| {
                    b.iter_batched_ref(
                        || {
                            push_storage_hashmap();
                            pull_storage_hashmap()
                        },
                        |hmap| remove(hmap),
                        BatchSize::SmallInput,
                    )
                });
                group.bench_function("remove_entry_api", |b| {
                    b.iter_batched_ref(
                        || {
                            push_storage_hashmap();
                            pull_storage_hashmap()
                        },
                        |hmap| remove_entry_api(hmap),
                        BatchSize::SmallInput,
                    )
                });
                group.finish();
                Ok(())
            })
            .unwrap();
        }
    };
238
239
}

240
241
242
mod lazyhmap_backend {
    use super::*;
    use ink_core::{
243
        env::hash::Blake2x256,
244
        storage::lazy::lazy_hmap::{
245
246
247
248
249
            Entry,
            LazyHashMap,
        },
    };

250
    gen_tests_for_backend!(LazyHashMap<i32, i32, Blake2x256>);
251
252

    pub fn insert(
253
        hmap: &mut LazyHashMap<i32, i32, Blake2x256>,
254
255
256
257
        key: i32,
        value: i32,
    ) -> Option<i32> {
        hmap.put_get(&key, Some(value))
258
259
    }

260
    pub fn take(hmap: &mut LazyHashMap<i32, i32, Blake2x256>, key: &i32) -> Option<i32> {
261
        hmap.put_get(key, None)
262
263
    }

264
    pub fn contains_key(hmap: &LazyHashMap<i32, i32, Blake2x256>, key: &i32) -> bool {
265
        hmap.get(key).is_some()
266
267
    }

268
269
270
    pub fn run() {
        self::main()
    }
271
272
}

273
274
mod hashmap_backend {
    use super::*;
275
    use ink_core::storage::collections::{
276
277
278
        hashmap::Entry,
        HashMap as StorageHashMap,
    };
279

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
    gen_tests_for_backend!(StorageHashMap<i32, i32>);

    pub fn insert(
        hmap: &mut StorageHashMap<i32, i32>,
        key: i32,
        value: i32,
    ) -> Option<i32> {
        hmap.insert(key, value)
    }

    pub fn take(hmap: &mut StorageHashMap<i32, i32>, key: &i32) -> Option<i32> {
        hmap.take(key)
    }

    pub fn contains_key(hmap: &StorageHashMap<i32, i32>, key: &i32) -> bool {
        hmap.contains_key(key)
    }

    pub fn run() {
        self::main()
    }
301
302
}

303
304
305
fn main() {
    hashmap_backend::run();
    lazyhmap_backend::run();
306
}