|
| 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 100 max inlined", |b| clone(b, &inlined)); |
| 48 | + bench.bench_function("clone 100 min heap", |b| clone(b, &heap)); |
| 49 | +} |
| 50 | + |
| 51 | +criterion_group!(benches, criterion_benchmarks); |
| 52 | +criterion_main!(benches); |
0 commit comments