use std::hint;
#[inline(always)]
pub fn inline_hot_path<F, R>(f: F) -> R
where
F: FnOnce() -> R,
{
f()
}
#[cold]
#[inline(never)]
pub fn cold_path<F, R>(f: F) -> R
where
F: FnOnce() -> R,
{
f()
}
#[inline(always)]
pub fn likely<T>(b: T) -> T
where
T: Copy,
{
b
}
#[inline(always)]
pub fn unlikely<T>(b: T) -> T
where
T: Copy,
{
b
}
#[macro_export]
macro_rules! feature_enabled {
($feature:literal) => {
cfg!(feature = $feature)
};
}
#[macro_export]
macro_rules! if_optimized {
($optimized:block, $debug:block) => {
#[cfg(not(debug_assertions))]
$optimized
#[cfg(debug_assertions)]
$debug
};
}
#[macro_export]
macro_rules! debug_only {
($expr:expr) => {
#[cfg(debug_assertions)]
$expr
};
}
#[macro_export]
macro_rules! release_only {
($expr:expr) => {
#[cfg(not(debug_assertions))]
$expr
};
}
#[inline(always)]
pub fn prefetch_read<T>(data: *const T) {
#[cfg(target_arch = "x86_64")]
{
unsafe {
std::arch::x86_64::_mm_prefetch(data as *const i8, std::arch::x86_64::_MM_HINT_T0);
}
}
}
#[inline(always)]
pub fn memory_fence() {
std::sync::atomic::fence(std::sync::atomic::Ordering::SeqCst);
}
#[macro_export]
macro_rules! fast_path {
($condition:expr, $fast:block, $slow:block) => {
if likely($condition) {
inline_hot_path(|| $fast)
} else {
cold_path(|| $slow)
}
};
}
pub struct CompileConfig;
impl CompileConfig {
pub const fn is_debug() -> bool {
cfg!(debug_assertions)
}
pub const fn is_release() -> bool {
!cfg!(debug_assertions)
}
pub const fn profiling_enabled() -> bool {
cfg!(feature = "profiling") || cfg!(debug_assertions)
}
pub const fn caching_enabled() -> bool {
cfg!(feature = "caching") || cfg!(feature = "optimizations")
}
pub const fn pooling_enabled() -> bool {
cfg!(feature = "pooling") || cfg!(feature = "optimizations")
}
pub const fn lazy_init_enabled() -> bool {
cfg!(feature = "lazy-init") || cfg!(feature = "optimizations")
}
pub const fn optimization_level() -> u8 {
if cfg!(feature = "optimizations") {
3 } else if cfg!(not(debug_assertions)) {
2 } else {
0 }
}
}
pub mod collections {
#[cfg(feature = "ahash")]
pub type FastHashMap<K, V> = std::collections::HashMap<K, V, ahash::RandomState>;
#[cfg(not(feature = "ahash"))]
pub type FastHashMap<K, V> = std::collections::HashMap<K, V>;
#[cfg(feature = "ahash")]
pub type FastHashSet<T> = std::collections::HashSet<T, ahash::RandomState>;
#[cfg(not(feature = "ahash"))]
pub type FastHashSet<T> = std::collections::HashSet<T>;
}
#[macro_export]
macro_rules! likely_if {
($condition:expr, $then:block) => {
if $crate::compile_opts::likely($condition) {
$then
}
};
($condition:expr, $then:block, $else:block) => {
if $crate::compile_opts::likely($condition) {
$then
} else {
$else
}
};
}
#[macro_export]
macro_rules! unlikely_if {
($condition:expr, $then:block) => {
if $crate::compile_opts::unlikely($condition) {
$then
}
};
($condition:expr, $then:block, $else:block) => {
if $crate::compile_opts::unlikely($condition) {
$then
} else {
$else
}
};
}
#[macro_export]
macro_rules! with_feature {
($feature:literal, $code:block) => {
#[cfg(feature = $feature)]
$code
};
}
#[macro_export]
macro_rules! without_feature {
($feature:literal, $code:block) => {
#[cfg(not(feature = $feature))]
$code
};
}
#[macro_export]
macro_rules! feature_stub {
($feature:literal, $fn_name:ident, $return_type:ty, $default_value:expr) => {
#[cfg(feature = $feature)]
pub fn $fn_name() -> $return_type {
$default_value
}
#[cfg(not(feature = $feature))]
#[inline(always)]
pub fn $fn_name() -> $return_type {
$default_value
}
};
}
#[macro_export]
macro_rules! concat_strs {
($($str:expr),*) => {
concat!($($str),*)
};
}
#[repr(align(64))] pub struct CacheLineAligned<T>(pub T);
#[repr(align(16))] pub struct SimdAligned<T>(pub T);
pub mod cpu_features {
#[cfg(target_arch = "x86_64")]
pub fn has_sse4_2() -> bool {
is_x86_feature_detected!("sse4.2")
}
#[cfg(target_arch = "x86_64")]
pub fn has_avx2() -> bool {
is_x86_feature_detected!("avx2")
}
#[cfg(not(target_arch = "x86_64"))]
pub fn has_sse4_2() -> bool { false }
#[cfg(not(target_arch = "x86_64"))]
pub fn has_avx2() -> bool { false }
}
#[cfg(not(target_os = "macos"))]
#[link_section = ".text.hot"]
pub fn hot_function() {
}
#[cfg(target_os = "macos")]
pub fn hot_function() {
}
#[cfg(not(target_os = "macos"))]
#[link_section = ".text.cold"]
pub fn cold_function() {
}
#[cfg(target_os = "macos")]
pub fn cold_function() {
}
#[macro_export]
macro_rules! static_assert_config {
($condition:expr, $message:expr) => {
const _: () = assert!($condition, $message);
};
}
static_assert_config!(
CompileConfig::optimization_level() <= 3,
"Invalid optimization level"
);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compile_config() {
assert!(CompileConfig::optimization_level() <= 3);
#[cfg(debug_assertions)]
assert!(CompileConfig::is_debug());
#[cfg(not(debug_assertions))]
assert!(CompileConfig::is_release());
}
#[test]
fn test_feature_macros() {
debug_only!({
});
release_only!({
});
if_optimized!({
}, {
});
}
#[test]
fn test_branch_hints() {
let condition = true;
likely_if!(condition, {
});
unlikely_if!(!condition, {
});
}
#[test]
fn test_collections() {
let mut map = collections::FastHashMap::default();
map.insert("key", "value");
assert_eq!(map.get("key"), Some(&"value"));
let mut set = collections::FastHashSet::default();
set.insert(42);
assert!(set.contains(&42));
}
}