Skip to content

Commit

Permalink
Fix nightly build and add inline attributes to {un,}likely (#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-sareen authored Nov 7, 2024
1 parent 753f71c commit 41501a5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ default = ["builtin_env_logger"]
# See `crate::util::logger` for more details.
builtin_env_logger = ["dep:env_logger"]

# Enable this feature if you want to use nightly features and compiler
nightly = []

# This feature is only supported on x86-64 for now
# It's manually added to CI scripts
perf_counter = ["dep:pfm"]
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// to me - considering it will break our API and all the efforts for all the developers to make the change, it may
// not worth it.
#![allow(clippy::upper_case_acronyms)]
// Use the `{likely, unlikely}` provided by compiler when using nightly
#![cfg_attr(feature = "nightly", feature(core_intrinsics))]

//! Memory Management ToolKit (MMTk) is a portable and high performance memory manager
//! that includes various garbage collection algorithms and provides clean and efficient
Expand Down
11 changes: 7 additions & 4 deletions src/util/rust_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,27 @@ pub const fn min_of_usize(a: usize, b: usize) -> usize {
}
}

#[rustversion::nightly]
#[cfg(feature = "nightly")]
pub use core::intrinsics::{likely, unlikely};

// likely() and unlikely() compiler hints in stable Rust
// [1]: https://github.com/rust-lang/hashbrown/blob/a41bd76de0a53838725b997c6085e024c47a0455/src/raw/mod.rs#L48-L70
// [2]: https://users.rust-lang.org/t/compiler-hint-for-unlikely-likely-for-if-branches/62102/3
#[rustversion::not(nightly)]
#[cfg(not(feature = "nightly"))]
#[inline]
#[cold]
fn cold() {}

#[rustversion::not(nightly)]
#[cfg(not(feature = "nightly"))]
#[inline]
pub fn likely(b: bool) -> bool {
if !b {
cold();
}
b
}
#[rustversion::not(nightly)]
#[cfg(not(feature = "nightly"))]
#[inline]
pub fn unlikely(b: bool) -> bool {
if b {
cold();
Expand Down

0 comments on commit 41501a5

Please sign in to comment.