#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum JitPolicy {
Eager,
Lazy {
hot_threshold: usize,
},
Off,
}
impl JitPolicy {
pub const DEFAULT: Self = Self::Eager;
#[must_use]
pub const fn requires_uncompiled_load(self) -> bool {
!matches!(self, Self::Eager)
}
#[must_use]
pub const fn effective(self) -> Self {
if cfg!(target_arch = "x86_64") { self } else { Self::Off }
}
#[must_use]
pub const fn is_lazy(self) -> bool {
matches!(self.effective(), Self::Lazy { .. })
}
#[must_use]
pub const fn hot_threshold(self) -> Option<usize> {
match self.effective() {
Self::Lazy { hot_threshold } => Some(hot_threshold),
Self::Eager | Self::Off => None,
}
}
}
impl Default for JitPolicy {
fn default() -> Self {
Self::DEFAULT
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_eager() {
assert_eq!(JitPolicy::default(), JitPolicy::Eager);
assert_eq!(JitPolicy::DEFAULT, JitPolicy::Eager);
}
#[test]
fn requires_uncompiled_load_per_variant() {
assert!(!JitPolicy::Eager.requires_uncompiled_load());
assert!(JitPolicy::Lazy { hot_threshold: 8 }.requires_uncompiled_load());
assert!(JitPolicy::Lazy { hot_threshold: 0 }.requires_uncompiled_load());
assert!(JitPolicy::Off.requires_uncompiled_load());
}
#[test]
fn effective_collapses_off_x86_64() {
if cfg!(target_arch = "x86_64") {
assert_eq!(JitPolicy::Eager.effective(), JitPolicy::Eager);
assert_eq!(
JitPolicy::Lazy { hot_threshold: 8 }.effective(),
JitPolicy::Lazy { hot_threshold: 8 }
);
assert_eq!(JitPolicy::Off.effective(), JitPolicy::Off);
} else {
assert_eq!(JitPolicy::Eager.effective(), JitPolicy::Off);
assert_eq!(JitPolicy::Lazy { hot_threshold: 8 }.effective(), JitPolicy::Off);
assert_eq!(JitPolicy::Off.effective(), JitPolicy::Off);
}
}
#[test]
fn lazy_threshold_accessor() {
assert_eq!(JitPolicy::Lazy { hot_threshold: 8 }.hot_threshold(), {
if cfg!(target_arch = "x86_64") { Some(8) } else { None }
});
assert_eq!(JitPolicy::Eager.hot_threshold(), None);
assert_eq!(JitPolicy::Off.hot_threshold(), None);
assert!(JitPolicy::Lazy { hot_threshold: 8 }.is_lazy() == cfg!(target_arch = "x86_64"));
assert!(!JitPolicy::Eager.is_lazy());
assert!(!JitPolicy::Off.is_lazy());
}
}