Skip to content

Commit 4d7618f

Browse files
authored
Merge pull request #398 from cuviper/bench-deps
Simplify benchmark dependencies
2 parents 7453721 + 68201eb commit 4d7618f

File tree

3 files changed

+49
-67
lines changed

3 files changed

+49
-67
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ default-features = false
3030

3131
[dev-dependencies]
3232
itertools = "0.14"
33-
rand = {version = "0.9", features = ["small_rng"] }
33+
fastrand = { version = "2", default-features = false }
3434
quickcheck = { version = "1.0", default-features = false }
3535
fnv = "1.0"
36-
lazy_static = "1.3"
3736
serde_derive = "1.0"
3837

3938
[features]

benches/bench.rs

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#![feature(test)]
22

33
extern crate test;
4-
#[macro_use]
5-
extern crate lazy_static;
64

75
use fnv::FnvHasher;
86
use std::hash::BuildHasherDefault;
97
use std::hash::Hash;
8+
use std::sync::LazyLock;
109
type FnvBuilder = BuildHasherDefault<FnvHasher>;
1110

1211
use test::black_box;
@@ -16,14 +15,10 @@ use indexmap::IndexMap;
1615

1716
use std::collections::HashMap;
1817

19-
use rand::rngs::SmallRng;
20-
use rand::seq::SliceRandom;
21-
use rand::SeedableRng;
22-
2318
/// Use a consistently seeded Rng for benchmark stability
24-
fn small_rng() -> SmallRng {
19+
fn small_rng() -> fastrand::Rng {
2520
let seed = u64::from_le_bytes(*b"indexmap");
26-
SmallRng::seed_from_u64(seed)
21+
fastrand::Rng::with_seed(seed)
2722
}
2823

2924
#[bench]
@@ -280,7 +275,7 @@ where
280275
{
281276
let mut v = Vec::from_iter(iter);
282277
let mut rng = small_rng();
283-
v.shuffle(&mut rng);
278+
rng.shuffle(&mut v);
284279
v
285280
}
286281

@@ -357,53 +352,45 @@ const LOOKUP_MAP_SIZE: u32 = 100_000_u32;
357352
const LOOKUP_SAMPLE_SIZE: u32 = 5000;
358353
const SORT_MAP_SIZE: usize = 10_000;
359354

360-
// use lazy_static so that comparison benchmarks use the exact same inputs
361-
lazy_static! {
362-
static ref KEYS: Vec<u32> = shuffled_keys(0..LOOKUP_MAP_SIZE);
363-
}
355+
// use (lazy) statics so that comparison benchmarks use the exact same inputs
364356

365-
lazy_static! {
366-
static ref HMAP_100K: HashMap<u32, u32> = {
367-
let c = LOOKUP_MAP_SIZE;
368-
let mut map = HashMap::with_capacity(c as usize);
369-
let keys = &*KEYS;
370-
for &key in keys {
371-
map.insert(key, key);
372-
}
373-
map
374-
};
375-
}
357+
static KEYS: LazyLock<Vec<u32>> = LazyLock::new(|| shuffled_keys(0..LOOKUP_MAP_SIZE));
376358

377-
lazy_static! {
378-
static ref IMAP_100K: IndexMap<u32, u32> = {
379-
let c = LOOKUP_MAP_SIZE;
380-
let mut map = IndexMap::with_capacity(c as usize);
381-
let keys = &*KEYS;
382-
for &key in keys {
383-
map.insert(key, key);
384-
}
385-
map
386-
};
387-
}
359+
static HMAP_100K: LazyLock<HashMap<u32, u32>> = LazyLock::new(|| {
360+
let c = LOOKUP_MAP_SIZE;
361+
let mut map = HashMap::with_capacity(c as usize);
362+
let keys = &*KEYS;
363+
for &key in keys {
364+
map.insert(key, key);
365+
}
366+
map
367+
});
388368

389-
lazy_static! {
390-
static ref IMAP_SORT_U32: IndexMap<u32, u32> = {
391-
let mut map = IndexMap::with_capacity(SORT_MAP_SIZE);
392-
for &key in &KEYS[..SORT_MAP_SIZE] {
393-
map.insert(key, key);
394-
}
395-
map
396-
};
397-
}
398-
lazy_static! {
399-
static ref IMAP_SORT_S: IndexMap<String, String> = {
400-
let mut map = IndexMap::with_capacity(SORT_MAP_SIZE);
401-
for &key in &KEYS[..SORT_MAP_SIZE] {
402-
map.insert(format!("{:^16x}", &key), String::new());
403-
}
404-
map
405-
};
406-
}
369+
static IMAP_100K: LazyLock<IndexMap<u32, u32>> = LazyLock::new(|| {
370+
let c = LOOKUP_MAP_SIZE;
371+
let mut map = IndexMap::with_capacity(c as usize);
372+
let keys = &*KEYS;
373+
for &key in keys {
374+
map.insert(key, key);
375+
}
376+
map
377+
});
378+
379+
static IMAP_SORT_U32: LazyLock<IndexMap<u32, u32>> = LazyLock::new(|| {
380+
let mut map = IndexMap::with_capacity(SORT_MAP_SIZE);
381+
for &key in &KEYS[..SORT_MAP_SIZE] {
382+
map.insert(key, key);
383+
}
384+
map
385+
});
386+
387+
static IMAP_SORT_S: LazyLock<IndexMap<String, String>> = LazyLock::new(|| {
388+
let mut map = IndexMap::with_capacity(SORT_MAP_SIZE);
389+
for &key in &KEYS[..SORT_MAP_SIZE] {
390+
map.insert(format!("{:^16x}", &key), String::new());
391+
}
392+
map
393+
});
407394

408395
#[bench]
409396
fn lookup_hashmap_100_000_multi(b: &mut Bencher) {
@@ -523,7 +510,7 @@ fn hashmap_merge_shuffle(b: &mut Bencher) {
523510
b.iter(|| {
524511
let mut merged = first_map.clone();
525512
v.extend(second_map.iter().map(|(&k, &v)| (k, v)));
526-
v.shuffle(&mut rng);
513+
rng.shuffle(&mut v);
527514
merged.extend(v.drain(..));
528515

529516
merged
@@ -550,7 +537,7 @@ fn indexmap_merge_shuffle(b: &mut Bencher) {
550537
b.iter(|| {
551538
let mut merged = first_map.clone();
552539
v.extend(second_map.iter().map(|(&k, &v)| (k, v)));
553-
v.shuffle(&mut rng);
540+
rng.shuffle(&mut v);
554541
merged.extend(v.drain(..));
555542

556543
merged
@@ -562,7 +549,7 @@ fn swap_remove_indexmap_100_000(b: &mut Bencher) {
562549
let map = IMAP_100K.clone();
563550
let mut keys = Vec::from_iter(map.keys().copied());
564551
let mut rng = small_rng();
565-
keys.shuffle(&mut rng);
552+
rng.shuffle(&mut keys);
566553

567554
b.iter(|| {
568555
let mut map = map.clone();
@@ -579,7 +566,7 @@ fn shift_remove_indexmap_100_000_few(b: &mut Bencher) {
579566
let map = IMAP_100K.clone();
580567
let mut keys = Vec::from_iter(map.keys().copied());
581568
let mut rng = small_rng();
582-
keys.shuffle(&mut rng);
569+
rng.shuffle(&mut keys);
583570
keys.truncate(50);
584571

585572
b.iter(|| {
@@ -600,7 +587,7 @@ fn shift_remove_indexmap_2_000_full(b: &mut Bencher) {
600587
map.insert(key, key);
601588
}
602589
let mut rng = small_rng();
603-
keys.shuffle(&mut rng);
590+
rng.shuffle(&mut keys);
604591

605592
b.iter(|| {
606593
let mut map = map.clone();

benches/faststring.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@ use indexmap::IndexMap;
88

99
use std::collections::HashMap;
1010

11-
use rand::rngs::SmallRng;
12-
use rand::seq::SliceRandom;
13-
use rand::SeedableRng;
14-
1511
use std::hash::{Hash, Hasher};
1612

1713
use std::borrow::Borrow;
1814
use std::ops::Deref;
1915

2016
/// Use a consistently seeded Rng for benchmark stability
21-
fn small_rng() -> SmallRng {
17+
fn small_rng() -> fastrand::Rng {
2218
let seed = u64::from_le_bytes(*b"indexmap");
23-
SmallRng::seed_from_u64(seed)
19+
fastrand::Rng::with_seed(seed)
2420
}
2521

2622
#[derive(PartialEq, Eq, Copy, Clone)]
@@ -68,7 +64,7 @@ where
6864
{
6965
let mut v = Vec::from_iter(iter);
7066
let mut rng = small_rng();
71-
v.shuffle(&mut rng);
67+
rng.shuffle(&mut v);
7268
v
7369
}
7470

0 commit comments

Comments
 (0)