Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace rand dev-dependency with fastrand #1176

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ crossbeam-queue = { version = "0.3.10", path = "crossbeam-queue", default-featur
crossbeam-utils = { version = "0.8.18", path = "crossbeam-utils", default-features = false, features = ["atomic"] }

[dev-dependencies]
rand = "0.8"

[lints]
workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crossbeam-channel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ std = ["crossbeam-utils/std"]
crossbeam-utils = { version = "0.8.18", path = "../crossbeam-utils", default-features = false, features = ["atomic"] }

[dev-dependencies]
fastrand = "2"
num_cpus = "1.13.0"
rand = "0.8"
signal-hook = "0.3"

[lints]
Expand Down
7 changes: 3 additions & 4 deletions crossbeam-channel/tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crossbeam_channel::{bounded, select, Receiver};
use crossbeam_channel::{RecvError, RecvTimeoutError, TryRecvError};
use crossbeam_channel::{SendError, SendTimeoutError, TrySendError};
use crossbeam_utils::thread::scope;
use rand::{thread_rng, Rng};

fn ms(ms: u64) -> Duration {
Duration::from_millis(ms)
Expand Down Expand Up @@ -519,11 +518,11 @@ fn drops() {
}
}

let mut rng = thread_rng();
let mut rng = fastrand::Rng::new();

for _ in 0..RUNS {
let steps = rng.gen_range(0..STEPS);
let additional = rng.gen_range(0..50);
let steps = rng.usize(0..STEPS);
let additional = rng.usize(0..50);

DROPS.store(0, Ordering::SeqCst);
let (s, r) = bounded::<DropCounter>(50);
Expand Down
7 changes: 3 additions & 4 deletions crossbeam-channel/tests/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crossbeam_channel::{select, unbounded, Receiver};
use crossbeam_channel::{RecvError, RecvTimeoutError, TryRecvError};
use crossbeam_channel::{SendError, SendTimeoutError, TrySendError};
use crossbeam_utils::thread::scope;
use rand::{thread_rng, Rng};

fn ms(ms: u64) -> Duration {
Duration::from_millis(ms)
Expand Down Expand Up @@ -421,11 +420,11 @@ fn drops() {
}
}

let mut rng = thread_rng();
let mut rng = fastrand::Rng::new();

for _ in 0..RUNS {
let steps = rng.gen_range(0..STEPS);
let additional = rng.gen_range(0..STEPS / 10);
let steps = rng.usize(0..STEPS);
let additional = rng.usize(0..STEPS / 10);

DROPS.store(0, Ordering::SeqCst);
let (s, r) = unbounded::<DropCounter>();
Expand Down
5 changes: 2 additions & 3 deletions crossbeam-channel/tests/zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crossbeam_channel::{bounded, select, Receiver};
use crossbeam_channel::{RecvError, RecvTimeoutError, TryRecvError};
use crossbeam_channel::{SendError, SendTimeoutError, TrySendError};
use crossbeam_utils::thread::scope;
use rand::{thread_rng, Rng};

fn ms(ms: u64) -> Duration {
Duration::from_millis(ms)
Expand Down Expand Up @@ -420,10 +419,10 @@ fn drops() {
}
}

let mut rng = thread_rng();
let mut rng = fastrand::Rng::new();

for _ in 0..RUNS {
let steps = rng.gen_range(0..STEPS);
let steps = rng.usize(0..STEPS);

DROPS.store(0, Ordering::SeqCst);
let (s, r) = bounded::<DropCounter>(0);
Expand Down
2 changes: 1 addition & 1 deletion crossbeam-deque/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ crossbeam-epoch = { version = "0.9.17", path = "../crossbeam-epoch", default-fea
crossbeam-utils = { version = "0.8.18", path = "../crossbeam-utils", default-features = false }

[dev-dependencies]
rand = "0.8"
fastrand = "2"

[lints]
workspace = true
11 changes: 5 additions & 6 deletions crossbeam-deque/tests/fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::sync::{Arc, Mutex};
use crossbeam_deque::Steal::{Empty, Success};
use crossbeam_deque::Worker;
use crossbeam_utils::thread::scope;
use rand::Rng;

#[test]
fn smoke() {
Expand Down Expand Up @@ -183,10 +182,10 @@ fn stress() {
});
}

let mut rng = rand::thread_rng();
let mut rng = fastrand::Rng::new();
let mut expected = 0;
while expected < COUNT {
if rng.gen_range(0..3) == 0 {
if rng.u8(0..3) == 0 {
while w.pop().is_some() {
hits.fetch_add(1, SeqCst);
}
Expand Down Expand Up @@ -244,11 +243,11 @@ fn no_starvation() {
});
}

let mut rng = rand::thread_rng();
let mut rng = fastrand::Rng::new();
let mut my_hits = 0;
loop {
for i in 0..rng.gen_range(0..COUNT) {
if rng.gen_range(0..3) == 0 && my_hits == 0 {
for i in 0..rng.usize(0..COUNT) {
if rng.u8(0..3) == 0 && my_hits == 0 {
while w.pop().is_some() {
my_hits += 1;
}
Expand Down
11 changes: 5 additions & 6 deletions crossbeam-deque/tests/injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::sync::{Arc, Mutex};
use crossbeam_deque::Steal::{Empty, Success};
use crossbeam_deque::{Injector, Worker};
use crossbeam_utils::thread::scope;
use rand::Rng;

#[test]
fn smoke() {
Expand Down Expand Up @@ -201,10 +200,10 @@ fn stress() {
});
}

let mut rng = rand::thread_rng();
let mut rng = fastrand::Rng::new();
let mut expected = 0;
while expected < COUNT {
if rng.gen_range(0..3) == 0 {
if rng.u8(0..3) == 0 {
while let Success(_) = q.steal() {
hits.fetch_add(1, SeqCst);
}
Expand Down Expand Up @@ -262,11 +261,11 @@ fn no_starvation() {
});
}

let mut rng = rand::thread_rng();
let mut rng = fastrand::Rng::new();
let mut my_hits = 0;
loop {
for i in 0..rng.gen_range(0..COUNT) {
if rng.gen_range(0..3) == 0 && my_hits == 0 {
for i in 0..rng.usize(0..COUNT) {
if rng.u8(0..3) == 0 && my_hits == 0 {
while let Success(_) = q.steal() {
my_hits += 1;
}
Expand Down
11 changes: 5 additions & 6 deletions crossbeam-deque/tests/lifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::sync::{Arc, Mutex};
use crossbeam_deque::Steal::{Empty, Success};
use crossbeam_deque::Worker;
use crossbeam_utils::thread::scope;
use rand::Rng;

#[test]
fn smoke() {
Expand Down Expand Up @@ -185,10 +184,10 @@ fn stress() {
});
}

let mut rng = rand::thread_rng();
let mut rng = fastrand::Rng::new();
let mut expected = 0;
while expected < COUNT {
if rng.gen_range(0..3) == 0 {
if rng.u8(0..3) == 0 {
while w.pop().is_some() {
hits.fetch_add(1, SeqCst);
}
Expand Down Expand Up @@ -246,11 +245,11 @@ fn no_starvation() {
});
}

let mut rng = rand::thread_rng();
let mut rng = fastrand::Rng::new();
let mut my_hits = 0;
loop {
for i in 0..rng.gen_range(0..COUNT) {
if rng.gen_range(0..3) == 0 && my_hits == 0 {
for i in 0..rng.usize(0..COUNT) {
if rng.u8(0..3) == 0 && my_hits == 0 {
while w.pop().is_some() {
my_hits += 1;
}
Expand Down
2 changes: 1 addition & 1 deletion crossbeam-epoch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ crossbeam-utils = { version = "0.8.18", path = "../crossbeam-utils", default-fea
loom-crate = { package = "loom", version = "0.7.1", optional = true }

[dev-dependencies]
rand = "0.8"
fastrand = "2"

[lints]
workspace = true
9 changes: 4 additions & 5 deletions crossbeam-epoch/examples/sanitize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ use std::thread;
use std::time::{Duration, Instant};

use crossbeam_epoch::{self as epoch, Atomic, Collector, LocalHandle, Owned, Shared};
use rand::Rng;

fn worker(a: Arc<Atomic<AtomicUsize>>, handle: LocalHandle) -> usize {
let mut rng = rand::thread_rng();
let mut rng = fastrand::Rng::new();
let mut sum = 0;

if rng.gen() {
if rng.bool() {
thread::sleep(Duration::from_millis(1));
}
let timeout = Duration::from_millis(rng.gen_range(0..10));
let timeout = Duration::from_millis(rng.u64(0..10));
let now = Instant::now();

while now.elapsed() < timeout {
for _ in 0..100 {
let guard = &handle.pin();
guard.flush();

let val = if rng.gen() {
let val = if rng.bool() {
let p = a.swap(Owned::new(AtomicUsize::new(sum)), AcqRel, guard);
unsafe {
guard.defer_destroy(p);
Expand Down
2 changes: 1 addition & 1 deletion crossbeam-queue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ alloc = []
crossbeam-utils = { version = "0.8.18", path = "../crossbeam-utils", default-features = false }

[dev-dependencies]
rand = "0.8"
fastrand = "2"

[lints]
workspace = true
7 changes: 3 additions & 4 deletions crossbeam-queue/tests/array_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::sync::atomic::{AtomicUsize, Ordering};

use crossbeam_queue::ArrayQueue;
use crossbeam_utils::thread::scope;
use rand::{thread_rng, Rng};

#[test]
fn smoke() {
Expand Down Expand Up @@ -295,11 +294,11 @@ fn drops() {
}
}

let mut rng = thread_rng();
let mut rng = fastrand::Rng::new();

for _ in 0..runs {
let steps = rng.gen_range(0..steps);
let additional = rng.gen_range(0..additional);
let steps = rng.usize(0..steps);
let additional = rng.usize(0..additional);

DROPS.store(0, Ordering::SeqCst);
let q = ArrayQueue::new(50);
Expand Down
7 changes: 3 additions & 4 deletions crossbeam-queue/tests/seg_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::sync::atomic::{AtomicUsize, Ordering};

use crossbeam_queue::SegQueue;
use crossbeam_utils::thread::scope;
use rand::{thread_rng, Rng};

#[test]
fn smoke() {
Expand Down Expand Up @@ -138,11 +137,11 @@ fn drops() {
}
}

let mut rng = thread_rng();
let mut rng = fastrand::Rng::new();

for _ in 0..runs {
let steps = rng.gen_range(0..steps);
let additional = rng.gen_range(0..additional);
let steps = rng.usize(0..steps);
let additional = rng.usize(0..additional);

DROPS.store(0, Ordering::SeqCst);
let q = SegQueue::new();
Expand Down
2 changes: 1 addition & 1 deletion crossbeam-skiplist/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ crossbeam-epoch = { version = "0.9.17", path = "../crossbeam-epoch", default-fea
crossbeam-utils = { version = "0.8.18", path = "../crossbeam-utils", default-features = false }

[dev-dependencies]
rand = "0.8"
fastrand = "2"

[lints]
workspace = true
2 changes: 1 addition & 1 deletion crossbeam-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ atomic-maybe-uninit = { version = "0.3.4", optional = true }
loom = { version = "0.7.1", optional = true }

[dev-dependencies]
rand = "0.8"
fastrand = "2"

[lints]
workspace = true
5 changes: 2 additions & 3 deletions crossbeam-utils/tests/sharded_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::sync::{Arc, TryLockError};
use std::thread;

use crossbeam_utils::sync::ShardedLock;
use rand::Rng;

#[derive(Eq, PartialEq, Debug)]
struct NonCopy(i32);
Expand Down Expand Up @@ -33,9 +32,9 @@ fn frob() {
let tx = tx.clone();
let r = r.clone();
thread::spawn(move || {
let mut rng = rand::thread_rng();
let mut rng = fastrand::Rng::new();
for _ in 0..M {
if rng.gen_bool(1.0 / (N as f64)) {
if rng.u32(0..N) == 0 {
drop(r.write().unwrap());
} else {
drop(r.read().unwrap());
Expand Down