-
Notifications
You must be signed in to change notification settings - Fork 44
Closed
Description
I've identified an inefficient pattern in the code where Arc<Mutex<Option<Vec>>> is used inside a thread_local! macro. This combination is redundant and represents an unnecessary use of atomic operations.
thread_local! {
static POPULAR_BABY_NAMES_2021: Arc<Mutex<Option<Vec<String>>>> =
Arc::new(Mutex::new(None));
}I recommend changing to Rc<RefCell<Vec>> like the following example
use std::cell::RefCell;
use std::rc::Rc;
thread_local! {
static POPULAR_BABY_NAMES_2021: Rc<RefCell<Vec<String>>> =
Rc::new(RefCell::new(vec![]));
}
fn main() {
let rc: Rc<RefCell<Vec<String>>> = POPULAR_BABY_NAMES_2021.with(|rc: &Rc<RefCell<Vec<String>>>| rc.clone());
let mut inner: std::cell::RefMut<'_, Vec<String>> = rc.borrow_mut();
inner.extend(vec![
String::from("Olivia"),
String::from("Liam"),
String::from("Emma"),
String::from("Noah"),
]);
println!("popular baby names of 2021: {:?}", inner);
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels