address concerns 1, 2

This commit is contained in:
l1npengtul 2022-11-05 22:46:08 +09:00
parent 654256ecfe
commit c49948def4
2 changed files with 12 additions and 9 deletions

View File

@ -98,7 +98,7 @@ fn hokmalock(address: usize) -> &'static HokmaLock {
#[must_use]
struct SusLock<T>
where
T: 'static + Copy,
T: 'static,
{
initialized: AtomicBool,
data: UnsafeCell<MaybeUninit<T>>,
@ -107,7 +107,7 @@ where
impl<T> SusLock<T>
where
T: 'static + Copy,
T: 'static,
{
#[inline]
pub const fn new() -> SusLock<T> {
@ -119,14 +119,14 @@ where
}
#[must_use]
pub fn get(&self) -> Option<T> {
pub fn get(&self) -> Option<&'static T> {
if self.initialized.load(Ordering::SeqCst) {
let hokma = hokmalock(unsafe { mem::transmute(self.data.get()) });
// we forgo the optimistic read, because we don't really care
let guard = hokma.write();
let val = {
let cast: *const T = self.data.get().cast();
unsafe { cast.read() }
unsafe { mem::transmute::<*const T, &'static T>(cast) }
};
guard.the_price_of_silence();
Some(val)
@ -136,9 +136,9 @@ where
}
#[must_use]
pub fn get_or_init(&self, f: impl FnOnce() -> T) -> Option<T> {
let value = f();
pub fn get_or_init(&self, f: impl FnOnce() -> T) -> Option<&'static T> {
if !self.initialized.load(Ordering::SeqCst) {
let value = f();
self.initialized.store(true, Ordering::SeqCst);
let hokma = hokmalock(unsafe { mem::transmute(self.data.get()) });
hokma.write();
@ -216,6 +216,9 @@ pub fn set_ahash_seed(new_seed: Option<[u64; 4]>) -> Result<(), Option<[u64; 4]>
/// See [`set_rhai_ahash_seed`] for more.
#[inline]
#[must_use]
pub fn get_ahash_seed() -> Option<[u64; 4]> {
AHASH_SEED.get_or_init(|| hashing_env::AHASH_SEED).flatten()
pub fn get_ahash_seed() -> &Option<[u64; 4]> {
match AHASH_SEED.get_or_init(|| hashing_env::AHASH_SEED) {
Some(ash) => ash,
None => None,
}
}

View File

@ -79,7 +79,7 @@ impl BuildHasher for StraightHasherBuilder {
pub fn get_hasher() -> ahash::AHasher {
match config::hashing::get_ahash_seed() {
Some([seed1, seed2, seed3, seed4]) if seed1 | seed2 | seed3 | seed4 != 0 => {
ahash::RandomState::with_seeds(seed1, seed2, seed3, seed4).build_hasher()
ahash::RandomState::with_seeds(*seed1, *seed2, *seed3, *seed4).build_hasher()
}
_ => ahash::AHasher::default(),
}