@@ -6,6 +6,73 @@ type FromToHistory<T> = [[T; 64]; 64];
66type PieceToHistory < T > = [ [ T ; 64 ] ; 13 ] ;
77type 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+
976fn 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
102169pub struct ContinuationCorrectionHistory {
103170 // [in_check][capture][piece][to][piece][to]
104- entries : Box < ContinuationHistoryType > ,
171+ entries : HugeBox < ContinuationHistoryType > ,
105172}
106173
107174impl ContinuationCorrectionHistory {
@@ -125,13 +192,13 @@ impl ContinuationCorrectionHistory {
125192
126193impl Default for ContinuationCorrectionHistory {
127194 fn default ( ) -> Self {
128- Self { entries : zeroed_box ( ) }
195+ Self { entries : HugeBox :: new_zeroed ( ) }
129196 }
130197}
131198
132199pub struct ContinuationHistory {
133200 // [in_check][capture][piece][to][piece][to]
134- entries : Box < ContinuationHistoryType > ,
201+ entries : HugeBox < ContinuationHistoryType > ,
135202}
136203
137204impl ContinuationHistory {
@@ -155,7 +222,7 @@ impl ContinuationHistory {
155222
156223impl Default for ContinuationHistory {
157224 fn default ( ) -> Self {
158- Self { entries : zeroed_box ( ) }
225+ Self { entries : HugeBox :: new_zeroed ( ) }
159226 }
160227}
161228
0 commit comments