Switch from rand to oorandom

The primary motivation here is reducing dependencies. rand has quite a
few of them, and many come from `getrandom` crate (bindings to system
APIs to get true randomness). Some of `getrandom` crates don't have
Apache 2.0 OR MIT license, and it probably doesn't make sense to make
salsa's licensing situation more complicated for feature we don't even
use.

There's a number of small&fast random crates there:

* randomize
* oorandom
* fastrand

I've picked oorandom because it was the simplest & smallest (doesn't
have a thread_local RNG, for example).
This commit is contained in:
Aleksey Kladov 2020-07-23 11:30:23 +02:00
parent 380c4c1dc8
commit b460db700a
2 changed files with 7 additions and 9 deletions

View file

@ -16,7 +16,7 @@ log = "0.4.5"
parking_lot = "0.11.0"
rustc-hash = "1.0"
smallvec = "1.0.0"
rand = { version = "0.7", features = [ "small_rng" ], default-features = false }
oorandom = "11"
salsa-macros = { version = "0.15.0", path = "components/salsa-macros" }

View file

@ -1,7 +1,5 @@
use parking_lot::Mutex;
use rand::rngs::SmallRng;
use rand::Rng;
use rand::SeedableRng;
use oorandom::Rand64;
use std::fmt::Debug;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
@ -33,7 +31,7 @@ struct LruData<Node> {
end_red_zone: usize,
end_yellow_zone: usize,
end_green_zone: usize,
rng: SmallRng,
rng: Rand64,
entries: Vec<Arc<Node>>,
}
@ -141,7 +139,7 @@ where
Self::with_rng(rng_with_seed(seed_str))
}
fn with_rng(rng: SmallRng) -> Self {
fn with_rng(rng: Rand64) -> Self {
LruData {
end_yellow_zone: 0,
end_green_zone: 0,
@ -287,7 +285,7 @@ where
fn pick_index(&mut self, zone: std::ops::Range<usize>) -> usize {
let end_index = std::cmp::min(zone.end, self.entries.len());
self.rng.gen_range(zone.start, end_index)
self.rng.rand_range(zone.start as u64 .. end_index as u64) as usize
}
}
@ -317,12 +315,12 @@ impl LruIndex {
}
}
fn rng_with_seed(seed_str: &str) -> SmallRng {
fn rng_with_seed(seed_str: &str) -> Rand64 {
let mut seed: [u8; 16] = [0; 16];
for (i, &b) in seed_str.as_bytes().iter().take(16).enumerate() {
seed[i] = b;
}
SmallRng::from_seed(seed)
Rand64::new(u128::from_le_bytes(seed))
}
// A note on ordering: