Skip to content

Commit 5020912

Browse files
authored
Implement huge page for Continuation and Continuation correction history (#1081)
STC SMP Elo | 2.31 +- 1.85 (95%) SPRT | 5.0+0.05s Threads=8 Hash=256MB LLR | 2.91 (-2.25, 2.89) [0.00, 4.00] Games | N: 32202 W: 8023 L: 7809 D: 16370 Penta | [32, 3587, 8667, 3765, 50] https://recklesschess.space/test/15269/ Bench: 3204302
1 parent 1d88dbc commit 5020912

1 file changed

Lines changed: 72 additions & 4 deletions

File tree

src/history.rs

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,74 @@ type FromToHistory<T> = [[T; 64]; 64];
66
type PieceToHistory<T> = [[T; 64]; 13];
77
type ContinuationHistoryType = [[[[PieceToHistory<i16>; 64]; 13]; 2]; 2];
88

9+
struct HugeBox<T> {
10+
ptr: std::ptr::NonNull<T>,
11+
}
12+
13+
unsafe impl<T: Send> Send for HugeBox<T> {}
14+
unsafe impl<T: Sync> Sync for HugeBox<T> {}
15+
16+
impl<T> HugeBox<T> {
17+
fn new_zeroed() -> Self {
18+
#[cfg(target_os = "linux")]
19+
let ptr = unsafe {
20+
use libc::{MADV_HUGEPAGE, MAP_ANONYMOUS, MAP_FAILED, MAP_PRIVATE, PROT_READ, PROT_WRITE, madvise, mmap};
21+
let size = std::mem::size_of::<T>();
22+
assert!(size > 0, "HugeBox requires a non-zero-sized type");
23+
let p = mmap(std::ptr::null_mut(), size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
24+
if p == MAP_FAILED {
25+
std::alloc::handle_alloc_error(std::alloc::Layout::new::<T>());
26+
}
27+
madvise(p, size, MADV_HUGEPAGE);
28+
std::ptr::NonNull::new_unchecked(p.cast::<T>())
29+
};
30+
31+
#[cfg(not(target_os = "linux"))]
32+
let ptr = unsafe {
33+
let layout = std::alloc::Layout::new::<T>();
34+
let p = std::alloc::alloc_zeroed(layout);
35+
if p.is_null() {
36+
std::alloc::handle_alloc_error(layout);
37+
}
38+
std::ptr::NonNull::new_unchecked(p.cast::<T>())
39+
};
40+
41+
HugeBox { ptr }
42+
}
43+
}
44+
45+
impl<T> std::ops::Deref for HugeBox<T> {
46+
type Target = T;
47+
fn deref(&self) -> &T {
48+
unsafe { self.ptr.as_ref() }
49+
}
50+
}
51+
52+
impl<T> std::ops::DerefMut for HugeBox<T> {
53+
fn deref_mut(&mut self) -> &mut T {
54+
unsafe { self.ptr.as_mut() }
55+
}
56+
}
57+
58+
impl<T> Drop for HugeBox<T> {
59+
fn drop(&mut self) {
60+
#[cfg(target_os = "linux")]
61+
{
62+
let size = std::mem::size_of::<T>();
63+
assert!(size > 0, "HugeBox requires a non-zero-sized type");
64+
unsafe {
65+
libc::munmap(self.ptr.as_ptr().cast(), size);
66+
}
67+
}
68+
69+
#[cfg(not(target_os = "linux"))]
70+
unsafe {
71+
let layout = std::alloc::Layout::new::<T>();
72+
std::alloc::dealloc(self.ptr.as_ptr().cast(), layout);
73+
}
74+
}
75+
}
76+
977
fn apply_bonus<const MAX: i32>(entry: &mut i16, bonus: i32) {
1078
let bonus = bonus.clamp(-MAX, MAX);
1179
*entry += (bonus - bonus.abs() * (*entry) as i32 / MAX) as i16;
@@ -101,7 +169,7 @@ impl Default for CorrectionHistory {
101169

102170
pub struct ContinuationCorrectionHistory {
103171
// [in_check][capture][piece][to][piece][to]
104-
entries: Box<ContinuationHistoryType>,
172+
entries: HugeBox<ContinuationHistoryType>,
105173
}
106174

107175
impl ContinuationCorrectionHistory {
@@ -125,13 +193,13 @@ impl ContinuationCorrectionHistory {
125193

126194
impl Default for ContinuationCorrectionHistory {
127195
fn default() -> Self {
128-
Self { entries: zeroed_box() }
196+
Self { entries: HugeBox::new_zeroed() }
129197
}
130198
}
131199

132200
pub struct ContinuationHistory {
133201
// [in_check][capture][piece][to][piece][to]
134-
entries: Box<ContinuationHistoryType>,
202+
entries: HugeBox<ContinuationHistoryType>,
135203
}
136204

137205
impl ContinuationHistory {
@@ -155,7 +223,7 @@ impl ContinuationHistory {
155223

156224
impl Default for ContinuationHistory {
157225
fn default() -> Self {
158-
Self { entries: zeroed_box() }
226+
Self { entries: HugeBox::new_zeroed() }
159227
}
160228
}
161229

0 commit comments

Comments
 (0)