membase 0.2.1

Ultra-high performance async runtime with fast task cycling, lock-free scheduling, and zero-overhead performance
Documentation
//! Prefetching optimizations for memory mapping.
//!
//! This module provides functionality for optimizing memory access patterns
//! through prefetching.

use crate::advanced::PrefetchStrategy;

/// Apply a prefetching strategy to a memory region.
///
/// # Safety
///
/// This function is unsafe because it operates on raw memory.
#[inline]
pub unsafe fn apply_strategy(ptr: *mut u8, len: usize, strategy: PrefetchStrategy) {
    match strategy {
        PrefetchStrategy::None => {
            // No prefetching
        },
        PrefetchStrategy::Sequential => {
            // Sequential prefetching
            prefetch_sequential(ptr, len);
        },
        PrefetchStrategy::Random => {
            // Random access prefetching
            // For random access, we don't do any specific prefetching
        },
        PrefetchStrategy::Custom(lookahead) => {
            // Custom prefetching with specified lookahead
            prefetch_custom(ptr, len, lookahead);
        },
    }
}

/// Prefetch memory sequentially.
///
/// # Safety
///
/// This function is unsafe because it operates on raw memory.
#[inline]
unsafe fn prefetch_sequential(ptr: *mut u8, len: usize) {
    // Get the page size
    let page_size = page_size();
    
    // Prefetch the first few pages
    let prefetch_pages = 4;
    let prefetch_size = std::cmp::min(len, prefetch_pages * page_size);
    
    for i in (0..prefetch_size).step_by(64) {
        prefetch_read((ptr as usize + i) as *const u8);
    }
}

/// Prefetch memory with a custom lookahead.
///
/// # Safety
///
/// This function is unsafe because it operates on raw memory.
#[inline]
unsafe fn prefetch_custom(ptr: *mut u8, len: usize, lookahead: usize) {
    // Prefetch with the specified lookahead
    let prefetch_size = std::cmp::min(len, lookahead);
    
    for i in (0..prefetch_size).step_by(64) {
        prefetch_read((ptr as usize + i) as *const u8);
    }
}

/// Prefetch memory for reading.
///
/// # Safety
///
/// This function is unsafe because it operates on raw memory.
#[inline]
unsafe fn prefetch_read(ptr: *const u8) {
    #[cfg(target_arch = "x86_64")]
    {
        std::arch::x86_64::_mm_prefetch(ptr as *const i8, std::arch::x86_64::_MM_HINT_T0);
    }
    
    #[cfg(target_arch = "aarch64")]
    {
        std::arch::aarch64::_prefetch(ptr, std::arch::aarch64::_PREFETCH_READ, std::arch::aarch64::_PREFETCH_LOCALITY3);
    }
    
    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    {
        // No prefetch instruction available
        // We'll just read the memory to bring it into cache
        std::ptr::read_volatile(ptr);
    }
}

/// Get the system page size.
#[inline]
fn page_size() -> usize {
    #[cfg(unix)]
    {
        unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize }
    }
    
    #[cfg(windows)]
    {
        use winapi::um::sysinfoapi::{GetSystemInfo, SYSTEM_INFO};
        
        unsafe {
            let mut system_info: SYSTEM_INFO = std::mem::zeroed();
            GetSystemInfo(&mut system_info);
            system_info.dwPageSize as usize
        }
    }
    
    #[cfg(not(any(unix, windows)))]
    {
        // Default to 4KB for unknown platforms
        4096
    }
}