Skip to content

Commit 02e1c96

Browse files
committed
Use std::hint::black_box in benchmarks, upgrade rand usage
1 parent 1429aea commit 02e1c96

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

benches/single_thread_single_byte.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
//! This is *not* a typical use case but it should nevertheless be useful
44
//! for comparing the overhead of different methods.
55
6-
use criterion::{black_box, criterion_group, criterion_main};
6+
use std::hint::black_box;
7+
8+
use criterion::{criterion_group, criterion_main};
79
use criterion::{AxisScale, PlotConfiguration};
810

911
use rtrb::RingBuffer;
@@ -15,6 +17,7 @@ where
1517
{
1618
group.bench_function(id, |b| {
1719
let mut i = 0;
20+
#[allow(clippy::incompatible_msrv)]
1821
b.iter(|| {
1922
assert_eq!(f(black_box(i)), black_box(i));
2023
i = i.wrapping_add(1);

benches/single_thread_two_bytes.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
//! Writing two elements to a three-element queue makes sure
66
//! that there is a ring buffer wrap-around every second time.
77
8+
use std::hint::black_box;
89
use std::io::{Read, Write};
910

10-
use criterion::{black_box, criterion_group, criterion_main};
11+
use criterion::{criterion_group, criterion_main};
1112
use criterion::{AxisScale, PlotConfiguration};
1213

1314
use rtrb::{CopyToUninit, RingBuffer};
@@ -19,6 +20,7 @@ where
1920
{
2021
group.bench_function(id, |b| {
2122
let mut i: u8 = 0;
23+
#[allow(clippy::incompatible_msrv)]
2224
b.iter_batched(
2325
|| {
2426
let mut data = [i, 0];

benches/single_thread_with_chunks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
//!
33
//! Single-threaded usage is *not* a typical use case!
44
5+
use std::hint::black_box;
56
use std::io::{Read, Write};
67

7-
use criterion::{black_box, criterion_group, criterion_main};
8+
use criterion::{criterion_group, criterion_main};
89
use criterion::{AxisScale, PlotConfiguration};
910

1011
use rtrb::{CopyToUninit, RingBuffer};
@@ -16,6 +17,7 @@ where
1617
F: FnMut(&[u8]) -> [u8; CHUNK_SIZE],
1718
M: criterion::measurement::Measurement,
1819
{
20+
#[allow(clippy::incompatible_msrv)]
1921
group.bench_function(id, |b| {
2022
let mut data = [0; CHUNK_SIZE];
2123
let mut i: u8 = 0;

benches/two_threads.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ macro_rules! create_two_threads_benchmark {
22
($($id:literal, $create:expr, $push:expr, $pop:expr,::)+) => {
33

44
use std::convert::TryInto as _;
5+
use std::hint::black_box;
56
use std::sync::{Arc, Barrier};
67

7-
use criterion::{black_box, criterion_group, criterion_main};
8+
use criterion::{criterion_group, criterion_main};
89

910
fn help_with_type_inference<P, C, Create, Push, Pop>(create: Create, push: Push, pop: Pop) -> (Create, Push, Pop)
1011
where
@@ -69,6 +70,7 @@ $(
6970
barrier.wait();
7071
let start_popping = std::time::Instant::now();
7172
for _ in 0..iters {
73+
#[allow(clippy::incompatible_msrv)]
7274
black_box(pop(&mut c));
7375
}
7476
let stop_popping = std::time::Instant::now();

tests/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn parallel() {
6868

6969
#[test]
7070
fn drops() {
71-
use rand::{thread_rng, Rng};
71+
use rand::prelude::*;
7272
use std::sync::atomic::{AtomicUsize, Ordering};
7373

7474
const RUNS: usize = if cfg!(miri) { 10 } else { 100 };
@@ -84,11 +84,11 @@ fn drops() {
8484
}
8585
}
8686

87-
let mut rng = thread_rng();
87+
let mut rng = rand::rng();
8888

8989
for _ in 0..RUNS {
90-
let steps = rng.gen_range(0..if cfg!(miri) { 100 } else { 10_000 });
91-
let additional = rng.gen_range(0..50);
90+
let steps = rng.random_range(0..if cfg!(miri) { 100 } else { 10_000 });
91+
let additional = rng.random_range(0..50);
9292

9393
DROPS.store(0, Ordering::SeqCst);
9494
let (mut p, mut c) = RingBuffer::new(50);

0 commit comments

Comments
 (0)