Skip to content

Commit 4023493

Browse files
committed
refactor: change fast_random from xorshift to siphash a counter
1 parent fd61bc9 commit 4023493

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

src/util.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,26 @@ where
2424
header
2525
}
2626

27-
// xor-shift
2827
#[cfg(not(target_arch = "wasm32"))]
2928
pub(crate) fn fast_random() -> u64 {
3029
use std::cell::Cell;
3130
use std::collections::hash_map::RandomState;
3231
use std::hash::{BuildHasher, Hasher};
33-
use std::num::Wrapping;
3432

3533
thread_local! {
36-
static RNG: Cell<Wrapping<u64>> = Cell::new(Wrapping(seed()));
34+
static KEY: RandomState = RandomState::new();
35+
static COUNTER: Cell<u64> = Cell::new(0);
3736
}
3837

39-
fn seed() -> u64 {
40-
let seed = RandomState::new();
38+
KEY.with(|key| {
39+
COUNTER.with(|ctr| {
40+
let n = ctr.get().wrapping_add(1);
41+
ctr.set(n);
4142

42-
let mut out = 0;
43-
let mut cnt = 0;
44-
while out == 0 {
45-
cnt += 1;
46-
let mut hasher = seed.build_hasher();
47-
hasher.write_usize(cnt);
48-
out = hasher.finish();
49-
}
50-
out
51-
}
52-
53-
RNG.with(|rng| {
54-
let mut n = rng.get();
55-
debug_assert_ne!(n.0, 0);
56-
n ^= n >> 12;
57-
n ^= n << 25;
58-
n ^= n >> 27;
59-
rng.set(n);
60-
n.0.wrapping_mul(0x2545_f491_4f6c_dd1d)
43+
let mut h = key.build_hasher();
44+
h.write_u64(n);
45+
h.finish()
46+
})
6147
})
6248
}
6349

0 commit comments

Comments
 (0)