Skip to content

Commit b06bdba

Browse files
chore: revise PrincipleVariation implementation
Change the PV implementation to make it easier to avoid error propagation. I'm fine with just letting this panic. bench: 2517656
1 parent 61e3d6d commit b06bdba

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

engine/src/principle_variation.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use anyhow::Result;
1+
use core::panic;
2+
23
use arrayvec::ArrayVec;
34
use chess::{definitions::MAX_MOVES, moves::Move};
45

@@ -32,9 +33,8 @@ impl PrincipleVariation {
3233
///
3334
/// Empty Result<> on success or an error if the underlying ArrayVec
3435
/// is full before trying to push.
35-
pub(crate) fn push(&mut self, m: Move) -> Result<()> {
36-
let push_result = self.data.try_push(m);
37-
Ok(push_result?)
36+
pub(crate) fn push(&mut self, m: Move) {
37+
self.data.push(m);
3838
}
3939

4040
/// Extend the current [PrincipleVariation] with the given move and another principle variation.
@@ -46,10 +46,20 @@ impl PrincipleVariation {
4646
/// - m: The new move to add to the principle variation.
4747
/// - pv: The principle variation to append to the current variation.
4848
#[inline(always)]
49-
pub(crate) fn extend(&mut self, m: Move, pv: &Self) -> Result<()> {
49+
#[allow(clippy::panic)]
50+
pub(crate) fn extend(&mut self, m: Move, pv: &Self) {
5051
self.clear();
51-
self.push(m)?;
52-
Ok(self.data.try_extend_from_slice(pv.data.as_slice())?)
52+
self.push(m);
53+
self.data
54+
.try_extend_from_slice(pv.data.as_slice())
55+
.unwrap_or_else(|err| {
56+
panic!(
57+
"Error extending PV of size {} when adding {} and {:?}\n {err}",
58+
self.data.len(),
59+
m,
60+
pv
61+
);
62+
})
5363
}
5464

5565
/// Clear the principle variation.
@@ -98,8 +108,8 @@ mod tests {
98108
let (move1, move2) = make_moves();
99109

100110
// Push moves to the principle variation
101-
assert!(pv.push(move1).is_ok());
102-
assert!(pv.push(move2).is_ok());
111+
pv.push(move1);
112+
pv.push(move2);
103113

104114
// Check that the moves are in the principle variation
105115
assert_eq!(pv.data.len(), 2);
@@ -112,47 +122,47 @@ mod tests {
112122
let (move1, move2) = make_moves();
113123

114124
// Push the first move to the principle variation
115-
assert!(pv.push(move1).is_ok());
125+
pv.push(move1);
116126

117127
// Create a new principle variation to extend from
118128
let mut pv2 = PrincipleVariation::new();
119-
assert!(pv2.push(move2).is_ok());
129+
pv2.push(move2);
120130

121-
let pv_len_before = pv.data.len();
122131
// Extend the original principle variation with the new one
123-
assert!(pv.extend(move1, &pv2).is_ok());
132+
pv.extend(move1, &pv2);
124133

125134
// Check that the moves are in the principle variation
126-
assert_eq!(pv.data.len(), pv_len_before + 1 + pv2.data.len());
135+
assert_eq!(pv.data.len(), 1 + pv2.data.len());
127136
}
128137

129138
#[test]
130-
fn extending_or_pushing_move_past_max_size_fails() {
139+
#[should_panic]
140+
fn extending_or_pushing_move_past_max_size_panics() {
131141
let (move1, move2) = make_moves();
132142
let mut pv = PrincipleVariation::new();
133143

134144
// Fill the principle variation to its maximum size
135145
for _ in 0..MAX_MOVES {
136-
assert!(pv.push(move1).is_ok());
146+
pv.push(move1);
137147
}
138148

139149
// Attempt to push another move, which should fail
140-
assert!(pv.push(move2).is_err());
150+
pv.push(move2);
141151

142152
// reset
143153
pv = PrincipleVariation::new();
144154
let mut pv2 = PrincipleVariation::new();
145155

146156
// Fill the principle variation to its maximum size
147157
for _ in 0..MAX_MOVES - 10 {
148-
assert!(pv.push(move1).is_ok());
158+
pv.push(move1);
149159
}
150160

151161
for _ in 0..10 {
152-
assert!(pv2.push(move2).is_ok());
162+
pv2.push(move2);
153163
}
154164

155165
// Attempt to extend the principle variation with another one that would exceed the max size
156-
assert!(pv.extend(move2, &pv2).is_err());
166+
pv.extend(move2, &pv2);
157167
}
158168
}

engine/src/search.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,7 @@ impl<'a> Search<'a> {
469469
best_score = score;
470470
best_move = Some(mv);
471471
if Node::PV {
472-
pv.extend(mv, &local_pv)
473-
.expect("Could not extend PV - have you allocated enough space?");
472+
pv.extend(mv, &local_pv);
474473
}
475474

476475
alpha_use = alpha_use.max(best_score);
@@ -707,8 +706,7 @@ impl<'a> Search<'a> {
707706

708707
// extend PV if we're in a PV node
709708
if Node::PV {
710-
pv.extend(mv, &local_pv)
711-
.expect("Could not extend PV - have you allocated enough space?");
709+
pv.extend(mv, &local_pv);
712710
}
713711

714712
if score >= beta {

0 commit comments

Comments
 (0)