Skip to content

Commit

Permalink
Optimize ThreadInfo::with
Browse files Browse the repository at this point in the history
The RefCell is now borrowed exactly once. In addition a code sequence
that contains an unwrap that is guaranteed to never panic at runtime is
replaced with get_or_insert_with, which makes the intended behavior
clearer and will not emit code to panic even without optimizations.
  • Loading branch information
bjorn3 committed Sep 16, 2021
1 parent af7eede commit f78cd44
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions library/std/src/sys_common/thread_info.rs
Expand Up @@ -17,12 +17,13 @@ impl ThreadInfo {
F: FnOnce(&mut ThreadInfo) -> R,
{
THREAD_INFO
.try_with(move |c| {
if c.borrow().is_none() {
*c.borrow_mut() =
Some(ThreadInfo { stack_guard: None, thread: Thread::new(None) })
}
f(c.borrow_mut().as_mut().unwrap())
.try_with(move |thread_info| {
let mut thread_info = thread_info.borrow_mut();
let thread_info = thread_info.get_or_insert_with(|| ThreadInfo {
stack_guard: None,
thread: Thread::new(None),
});
f(thread_info)
})
.ok()
}
Expand Down

0 comments on commit f78cd44

Please sign in to comment.