bench_lazy.rs 2.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
//
// 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,
    BenchmarkId,
    Criterion,
};
use ink_core::{
    env,
24
    storage::{
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
        lazy::Lazy,
        traits::{
            KeyPtr,
            SpreadLayout,
        },
    },
};
use ink_primitives::Key;

criterion_group!(populated_cache, bench_set_populated_cache);
criterion_group!(empty_cache, bench_set_empty_cache,);
criterion_main!(populated_cache, empty_cache);

mod populated_cache {
    use super::*;
    use core::ops::DerefMut;

    pub fn set() {
        let mut lazy = <Lazy<i32>>::new(1);
        let lazy_mut = black_box(&mut lazy);
        black_box(Lazy::set(lazy_mut, 17));
    }

    pub fn deref_mut() {
        let mut lazy = <Lazy<i32>>::new(1);
        let i32_mut = black_box(lazy.deref_mut());
        black_box(*i32_mut = 17);
    }
}

fn bench_set_populated_cache(c: &mut Criterion) {
    let mut group = c.benchmark_group("Compare: `set` and `deref_mut` (populated cache)");
    group.bench_function(BenchmarkId::new("set", 0), |b| {
        b.iter(|| populated_cache::set())
    });
    group.bench_function(BenchmarkId::new("deref_mut", 0), |b| {
        b.iter(|| populated_cache::deref_mut())
    });
    group.finish();
}

/// Pushes a value to contract storage and creates a `Lazy` pointing to it.
fn push_storage_lazy(value: i32) -> Lazy<i32> {
    let root_key = Key::from([0x00; 32]);
    SpreadLayout::push_spread(&Lazy::new(value), &mut KeyPtr::from(root_key));
    <Lazy<i32>>::lazy(root_key)
}

mod empty_cache {
    use super::*;

    pub fn set() {
        let mut lazy = push_storage_lazy(1);
        black_box(Lazy::set(&mut lazy, 13));
    }

    pub fn deref_mut() {
        let mut lazy = push_storage_lazy(1);
        black_box(*lazy = 13);
    }
}

fn bench_set_empty_cache(c: &mut Criterion) {
    let _ = env::test::run_test::<env::DefaultEnvTypes, _>(|_| {
        let mut group = c.benchmark_group("Compare: `set` and `deref_mut` (empty cache)");
        group.bench_function(BenchmarkId::new("set", 0), |b| {
            b.iter(|| empty_cache::set())
        });
        group.bench_function(BenchmarkId::new("deref_mut", 0), |b| {
            b.iter(|| empty_cache::deref_mut())
        });
        group.finish();
        Ok(())
    })
    .unwrap();
}