Skip to content

Commit f78cd44

Browse files
committed
Optimize ThreadInfo::with
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.
1 parent af7eede commit f78cd44

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

library/std/src/sys_common/thread_info.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ impl ThreadInfo {
1717
F: FnOnce(&mut ThreadInfo) -> R,
1818
{
1919
THREAD_INFO
20-
.try_with(move |c| {
21-
if c.borrow().is_none() {
22-
*c.borrow_mut() =
23-
Some(ThreadInfo { stack_guard: None, thread: Thread::new(None) })
24-
}
25-
f(c.borrow_mut().as_mut().unwrap())
20+
.try_with(move |thread_info| {
21+
let mut thread_info = thread_info.borrow_mut();
22+
let thread_info = thread_info.get_or_insert_with(|| ThreadInfo {
23+
stack_guard: None,
24+
thread: Thread::new(None),
25+
});
26+
f(thread_info)
2627
})
2728
.ok()
2829
}

0 commit comments

Comments
 (0)