Skip to content

Commit cdbf554

Browse files
committed
Add small bench and add parity copyright header to storage
1 parent 7330c93 commit cdbf554

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

misc/multiaddr/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ bincode = "1"
2525
quickcheck = "0.9.0"
2626
rand = "0.7.2"
2727
serde_json = "1.0"
28+
criterion = "0.3"
29+
30+
[[bench]]
31+
name = "clone"
32+
harness = false

misc/multiaddr/benches/clone.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2019 Parity Technologies (UK) Ltd.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the "Software"),
5+
// to deal in the Software without restriction, including without limitation
6+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7+
// and/or sell copies of the Software, and to permit persons to whom the
8+
// Software is furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
// DEALINGS IN THE SOFTWARE.
20+
//! This benchmark tests the speed of cloning of the largest possible inlined
21+
//! multiaddr vs the smalles possible heap allocated multiaddr.
22+
//!
23+
//! Note that the main point of the storage optimization is not to speed up clone
24+
//! but to avoid allocating on the heap at all, but still you see a nice benefit
25+
//! in the speed of cloning.
26+
use criterion::{Bencher, Criterion, criterion_main, criterion_group, black_box};
27+
use parity_multiaddr::Multiaddr;
28+
29+
fn do_clone(multiaddr: &Multiaddr) -> usize {
30+
let mut res = 0usize;
31+
for _ in 0..10 {
32+
res += multiaddr.clone().as_ref().len()
33+
}
34+
res
35+
}
36+
37+
fn clone(bench: &mut Bencher, addr: &Multiaddr) {
38+
bench.iter(|| do_clone(black_box(addr)))
39+
}
40+
41+
fn criterion_benchmarks(bench: &mut Criterion) {
42+
let inlined: Multiaddr = "/dns4/01234567890123456789123/tcp/80/ws".parse().unwrap();
43+
let heap: Multiaddr = "/dns4/0123456789012345678901234/tcp/80/ws".parse().unwrap();
44+
assert_eq!(inlined.as_ref().len(), 30);
45+
assert_eq!(heap.as_ref().len(), 32);
46+
47+
bench.bench_function("clone 10 max inlined", |b| clone(b, &inlined));
48+
bench.bench_function("clone 10 min heap", |b| clone(b, &heap));
49+
}
50+
51+
criterion_group!(benches, criterion_benchmarks);
52+
criterion_main!(benches);

misc/multiaddr/src/storage.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the "Software"),
5+
// to deal in the Software without restriction, including without limitation
6+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7+
// and/or sell copies of the Software, and to permit persons to whom the
8+
// Software is furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
// DEALINGS IN THE SOFTWARE.
120
use std::sync::Arc;
221

322
/// MAX_INLINE is the maximum size of a multiaddr that can be stored inline.
@@ -41,9 +60,23 @@ impl Storage {
4160

4261
#[cfg(test)]
4362
mod tests {
63+
use crate::Multiaddr;
4464
use super::{Storage, MAX_INLINE};
4565
use quickcheck::quickcheck;
4666

67+
#[test]
68+
fn multihash_size() {
69+
fn assert_size(ma: &str, n: usize, inline: bool) {
70+
let ma: Multiaddr = ma.parse().unwrap();
71+
assert_eq!(ma.as_ref().len(), n);
72+
assert_eq!(n <= MAX_INLINE, inline);
73+
}
74+
assert_size("/ip4/127.0.0.1", 5, true);
75+
assert_size("/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000", 20, true);
76+
assert_size("/dns4/0123456789012345678901234/tcp/8000", 30, true);
77+
assert_size("/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", 59, false);
78+
}
79+
4780
#[test]
4881
fn struct_size() {
4982
// this should be true for both 32 and 64 bit archs

0 commit comments

Comments
 (0)