Performance changes

This commit is contained in:
asonix 2024-01-31 15:39:51 -06:00
parent a9c8dae84b
commit e805992bb1

View file

@ -6,7 +6,7 @@ use std::{
}, },
}; };
use rand::{seq::SliceRandom, thread_rng}; use nanorand::Rng;
use rustls::sign::CertifiedKey; use rustls::sign::CertifiedKey;
/// Create a new resolver channel. The Sender can update the key, and the Receiver can be /// Create a new resolver channel. The Sender can update the key, and the Receiver can be
@ -64,11 +64,11 @@ impl sealed::Shard {
} }
fn update(&self, key: CertifiedKey) { fn update(&self, key: CertifiedKey) {
let mut guard = self.lock.write().unwrap(); {
// update generation while lock is held - ensures any "after" accesses to generation queue *self.lock.write().unwrap() = Arc::new(key);
// at the read-lock for a new key }
self.generation.fetch_add(1, Ordering::Release); // update generation after lock is released. reduces lock contention with readers
*guard = Arc::new(key); self.generation.fetch_add(1, Ordering::AcqRel);
} }
fn read(&self) -> Arc<CertifiedKey> { fn read(&self) -> Arc<CertifiedKey> {
@ -124,7 +124,7 @@ impl<const SHARDS: usize> ChannelResolver<SHARDS> {
#[doc(hidden)] #[doc(hidden)]
pub fn read(&self) -> Arc<CertifiedKey> { pub fn read(&self) -> Arc<CertifiedKey> {
// choose random shard to reduce contention. unwrap since slice is always non-empty // choose random shard to reduce contention. unwrap since slice is always non-empty
self.locks.choose(&mut thread_rng()).unwrap().read() self.locks[nanorand::tls_rng().generate_range(0..SHARDS)].read()
} }
} }