Skip to content

Commit

Permalink
Fix benchmark compilation
Browse files Browse the repository at this point in the history
Signed-off-by: Tin Švagelj <[email protected]>
  • Loading branch information
Caellian committed Nov 21, 2024
1 parent dfa3285 commit 09f5ea4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
20 changes: 10 additions & 10 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ criterion_main!(bench_get_or_intern, bench_resolve, bench_get, bench_iter);

fn bench_get_or_intern_static(c: &mut Criterion) {
let mut g = c.benchmark_group("get_or_intern_static");
fn bench_for_backend<BB: BackendBenchmark>(g: &mut BenchmarkGroup<WallTime>) {
fn bench_for_backend<'i, BB: BackendBenchmark<'i>>(g: &mut BenchmarkGroup<WallTime>) {
#[rustfmt::skip]
let static_strings = &[
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
Expand Down Expand Up @@ -87,7 +87,7 @@ fn bench_get_or_intern_static(c: &mut Criterion) {
fn bench_get_or_intern_fill_with_capacity(c: &mut Criterion) {
let mut g = c.benchmark_group("get_or_intern/fill-empty/with_capacity");
g.throughput(Throughput::Elements(BENCH_LEN_STRINGS as u64));
fn bench_for_backend<BB: BackendBenchmark>(g: &mut BenchmarkGroup<WallTime>) {
fn bench_for_backend<'i, BB: BackendBenchmark<'i>>(g: &mut BenchmarkGroup<WallTime>) {
g.bench_with_input(
BB::NAME,
&(BENCH_LEN_STRINGS, BENCH_STRING_LEN),
Expand All @@ -113,7 +113,7 @@ fn bench_get_or_intern_fill_with_capacity(c: &mut Criterion) {
fn bench_get_or_intern_fill(c: &mut Criterion) {
let mut g = c.benchmark_group("get_or_intern/fill-empty/new");
g.throughput(Throughput::Elements(BENCH_LEN_STRINGS as u64));
fn bench_for_backend<BB: BackendBenchmark>(g: &mut BenchmarkGroup<WallTime>) {
fn bench_for_backend<'i, BB: BackendBenchmark<'i>>(g: &mut BenchmarkGroup<WallTime>) {
g.bench_with_input(
BB::NAME,
&(BENCH_LEN_STRINGS, BENCH_STRING_LEN),
Expand All @@ -139,7 +139,7 @@ fn bench_get_or_intern_fill(c: &mut Criterion) {
fn bench_get_or_intern_already_filled(c: &mut Criterion) {
let mut g = c.benchmark_group("get_or_intern/already-filled");
g.throughput(Throughput::Elements(BENCH_LEN_STRINGS as u64));
fn bench_for_backend<BB: BackendBenchmark>(g: &mut BenchmarkGroup<WallTime>) {
fn bench_for_backend<'i, BB: BackendBenchmark<'i>>(g: &mut BenchmarkGroup<WallTime>) {
g.bench_with_input(
BB::NAME,
&(BENCH_LEN_STRINGS, BENCH_STRING_LEN),
Expand All @@ -165,7 +165,7 @@ fn bench_get_or_intern_already_filled(c: &mut Criterion) {
fn bench_resolve_already_filled(c: &mut Criterion) {
let mut g = c.benchmark_group("resolve/already-filled");
g.throughput(Throughput::Elements(BENCH_LEN_STRINGS as u64));
fn bench_for_backend<BB: BackendBenchmark>(g: &mut BenchmarkGroup<WallTime>) {
fn bench_for_backend<'i, BB: BackendBenchmark<'i>>(g: &mut BenchmarkGroup<WallTime>) {
g.bench_with_input(
BB::NAME,
&(BENCH_LEN_STRINGS, BENCH_STRING_LEN),
Expand All @@ -191,7 +191,7 @@ fn bench_resolve_already_filled(c: &mut Criterion) {
fn bench_resolve_unchecked_already_filled(c: &mut Criterion) {
let mut g = c.benchmark_group("resolve_unchecked/already-filled");
g.throughput(Throughput::Elements(BENCH_LEN_STRINGS as u64));
fn bench_for_backend<BB: BackendBenchmark>(g: &mut BenchmarkGroup<WallTime>) {
fn bench_for_backend<'i, BB: BackendBenchmark<'i>>(g: &mut BenchmarkGroup<WallTime>) {
g.bench_with_input(
BB::NAME,
&(BENCH_LEN_STRINGS, BENCH_STRING_LEN),
Expand Down Expand Up @@ -220,7 +220,7 @@ fn bench_resolve_unchecked_already_filled(c: &mut Criterion) {
fn bench_get_already_filled(c: &mut Criterion) {
let mut g = c.benchmark_group("get/already-filled");
g.throughput(Throughput::Elements(BENCH_LEN_STRINGS as u64));
fn bench_for_backend<BB: BackendBenchmark>(g: &mut BenchmarkGroup<WallTime>) {
fn bench_for_backend<'i, BB: BackendBenchmark<'i>>(g: &mut BenchmarkGroup<WallTime>) {
g.bench_with_input(
BB::NAME,
&(BENCH_LEN_STRINGS, BENCH_STRING_LEN),
Expand All @@ -246,11 +246,11 @@ fn bench_get_already_filled(c: &mut Criterion) {
fn bench_iter_already_filled(c: &mut Criterion) {
let mut g = c.benchmark_group("iter/already-filled");
g.throughput(Throughput::Elements(BENCH_LEN_STRINGS as u64));
fn bench_for_backend<BB: BackendBenchmark>(g: &mut BenchmarkGroup<WallTime>)
fn bench_for_backend<'i, BB: BackendBenchmark<'i>>(g: &mut BenchmarkGroup<WallTime>)
where
for<'a> &'a <BB as BackendBenchmark>::Backend: IntoIterator<
for<'a> &'a <BB as BackendBenchmark<'i>>::Backend: IntoIterator<
Item = (
<<BB as BackendBenchmark>::Backend as Backend>::Symbol,
<<BB as BackendBenchmark<'i>>::Backend as Backend<'i>>::Symbol,
&'a str,
),
>,
Expand Down
56 changes: 33 additions & 23 deletions benches/setup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use string_interner::{
backend::{Backend, BucketBackend, BufferBackend, StringBackend},
DefaultSymbol,
StringInterner,
DefaultSymbol, StringInterner,
};

/// Alphabet containing all characters that may be put into a benchmark string.
Expand Down Expand Up @@ -79,53 +78,64 @@ pub const BENCH_LEN_STRINGS: usize = 100_000;
pub const BENCH_STRING_LEN: usize = 5;

type FxBuildHasher = fxhash::FxBuildHasher;
type StringInternerWith<B> = StringInterner<B, FxBuildHasher>;
type StringInternerWith<'i, B> = StringInterner<'i, B, FxBuildHasher>;

pub trait BackendBenchmark {
pub trait BackendBenchmark<'i> {
const NAME: &'static str;
type Backend: Backend;
type Backend: Backend<'i>;

fn setup() -> StringInternerWith<Self::Backend> {
fn setup() -> StringInternerWith<'i, Self::Backend> {
<StringInternerWith<Self::Backend>>::new()
}

fn setup_with_capacity(cap: usize) -> StringInternerWith<Self::Backend> {
fn setup_with_capacity(cap: usize) -> StringInternerWith<'i, Self::Backend> {
<StringInternerWith<Self::Backend>>::with_capacity(cap)
}

fn setup_filled(words: &[String]) -> StringInternerWith<Self::Backend> {
words.iter().collect::<StringInternerWith<Self::Backend>>()
fn setup_filled<I, S>(words: I) -> StringInternerWith<'i, Self::Backend>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
words
.into_iter()
.map(|it| it.as_ref().to_string())
.collect::<StringInternerWith<Self::Backend>>()
}

fn setup_filled_with_ids(
words: &[String],
fn setup_filled_with_ids<I, S>(
words: I,
) -> (
StringInternerWith<Self::Backend>,
Vec<<Self::Backend as Backend>::Symbol>,
) {
let mut interner = <StringInternerWith<Self::Backend>>::new();
StringInternerWith<'i, Self::Backend>,
Vec<<Self::Backend as Backend<'i>>::Symbol>,
)
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut interner = <StringInternerWith<'i, Self::Backend>>::new();
let word_ids = words
.iter()
.map(|word| interner.get_or_intern(word))
.into_iter()
.map(|word| interner.get_or_intern(word.as_ref()))
.collect::<Vec<_>>();
(interner, word_ids)
}
}

pub struct BenchBucket;
impl BackendBenchmark for BenchBucket {
impl<'i> BackendBenchmark<'i> for BenchBucket {
const NAME: &'static str = "BucketBackend";
type Backend = BucketBackend<DefaultSymbol>;
type Backend = BucketBackend<'i, DefaultSymbol>;
}

pub struct BenchString;
impl BackendBenchmark for BenchString {
impl<'i> BackendBenchmark<'i> for BenchString {
const NAME: &'static str = "StringBackend";
type Backend = StringBackend<DefaultSymbol>;
type Backend = StringBackend<'i, DefaultSymbol>;
}

pub struct BenchBuffer;
impl BackendBenchmark for BenchBuffer {
impl<'i> BackendBenchmark<'i> for BenchBuffer {
const NAME: &'static str = "BufferBackend";
type Backend = BufferBackend<DefaultSymbol>;
type Backend = BufferBackend<'i, DefaultSymbol>;
}
3 changes: 1 addition & 2 deletions src/backend/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use core::{mem, str};

/// An interner backend that concatenates all interned string contents into one large
/// buffer [`Vec`]. Unlike [`StringBackend`][crate::backend::StringBackend], string
/// lengths are stored in the same buffer as strings preceeding the respective string
/// data.
/// lengths are stored in the same buffer as strings preceeding the respective string data.
///
/// ## Trade-offs
/// - **Advantages:**
Expand Down

0 comments on commit 09f5ea4

Please sign in to comment.