Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bench: Check self-eq performance #29

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ harness = false
name = "access"
harness = false

[[bench]]
name = "eq"
harness = false

[profile.release]
debug = 1
168 changes: 168 additions & 0 deletions benches/eq.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#![allow(
clippy::clone_on_copy,
clippy::useless_conversion,
clippy::clone_double_ref
)]

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};

mod fixture;

type StringCow<'s> = std::borrow::Cow<'s, str>;

fn bench_access(c: &mut Criterion) {
let mut group = c.benchmark_group("access");
for fixture in fixture::SAMPLES {
let len = fixture.len();
group.throughput(Throughput::Bytes(len as u64));
group.bench_with_input(BenchmarkId::new("String", len), &len, |b, _| {
let uut = String::from(*fixture);
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
});
group.bench_with_input(BenchmarkId::new("Box<str>", len), &len, |b, _| {
let uut = Box::<str>::from(*fixture);
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
});
group.bench_with_input(BenchmarkId::new("Arc<str>", len), &len, |b, _| {
let uut = std::sync::Arc::<str>::from(*fixture);
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
});
group.bench_with_input(BenchmarkId::new("StringCow::Owned", len), &len, |b, _| {
let uut = StringCow::Owned(String::from(*fixture));
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
});

group.bench_with_input(BenchmarkId::new("ArcStr::from", len), &len, |b, _| {
let uut = arcstr::ArcStr::from(*fixture);
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
});
group.bench_with_input(BenchmarkId::new("CompactString::new", len), &len, |b, _| {
let uut = compact_str::CompactString::new(fixture);
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
});
group.bench_with_input(BenchmarkId::new("EcoString::from", len), &len, |b, _| {
let uut = ecow::EcoString::from(*fixture);
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
});
group.bench_with_input(
BenchmarkId::new("flexstr::SharedStr::from_ref", len),
&len,
|b, _| {
let uut = flexstr::SharedStr::from_ref(*fixture);
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
},
);
group.bench_with_input(BenchmarkId::new("HipStr::from", len), &len, |b, _| {
let uut = hipstr::HipStr::from(*fixture);
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
});
group.bench_with_input(BenchmarkId::new("ImString::from", len), &len, |b, _| {
let uut = imstr::ImString::from(*fixture);
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
});
group.bench_with_input(BenchmarkId::new("KString::from_ref", len), &len, |b, _| {
let uut = kstring::KString::from_ref(*fixture);
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
});
group.bench_with_input(
BenchmarkId::new("KString::from_string", len),
&len,
|b, _| {
let uut = kstring::KString::from_string(String::from(*fixture));
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
},
);
group.bench_with_input(
BenchmarkId::new("smartstring::String::new", len),
&len,
|b, _| {
let uut = smartstring::alias::String::from(*fixture);
let uut = criterion::black_box(uut);
let copy = uut.clone();
let copy = criterion::black_box(copy);
b.iter(|| uut == copy)
},
);
}
group.finish();
}

fn bench_access_static(c: &mut Criterion) {
let mut group = c.benchmark_group("access_static");
for fixture in fixture::SAMPLES {
let len = fixture.len();
group.throughput(Throughput::Bytes(len as u64));
group.bench_with_input(BenchmarkId::new("&'static str", len), &len, |b, _| {
let uut = *fixture;
let uut = criterion::black_box(uut);
b.iter(|| uut.is_empty())
});
group.bench_with_input(
BenchmarkId::new("StringCow::Borrowed", len),
&len,
|b, _| {
let uut = StringCow::Borrowed(*fixture);
let uut = criterion::black_box(uut);
b.iter(|| uut.is_empty())
},
);
group.bench_with_input(
BenchmarkId::new("flexstr::SharedStr::from_static", len),
&len,
|b, _| {
let uut = flexstr::SharedStr::from_static(*fixture);
let uut = criterion::black_box(uut);
b.iter(|| uut.is_empty())
},
);
group.bench_with_input(
BenchmarkId::new("KString::from_static", len),
&len,
|b, _| {
let uut = kstring::KString::from_static(*fixture);
let uut = criterion::black_box(uut);
b.iter(|| uut.is_empty())
},
);
}
group.finish();
}

criterion_group!(benches, bench_access, bench_access_static);
criterion_main!(benches);
Loading