-
-
Save vmx/0dc0d981b522e2834084559faa0dcb56 to your computer and use it in GitHub Desktop.
Vec<u8>/Bytes/custom storage cloning benchmark. Download ZIP and run `cargo bench`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "bench" | |
version = "0.1.0" | |
edition = "2018" | |
[dev-dependencies] | |
bytes = "0.5" | |
criterion = "0.3" | |
[[bench]] | |
name = "clone" | |
harness = false | |
path = "clone.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[path = "storage.rs"] | |
mod storage; | |
use bytes::{Bytes, BytesMut}; | |
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; | |
use storage::Storage; | |
criterion_group!(benches, clone); | |
criterion_main!(benches); | |
fn clone(c: &mut Criterion) { | |
let mut group = c.benchmark_group("10 clones of Vec<u8>"); | |
for v in &[vec(10), vec(100)] { | |
group.bench_with_input(BenchmarkId::from_parameter(v.len()), v, |b, v| { | |
b.iter(|| { | |
for _ in 0 .. 10 { | |
let _ = v.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
let mut group = c.benchmark_group("10 clones of Bytes"); | |
for v in &[bytes(10), bytes(100)] { | |
group.bench_with_input(BenchmarkId::from_parameter(v.len()), v, |b, v| { | |
b.iter(|| { | |
for _ in 0 .. 10 { | |
let _ = v.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
let mut group = c.benchmark_group("10 clones of custom storage"); | |
for v in &[storage(10), storage(100)] { | |
group.bench_with_input(BenchmarkId::from_parameter(v.bytes().len()), v, |b, v| { | |
b.iter(|| { | |
for _ in 0 .. 10 { | |
let _ = v.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
let mut group = c.benchmark_group("64 bytes long Vec<u8> cloning"); | |
for v in &[10, 100] { | |
group.bench_with_input(BenchmarkId::from_parameter(v), v, |b, &v| { | |
let data = vec(64); | |
b.iter(|| { | |
for _ in 0 .. v { | |
let _ = data.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
let mut group = c.benchmark_group("64 bytes long Bytes cloning"); | |
for v in &[10, 100] { | |
group.bench_with_input(BenchmarkId::from_parameter(v), v, |b, &v| { | |
let data = bytes(64); | |
b.iter(|| { | |
for _ in 0 .. v { | |
let _ = data.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
let mut group = c.benchmark_group("64 bytes long custom storage cloning"); | |
for v in &[10, 100] { | |
group.bench_with_input(BenchmarkId::from_parameter(v), v, |b, &v| { | |
let data = storage(36); | |
b.iter(|| { | |
for _ in 0 .. v { | |
let _ = data.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
let mut group = c.benchmark_group("36 bytes long Vec<u8> cloning"); | |
for v in &[10, 100] { | |
group.bench_with_input(BenchmarkId::from_parameter(v), v, |b, &v| { | |
let data = vec(36); | |
b.iter(|| { | |
for _ in 0 .. v { | |
let _ = data.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
let mut group = c.benchmark_group("36 bytes long Bytes cloning"); | |
for v in &[10, 100] { | |
group.bench_with_input(BenchmarkId::from_parameter(v), v, |b, &v| { | |
let data = bytes(36); | |
b.iter(|| { | |
for _ in 0 .. v { | |
let _ = data.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
let mut group = c.benchmark_group("36 bytes long custom storage cloning"); | |
for v in &[10, 100] { | |
group.bench_with_input(BenchmarkId::from_parameter(v), v, |b, &v| { | |
let data = storage(36); | |
b.iter(|| { | |
for _ in 0 .. v { | |
let _ = data.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
} | |
fn vec(n: usize) -> Vec<u8> { | |
let mut data = Vec::with_capacity(n); | |
data.resize(n, 0); | |
data | |
} | |
fn bytes(n: usize) -> Bytes { | |
let mut data = BytesMut::with_capacity(n); | |
data.resize(n, 0); | |
data.freeze() | |
} | |
fn storage(n: usize) -> Storage { | |
let mut data: Vec<u8> = Vec::with_capacity(n); | |
data.resize(n, 0); | |
Storage::from_slice(&data) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::Arc; | |
const MAX_INLINE: usize = 38; | |
#[derive(Clone)] | |
pub enum Storage { | |
/// hash is stored inline if it is smaller than MAX_INLINE | |
Inline(u8, [u8; MAX_INLINE]), | |
/// hash is stored on the heap. this must be only used if the hash is actually larger than | |
/// MAX_INLINE bytes to ensure an unique representation. | |
Heap(Arc<[u8]>), | |
} | |
impl Storage { | |
/// The raw bytes. | |
pub fn bytes(&self) -> &[u8] { | |
match self { | |
Storage::Inline(len, bytes) => &bytes[..(*len as usize)], | |
Storage::Heap(data) => &data, | |
} | |
} | |
/// creates storage from a vec. For a size up to MAX_INLINE, this will not allocate. | |
pub fn from_slice(slice: &[u8]) -> Self { | |
let len = slice.len(); | |
if len <= MAX_INLINE { | |
let mut data: [u8; MAX_INLINE] = [0; MAX_INLINE]; | |
data[..len].copy_from_slice(slice); | |
Storage::Inline(len as u8, data) | |
} else { | |
Storage::Heap(slice.into()) | |
} | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::{Storage, MAX_INLINE}; | |
#[test] | |
fn struct_size() { | |
// this should be true for both 32 and 64 bit archs | |
assert_eq!(std::mem::size_of::<Storage>(), 40); | |
} | |
#[test] | |
fn roundtrip() { | |
// check that .bytes() returns whatever the storage was created with | |
for i in 0..((MAX_INLINE + 10) as u8) { | |
let data = (0..i).collect::<Vec<u8>>(); | |
let storage = Storage::from_slice(&data); | |
assert_eq!(data, storage.bytes()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results from my machine: