From ef20cd82b43fba181800fbcd31c4a980aedee613 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 10 Oct 2023 09:37:39 -0500 Subject: [PATCH] bench: Check self-eq performance --- Cargo.toml | 4 ++ benches/eq.rs | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 benches/eq.rs diff --git a/Cargo.toml b/Cargo.toml index c4c8f4d..afe50d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,5 +29,9 @@ harness = false name = "access" harness = false +[[bench]] +name = "eq" +harness = false + [profile.release] debug = 1 diff --git a/benches/eq.rs b/benches/eq.rs new file mode 100644 index 0000000..1442c79 --- /dev/null +++ b/benches/eq.rs @@ -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", len), &len, |b, _| { + let uut = Box::::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", len), &len, |b, _| { + let uut = std::sync::Arc::::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);