Skip to content

Commit 77369fa

Browse files
committed
Re-implement likely/unlikely with #[cold]
Since rust-lang/rust#120370 `#[cold]` affects branch weights in the backend (again?). Re-implement `likely` and `unlikely` in terms of a `#[cold]` function. These were removed in rust-lang@d677fd4.
1 parent 670213f commit 77369fa

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/util.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1-
// FIXME: Branch prediction hint. This is currently only available on nightly
2-
// but it consistently improves performance by 10-15%.
3-
#[cfg(not(feature = "nightly"))]
4-
pub(crate) use core::convert::{identity as likely, identity as unlikely};
1+
// FIXME: Replace with `core::hint::{likely, unlikely}` once they are stable.
52
#[cfg(feature = "nightly")]
63
pub(crate) use core::intrinsics::{likely, unlikely};
74

5+
#[cfg(not(feature = "nightly"))]
6+
#[inline(always)]
7+
#[cold]
8+
fn cold_path() {}
9+
10+
#[cfg(not(feature = "nightly"))]
11+
#[inline(always)]
12+
pub(crate) fn likely(b: bool) -> bool {
13+
if b {
14+
true
15+
} else {
16+
cold_path();
17+
false
18+
}
19+
}
20+
21+
#[cfg(not(feature = "nightly"))]
22+
#[inline(always)]
23+
pub(crate) fn unlikely(b: bool) -> bool {
24+
if b {
25+
cold_path();
26+
true
27+
} else {
28+
false
29+
}
30+
}
31+
832
// FIXME: use strict provenance functions once they are stable.
933
// Implement it with a transmute for now.
1034
#[inline(always)]

0 commit comments

Comments
 (0)