Skip to content

Commit b2f44d8

Browse files
committed
Implement huge page for Continuation and Continuation correction history
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 b2f44d8

1 file changed

Lines changed: 71 additions & 4 deletions

File tree

src/history.rs

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,73 @@ 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+
let size = std::mem::size_of::<T>();
19+
assert!(size > 0, "HugeBox requires a non-zero-sized type");
20+
21+
#[cfg(target_os = "linux")]
22+
let ptr = unsafe {
23+
use libc::{MADV_HUGEPAGE, MAP_ANONYMOUS, MAP_FAILED, MAP_PRIVATE, PROT_READ, PROT_WRITE, madvise, mmap};
24+
let p = mmap(std::ptr::null_mut(), size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
25+
if p == MAP_FAILED {
26+
std::alloc::handle_alloc_error(std::alloc::Layout::new::<T>());
27+
}
28+
madvise(p, size, MADV_HUGEPAGE);
29+
std::ptr::NonNull::new_unchecked(p.cast::<T>())
30+
};
31+
32+
#[cfg(not(target_os = "linux"))]
33+
let ptr = unsafe {
34+
let layout = std::alloc::Layout::new::<T>();
35+
let p = std::alloc::alloc_zeroed(layout);
36+
if p.is_null() {
37+
std::alloc::handle_alloc_error(layout);
38+
}
39+
std::ptr::NonNull::new_unchecked(p.cast::<T>())
40+
};
41+
42+
HugeBox { ptr }
43+
}
44+
}
45+
46+
impl<T> std::ops::Deref for HugeBox<T> {
47+
type Target = T;
48+
fn deref(&self) -> &T {
49+
unsafe { self.ptr.as_ref() }
50+
}
51+
}
52+
53+
impl<T> std::ops::DerefMut for HugeBox<T> {
54+
fn deref_mut(&mut self) -> &mut T {
55+
unsafe { self.ptr.as_mut() }
56+
}
57+
}
58+
59+
impl<T> Drop for HugeBox<T> {
60+
fn drop(&mut self) {
61+
let size = std::mem::size_of::<T>();
62+
63+
#[cfg(target_os = "linux")]
64+
unsafe {
65+
libc::munmap(self.ptr.as_ptr().cast(), size);
66+
}
67+
68+
#[cfg(not(target_os = "linux"))]
69+
unsafe {
70+
let layout = std::alloc::Layout::new::<T>();
71+
std::alloc::dealloc(self.ptr.as_ptr().cast(), layout);
72+
}
73+
}
74+
}
75+
976
fn apply_bonus<const MAX: i32>(entry: &mut i16, bonus: i32) {
1077
let bonus = bonus.clamp(-MAX, MAX);
1178
*entry += (bonus - bonus.abs() * (*entry) as i32 / MAX) as i16;
@@ -101,7 +168,7 @@ impl Default for CorrectionHistory {
101168

102169
pub struct ContinuationCorrectionHistory {
103170
// [in_check][capture][piece][to][piece][to]
104-
entries: Box<ContinuationHistoryType>,
171+
entries: HugeBox<ContinuationHistoryType>,
105172
}
106173

107174
impl ContinuationCorrectionHistory {
@@ -125,13 +192,13 @@ impl ContinuationCorrectionHistory {
125192

126193
impl Default for ContinuationCorrectionHistory {
127194
fn default() -> Self {
128-
Self { entries: zeroed_box() }
195+
Self { entries: HugeBox::new_zeroed() }
129196
}
130197
}
131198

132199
pub struct ContinuationHistory {
133200
// [in_check][capture][piece][to][piece][to]
134-
entries: Box<ContinuationHistoryType>,
201+
entries: HugeBox<ContinuationHistoryType>,
135202
}
136203

137204
impl ContinuationHistory {
@@ -155,7 +222,7 @@ impl ContinuationHistory {
155222

156223
impl Default for ContinuationHistory {
157224
fn default() -> Self {
158-
Self { entries: zeroed_box() }
225+
Self { entries: HugeBox::new_zeroed() }
159226
}
160227
}
161228

0 commit comments

Comments
 (0)