Skip to content

Commit 281b809

Browse files
committed
Use the cold attribute to optimize dispatch_once.
This closes #1.
1 parent f84614c commit 281b809

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/lib.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,21 @@ impl Once {
465465

466466
#[inline(always)]
467467
pub fn call_once<F>(&'static self, work: F) where F: FnOnce() {
468-
let predicate = unsafe { *self.predicate.get() };
469-
if predicate != !0 {
468+
#[cold]
469+
#[inline(never)]
470+
fn once<F>(predicate: *mut dispatch_once_t, work: F)
471+
where F: FnOnce() {
470472
let mut work = Some(work);
471473
let (context, work) = context_and_sync_function(&mut work);
472474
unsafe {
473-
dispatch_once_f(self.predicate.get(), context, work);
475+
dispatch_once_f(predicate, context, work);
476+
}
477+
}
478+
479+
unsafe {
480+
let predicate = self.predicate.get();
481+
if *predicate != !0 {
482+
once(predicate, work);
474483
}
475484
}
476485
}

0 commit comments

Comments
 (0)