use scirs2_core::ndarray::Array2;
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
pub mod branch_optimization {
#[inline(always)]
pub fn likely(b: bool) -> bool {
#[cold]
fn cold() {}
if b {
true
} else {
cold();
false
}
}
#[inline(always)]
pub fn unlikely(b: bool) -> bool {
#[cold]
fn cold() {}
if b {
cold();
true
} else {
false
}
}
#[inline]
pub fn optimized_classify(
score: f64,
threshold: f64,
positive_class: i32,
negative_class: i32,
) -> i32 {
if likely(score >= threshold) {
positive_class
} else {
negative_class
}
}
#[inline(always)]
pub fn branchless_binary_classify(score: f64, threshold: f64) -> i32 {
(score >= threshold) as i32
}
pub fn optimized_batch_predict<F>(data: &[f64], threshold: f64, predict_fn: F) -> Vec<i32>
where
F: Fn(f64) -> f64,
{
let mut results = Vec::with_capacity(data.len());
for &value in data {
let score = predict_fn(value);
results.push(if likely(score >= threshold) { 1 } else { 0 });
}
results
}
}
pub mod cpu_optimization {
pub struct CPUFeatureDetector {
pub has_avx512: bool,
pub has_avx2: bool,
pub has_sse4_2: bool,
pub has_fma: bool,
pub cache_line_size: usize,
pub l1_cache_size: usize,
pub l2_cache_size: usize,
}
impl CPUFeatureDetector {
pub fn detect() -> Self {
Self {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
has_avx512: is_x86_feature_detected!("avx512f"),
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
has_avx512: false,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
has_avx2: is_x86_feature_detected!("avx2"),
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
has_avx2: false,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
has_sse4_2: is_x86_feature_detected!("sse4.2"),
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
has_sse4_2: false,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
has_fma: is_x86_feature_detected!("fma"),
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
has_fma: false,
cache_line_size: 64, l1_cache_size: 32 * 1024, l2_cache_size: 256 * 1024, }
}
pub fn optimal_simd_width(&self) -> usize {
if self.has_avx512 {
64 } else if self.has_avx2 {
32 } else if self.has_sse4_2 {
16 } else {
8 }
}
pub fn optimal_block_size<T>(&self) -> usize {
let element_size = std::mem::size_of::<T>();
let elements_per_cache_line = self.cache_line_size / element_size;
let max_elements_in_l1 = self.l1_cache_size / element_size;
(max_elements_in_l1 / elements_per_cache_line) * elements_per_cache_line
}
}
pub struct FastPathDispatcher {
detector: CPUFeatureDetector,
}
impl Default for FastPathDispatcher {
fn default() -> Self {
Self::new()
}
}
impl FastPathDispatcher {
pub fn new() -> Self {
Self {
detector: CPUFeatureDetector::detect(),
}
}
pub fn dispatch_sum(&self, data: &[f64]) -> f64 {
if self.detector.has_avx2 && data.len() >= 32 {
crate::performance::simd_stats::fast_sum(data)
} else if data.len() >= 1000 {
data.chunks(1000)
.map(|chunk| chunk.iter().sum::<f64>())
.sum()
} else {
data.iter().sum()
}
}
pub fn dispatch_mean(&self, data: &[f64]) -> f64 {
if data.is_empty() {
return 0.0;
}
if self.detector.has_avx2 && data.len() >= 16 {
crate::performance::simd_stats::fast_mean(data)
} else {
data.iter().sum::<f64>() / data.len() as f64
}
}
pub fn features(&self) -> &CPUFeatureDetector {
&self.detector
}
}
}
pub mod dummy_optimization {
use super::*;
pub struct PredictionLookupCache {
cache: HashMap<u64, f64>,
strategy_cache: HashMap<u8, f64>, hit_count: AtomicUsize,
miss_count: AtomicUsize,
}
impl Default for PredictionLookupCache {
fn default() -> Self {
Self::new()
}
}
impl PredictionLookupCache {
pub fn new() -> Self {
Self {
cache: HashMap::new(),
strategy_cache: HashMap::new(),
hit_count: AtomicUsize::new(0),
miss_count: AtomicUsize::new(0),
}
}
pub fn get_or_compute<F>(&mut self, key: u64, compute_fn: F) -> f64
where
F: FnOnce() -> f64,
{
if let Some(&cached) = self.cache.get(&key) {
self.hit_count.fetch_add(1, Ordering::Relaxed);
cached
} else {
self.miss_count.fetch_add(1, Ordering::Relaxed);
let result = compute_fn();
self.cache.insert(key, result);
result
}
}
pub fn cache_strategy_result(&mut self, strategy_id: u8, result: f64) {
self.strategy_cache.insert(strategy_id, result);
}
pub fn get_strategy_result(&self, strategy_id: u8) -> Option<f64> {
self.strategy_cache.get(&strategy_id).copied()
}
pub fn hit_rate(&self) -> f64 {
let hits = self.hit_count.load(Ordering::Relaxed) as f64;
let misses = self.miss_count.load(Ordering::Relaxed) as f64;
let total = hits + misses;
if total > 0.0 {
hits / total
} else {
0.0
}
}
pub fn clear(&mut self) {
self.cache.clear();
self.strategy_cache.clear();
self.hit_count.store(0, Ordering::Relaxed);
self.miss_count.store(0, Ordering::Relaxed);
}
}
#[allow(dead_code)] pub struct BatchPredictor {
cache: PredictionLookupCache,
dispatcher: cpu_optimization::FastPathDispatcher,
}
impl Default for BatchPredictor {
fn default() -> Self {
Self::new()
}
}
impl BatchPredictor {
pub fn new() -> Self {
Self {
cache: PredictionLookupCache::new(),
dispatcher: cpu_optimization::FastPathDispatcher::new(),
}
}
pub fn predict_classification_batch(
&mut self,
strategy_id: u8,
x: &Array2<f64>,
prediction_value: i32,
) -> Vec<i32> {
if let Some(_cached) = self.cache.get_strategy_result(strategy_id) {
return vec![prediction_value; x.nrows()];
}
self.cache
.cache_strategy_result(strategy_id, prediction_value as f64);
vec![prediction_value; x.nrows()]
}
pub fn predict_regression_batch(
&mut self,
strategy_id: u8,
x: &Array2<f64>,
prediction_value: f64,
) -> Vec<f64> {
if let Some(cached) = self.cache.get_strategy_result(strategy_id) {
return vec![cached; x.nrows()];
}
self.cache
.cache_strategy_result(strategy_id, prediction_value);
vec![prediction_value; x.nrows()]
}
pub fn cache_stats(&self) -> (f64, usize, usize) {
(
self.cache.hit_rate(),
self.cache.hit_count.load(Ordering::Relaxed),
self.cache.miss_count.load(Ordering::Relaxed),
)
}
}
#[allow(dead_code)] pub struct CompactPredictionStorage {
data: Vec<u8>,
prediction_type: PredictionType,
length: usize,
}
#[derive(Debug, Clone, Copy)]
pub enum PredictionType {
BinaryClassification,
MultiClassification { num_classes: u8 },
Regression,
}
impl CompactPredictionStorage {
pub fn new(prediction_type: PredictionType) -> Self {
Self {
data: Vec::new(),
prediction_type,
length: 0,
}
}
pub fn store_binary_predictions(&mut self, predictions: &[bool]) {
self.length = predictions.len();
self.data.clear();
self.data.reserve(predictions.len().div_ceil(8));
let mut current_byte = 0u8;
for (i, &pred) in predictions.iter().enumerate() {
if pred {
current_byte |= 1 << (i % 8);
}
if i % 8 == 7 || i == predictions.len() - 1 {
self.data.push(current_byte);
current_byte = 0;
}
}
}
pub fn get_binary_predictions(&self) -> Vec<bool> {
let mut result = Vec::with_capacity(self.length);
for (byte_idx, &byte) in self.data.iter().enumerate() {
for bit_idx in 0..8 {
let global_idx = byte_idx * 8 + bit_idx;
if global_idx >= self.length {
break;
}
result.push((byte >> bit_idx) & 1 == 1);
}
}
result
}
pub fn memory_usage(&self) -> usize {
self.data.len()
}
pub fn compression_ratio(&self) -> f64 {
if self.length == 0 {
return 0.0;
}
let vec_bool_size = self.length * std::mem::size_of::<bool>();
let compact_size = self.data.len();
vec_bool_size as f64 / compact_size as f64
}
}
}
#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
use super::*;
use scirs2_core::ndarray::Array2;
#[test]
fn test_branch_optimization() {
assert!(branch_optimization::likely(true));
assert!(!branch_optimization::unlikely(false));
let result = branch_optimization::optimized_classify(0.7, 0.5, 1, 0);
assert_eq!(result, 1);
let result = branch_optimization::optimized_classify(0.3, 0.5, 1, 0);
assert_eq!(result, 0);
assert_eq!(branch_optimization::branchless_binary_classify(0.7, 0.5), 1);
assert_eq!(branch_optimization::branchless_binary_classify(0.3, 0.5), 0);
}
#[test]
fn test_cpu_optimization() {
let detector = cpu_optimization::CPUFeatureDetector::detect();
let width = detector.optimal_simd_width();
assert!(width >= 8);
let block_size = detector.optimal_block_size::<f64>();
assert!(block_size > 0);
let dispatcher = cpu_optimization::FastPathDispatcher::new();
let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let sum = dispatcher.dispatch_sum(&data);
assert_eq!(sum, 15.0);
let mean = dispatcher.dispatch_mean(&data);
assert_eq!(mean, 3.0);
}
#[test]
fn test_dummy_optimization() {
let mut cache = dummy_optimization::PredictionLookupCache::new();
let result = cache.get_or_compute(42, || 1.23);
assert_eq!(result, 1.23);
let result2 = cache.get_or_compute(42, || 999.0); assert_eq!(result2, 1.23);
cache.cache_strategy_result(1, 42.0);
assert_eq!(cache.get_strategy_result(1), Some(42.0));
assert_eq!(cache.get_strategy_result(2), None);
let mut predictor = dummy_optimization::BatchPredictor::new();
let x = Array2::zeros((10, 5));
let predictions = predictor.predict_classification_batch(0, &x, 1);
assert_eq!(predictions.len(), 10);
assert!(predictions.iter().all(|&p| p == 1));
let predictions = predictor.predict_regression_batch(0, &x, 1.23);
assert_eq!(predictions.len(), 10);
assert!(!predictions.is_empty());
}
#[test]
fn test_compact_storage() {
let mut storage = dummy_optimization::CompactPredictionStorage::new(
dummy_optimization::PredictionType::BinaryClassification,
);
let predictions = vec![true, false, true, true, false, false, true, false];
storage.store_binary_predictions(&predictions);
let retrieved = storage.get_binary_predictions();
assert_eq!(retrieved, predictions);
let ratio = storage.compression_ratio();
assert!(ratio > 1.0);
let memory_usage = storage.memory_usage();
assert!(memory_usage < predictions.len() * std::mem::size_of::<bool>());
}
}