Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 72 additions & 4 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,74 @@ type FromToHistory<T> = [[T; 64]; 64];
type PieceToHistory<T> = [[T; 64]; 13];
type ContinuationHistoryType = [[[[PieceToHistory<i16>; 64]; 13]; 2]; 2];

struct HugeBox<T> {
ptr: std::ptr::NonNull<T>,
}

unsafe impl<T: Send> Send for HugeBox<T> {}
unsafe impl<T: Sync> Sync for HugeBox<T> {}

impl<T> HugeBox<T> {
fn new_zeroed() -> Self {
#[cfg(target_os = "linux")]
let ptr = unsafe {
use libc::{MADV_HUGEPAGE, MAP_ANONYMOUS, MAP_FAILED, MAP_PRIVATE, PROT_READ, PROT_WRITE, madvise, mmap};
let size = std::mem::size_of::<T>();
assert!(size > 0, "HugeBox requires a non-zero-sized type");
let p = mmap(std::ptr::null_mut(), size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if p == MAP_FAILED {
std::alloc::handle_alloc_error(std::alloc::Layout::new::<T>());
}
madvise(p, size, MADV_HUGEPAGE);
std::ptr::NonNull::new_unchecked(p.cast::<T>())
};

#[cfg(not(target_os = "linux"))]
let ptr = unsafe {
let layout = std::alloc::Layout::new::<T>();
let p = std::alloc::alloc_zeroed(layout);
if p.is_null() {
std::alloc::handle_alloc_error(layout);
}
std::ptr::NonNull::new_unchecked(p.cast::<T>())
};

HugeBox { ptr }
}
}

impl<T> std::ops::Deref for HugeBox<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { self.ptr.as_ref() }
}
}

impl<T> std::ops::DerefMut for HugeBox<T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { self.ptr.as_mut() }
}
}

impl<T> Drop for HugeBox<T> {
fn drop(&mut self) {
#[cfg(target_os = "linux")]
{
let size = std::mem::size_of::<T>();
assert!(size > 0, "HugeBox requires a non-zero-sized type");
unsafe {
libc::munmap(self.ptr.as_ptr().cast(), size);
}
}

#[cfg(not(target_os = "linux"))]
unsafe {
let layout = std::alloc::Layout::new::<T>();
std::alloc::dealloc(self.ptr.as_ptr().cast(), layout);
}
}
}

fn apply_bonus<const MAX: i32>(entry: &mut i16, bonus: i32) {
let bonus = bonus.clamp(-MAX, MAX);
*entry += (bonus - bonus.abs() * (*entry) as i32 / MAX) as i16;
Expand Down Expand Up @@ -101,7 +169,7 @@ impl Default for CorrectionHistory {

pub struct ContinuationCorrectionHistory {
// [in_check][capture][piece][to][piece][to]
entries: Box<ContinuationHistoryType>,
entries: HugeBox<ContinuationHistoryType>,
}

impl ContinuationCorrectionHistory {
Expand All @@ -125,13 +193,13 @@ impl ContinuationCorrectionHistory {

impl Default for ContinuationCorrectionHistory {
fn default() -> Self {
Self { entries: zeroed_box() }
Self { entries: HugeBox::new_zeroed() }
}
}

pub struct ContinuationHistory {
// [in_check][capture][piece][to][piece][to]
entries: Box<ContinuationHistoryType>,
entries: HugeBox<ContinuationHistoryType>,
}

impl ContinuationHistory {
Expand All @@ -155,7 +223,7 @@ impl ContinuationHistory {

impl Default for ContinuationHistory {
fn default() -> Self {
Self { entries: zeroed_box() }
Self { entries: HugeBox::new_zeroed() }
}
}

Expand Down