Skip to content

Commit 505c6ae

Browse files
committed
use all threads in benchmark; use borrow in benchmark
1 parent c90c587 commit 505c6ae

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

benches/comparison.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
use std::{collections::HashMap, hash::Hash, sync::LazyLock};
1+
use std::{borrow::Borrow, collections::HashMap, hash::Hash, sync::LazyLock};
22

33
use criterion::{Criterion, criterion_group, criterion_main};
4+
use intern_mint::BorrowedInterned;
45

56
include!("./random_strings_pool.rs");
67

78
#[inline]
8-
fn use_generic<T: Eq + Hash + Clone>(intern: fn(&str) -> T) -> u32 {
9-
let mut map = HashMap::<T, u32>::with_capacity(POOL.len());
9+
fn use_generic<K, Q>(intern: fn(&str) -> K) -> u32
10+
where
11+
K: Eq + Hash + Clone + Borrow<Q>,
12+
Q: Eq + Hash + ?Sized,
13+
{
14+
let mut map = HashMap::<K, u32>::with_capacity(POOL.len());
1015

1116
for i in 0..2 {
1217
for &s in POOL {
@@ -20,26 +25,27 @@ fn use_generic<T: Eq + Hash + Clone>(intern: fn(&str) -> T) -> u32 {
2025

2126
for key in map.keys() {
2227
let key = key.clone();
23-
sum += map.get(&key).copied().unwrap_or_default();
28+
let borrowed = Borrow::<Q>::borrow(&key);
29+
sum += map.get(borrowed).copied().unwrap_or_default();
2430
}
2531

2632
sum
2733
}
2834

2935
fn use_intern_mint() -> u32 {
3036
use intern_mint::Interned;
31-
use_generic(|o| Interned::from(o))
37+
use_generic::<Interned, BorrowedInterned>(|o| Interned::from(o))
3238
}
3339

3440
fn use_internment() -> u32 {
3541
use internment::ArcIntern;
36-
use_generic(|o| ArcIntern::<String>::from_ref(o))
42+
use_generic::<ArcIntern<String>, ArcIntern<String>>(|o| ArcIntern::from_ref(o))
3743
}
3844

3945
fn use_intern_arc() -> u32 {
40-
use intern_arc::HashInterner;
46+
use intern_arc::{HashInterner, InternedHash};
4147
static INTERN_ARC_POOL: LazyLock<HashInterner<str>> = LazyLock::new(Default::default);
42-
use_generic(|o| INTERN_ARC_POOL.intern_ref(o))
48+
use_generic::<InternedHash<str>, InternedHash<str>>(|o| INTERN_ARC_POOL.intern_ref(o))
4349
}
4450

4551
fn use_multithreaded(to_test: fn() -> u32) {
@@ -50,7 +56,7 @@ fn benchmark(c: &mut Criterion) {
5056
rayon::ThreadPoolBuilder::new()
5157
.num_threads(
5258
std::thread::available_parallelism()
53-
.map(|o| o.get().div_ceil(2))
59+
.map(|o| o.get())
5460
.unwrap_or(1),
5561
)
5662
.build_global()

0 commit comments

Comments
 (0)