bench_vec.rs 6.43 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
        collections::Vec as StorageVec,
        traits::{
            KeyPtr,
            SpreadLayout,
        },
    },
};
use ink_primitives::Key;

criterion_group!(
    populated_cache,
    bench_clear_populated_cache,
    bench_put_populated_cache,
);
criterion_group!(empty_cache, bench_clear_empty_cache, bench_put_empty_cache,);
criterion_main!(populated_cache, empty_cache,);

/// Returns some test values for use in benchmarks.
#[rustfmt::skip]
fn test_values() -> &'static [u8] {
    &[
        b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9',
        b'A', b'B', b'C', b'D', b'E', b'F'
    ]
}

/// Creates a storage vector from the given slice.
fn storage_vec_from_slice(slice: &[u8]) -> StorageVec<u8> {
    slice.iter().copied().collect::<StorageVec<u8>>()
}

/// Creates a storage vector and pushes it to the contract storage.
fn push_storage_vec() {
    let vec = storage_vec_from_slice(test_values());
    let root_key = Key::from([0x00; 32]);
    SpreadLayout::push_spread(&vec, &mut KeyPtr::from(root_key));
}

/// Pulls a lazily loading storage vector instance from the contract storage.
fn pull_storage_vec() -> StorageVec<u8> {
    let root_key = Key::from([0x00; 32]);
    <StorageVec<u8> as SpreadLayout>::pull_spread(&mut KeyPtr::from(root_key))
}

mod populated_cache {
    use super::*;

    pub fn clear(test_values: &[u8]) {
        let mut vec = storage_vec_from_slice(&test_values);
        black_box(vec.clear());
    }

    pub fn pop_all(test_values: &[u8]) {
        let mut vec = storage_vec_from_slice(&test_values);
        while let Some(ignored) = black_box(vec.pop()) {
            black_box(ignored);
        }
    }

    pub fn set(test_values: &[u8]) {
        let mut vec = storage_vec_from_slice(&test_values);
        for (index, _value) in test_values.iter().enumerate() {
87
            let _ = black_box(vec.set(index as u32, b'X'));
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
        }
    }

    pub fn get_mut(test_values: &[u8]) {
        let mut vec = storage_vec_from_slice(&test_values);
        for (index, _value) in test_values.iter().enumerate() {
            *black_box(vec.get_mut(index as u32).unwrap()) = b'X';
        }
    }
}

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

fn bench_clear_populated_cache(c: &mut Criterion) {
    let mut group = c.benchmark_group("Compare: `clear` and `pop_all` (populated cache)");
    let test_values = [b'A', b'B', b'C', b'D', b'E', b'F'];
    group.bench_with_input("clear", &test_values, |b, i| {
        b.iter(|| populated_cache::clear(i))
    });
    group.bench_with_input("pop_all", &test_values, |b, i| {
        b.iter(|| populated_cache::pop_all(i))
    });
    group.finish();
}

mod empty_cache {
    use super::*;

    /// In this case we lazily load the vec from storage using `pull_spread`.
    /// This will just load lazily and won't pull anything from the storage.
    pub fn clear() {
        push_storage_vec();
        let mut vec = pull_storage_vec();
        black_box(vec.clear());
    }

    /// In this case we lazily load the vec from storage using `pull_spread`.
    /// This will just load lazily and won't pull anything from the storage.
    /// `pop` will then result in loading from storage.
    pub fn pop_all() {
        push_storage_vec();
        let mut vec = pull_storage_vec();
        while let Some(ignored) = black_box(vec.pop()) {
            black_box(ignored);
        }
    }

    /// In this case we lazily load the vec from storage using `pull_spread`.
    /// This will just load lazily and won't pull anything from the storage.
    /// The `deref` will then load from storage.
    pub fn get_mut() {
        push_storage_vec();
        let mut vec = pull_storage_vec();
        for index in 0..vec.len() {
            *black_box(vec.get_mut(index).unwrap()) = b'X';
        }
    }

    /// In this case we lazily load the vec from storage using `pull_spread`.
    /// This will just load lazily and won't pull anything from the storage.
    /// The `vec.set()` will not load anything from storage.
    pub fn set() {
        push_storage_vec();
        let mut vec = pull_storage_vec();
        for index in 0..vec.len() {
162
            let _ = black_box(vec.set(index, b'X'));
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
        }
    }
}

/// In this case we lazily instantiate a `StorageVec` by first `create_and_store`-ing
/// into the contract storage. We then load the vec from storage lazily in each
/// benchmark iteration.
fn bench_clear_empty_cache(c: &mut Criterion) {
    let _ = env::test::run_test::<env::DefaultEnvTypes, _>(|_| {
        let mut group = c.benchmark_group("Compare: `clear` and `pop_all` (empty cache)");
        group.bench_function("clear", |b| b.iter(|| empty_cache::clear()));
        group.bench_function("pop_all", |b| b.iter(|| empty_cache::pop_all()));
        group.finish();
        Ok(())
    })
    .unwrap();
}

/// In this case we lazily instantiate a `StorageVec` by first `create_and_store`-ing
/// into the contract storage. We then load the vec from storage lazily in each
/// benchmark iteration.
fn bench_put_empty_cache(c: &mut Criterion) {
    let _ = env::test::run_test::<env::DefaultEnvTypes, _>(|_| {
        let mut group = c.benchmark_group("Compare: `set` and `get_mut` (empty cache)");
        group.bench_function("set", |b| b.iter(|| empty_cache::set()));
        group.bench_function("get_mut", |b| b.iter(|| empty_cache::get_mut()));
        group.finish();
        Ok(())
    })
    .unwrap();
}