From 2e63747b77eb96ea2ef68c4322b346a304e8f655 Mon Sep 17 00:00:00 2001 From: Paul Masurel Date: Fri, 26 Apr 2024 11:06:41 +0900 Subject: [PATCH] Removing useless Atomic --- chitchat/src/listener.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/chitchat/src/listener.rs b/chitchat/src/listener.rs index d42607e..50dc2bf 100644 --- a/chitchat/src/listener.rs +++ b/chitchat/src/listener.rs @@ -1,6 +1,5 @@ use std::collections::{BTreeMap, HashMap}; use std::ops::Bound; -use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, RwLock, Weak}; use tracing::error; @@ -59,9 +58,8 @@ impl Listeners { let key_prefix = key_prefix.to_string(); let weak_listeners = Arc::downgrade(&self.inner); let mut inner_listener_guard = self.inner.write().unwrap(); - let new_idx = inner_listener_guard - .listener_idx - .fetch_add(1, Ordering::Relaxed); + let new_idx = inner_listener_guard.listener_idx; + inner_listener_guard.listener_idx += 1; let callback_entry = CallbackEntry { prefix: key_prefix.clone(), callback: boxed_listener, @@ -93,7 +91,7 @@ struct InnerListeners { // A trie would have been more efficient, but in reality we don't have // that many listeners. listeners: BTreeMap>, - listener_idx: AtomicUsize, + listener_idx: usize, // Callbacks is a hashmap because as we delete listeners, we create "holes" in the // callback_id -> callback mapping callbacks: HashMap, @@ -171,6 +169,8 @@ impl InnerListeners { #[cfg(test)] mod tests { + use std::sync::atomic::{Ordering, AtomicUsize}; + use super::*; use crate::ChitchatId;