2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-20 11:21:14 +00:00

Remove 'armed' from NotificationEntryInner by only creating them when needed

This commit is contained in:
asonix 2024-04-03 13:22:34 -05:00
parent cef9a68307
commit d97cfe2a64

View file

@ -1,10 +1,7 @@
use dashmap::DashMap; use dashmap::{mapref::entry::Entry, DashMap};
use std::{ use std::{
future::Future, future::Future,
sync::{ sync::{Arc, Weak},
atomic::{AtomicBool, Ordering},
Arc, Weak,
},
time::Duration, time::Duration,
}; };
use tokio::sync::Notify; use tokio::sync::Notify;
@ -26,7 +23,6 @@ struct NotificationEntryInner {
key: Arc<str>, key: Arc<str>,
map: Map, map: Map,
notify: Notify, notify: Notify,
armed: AtomicBool,
} }
impl NotificationMap { impl NotificationMap {
@ -37,30 +33,34 @@ impl NotificationMap {
} }
pub(super) fn register_interest(&self, key: Arc<str>) -> NotificationEntry { pub(super) fn register_interest(&self, key: Arc<str>) -> NotificationEntry {
let new_entry = Arc::new(NotificationEntryInner { match self.map.entry(key.clone()) {
key: key.clone(), Entry::Occupied(mut occupied) => {
map: self.map.clone(), if let Some(inner) = occupied.get().upgrade() {
notify: crate::sync::bare_notify(), NotificationEntry { inner }
armed: AtomicBool::new(false), } else {
}); let inner = Arc::new(NotificationEntryInner {
key,
map: self.map.clone(),
notify: crate::sync::bare_notify(),
});
let mut key_entry = self occupied.insert(Arc::downgrade(&inner));
.map
.entry(key)
.or_insert_with(|| Arc::downgrade(&new_entry));
let upgraded_entry = key_entry.value().upgrade(); NotificationEntry { inner }
}
}
Entry::Vacant(vacant) => {
let inner = Arc::new(NotificationEntryInner {
key,
map: self.map.clone(),
notify: crate::sync::bare_notify(),
});
let inner = if let Some(entry) = upgraded_entry { vacant.insert(Arc::downgrade(&inner));
entry
} else {
*key_entry.value_mut() = Arc::downgrade(&new_entry);
new_entry
};
inner.armed.store(true, Ordering::Release); NotificationEntry { inner }
}
NotificationEntry { inner } }
} }
pub(super) fn notify(&self, key: &str) { pub(super) fn notify(&self, key: &str) {
@ -87,8 +87,6 @@ impl Default for NotificationMap {
impl Drop for NotificationEntryInner { impl Drop for NotificationEntryInner {
fn drop(&mut self) { fn drop(&mut self) {
if self.armed.load(Ordering::Acquire) { self.map.remove(&self.key);
self.map.remove(&self.key);
}
} }
} }