Skip to content

Commit 28c2bed

Browse files
committed
Improve unsanitized legality checking - closes #1
1 parent 2ffa845 commit 28c2bed

6 files changed

Lines changed: 87 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ v0.0.3
22
- Added optional `tracing` instrumentation to Game - use the `instrument_game` feature to enable.
33
- Additional 3x increase in build speed, by only rebuilding magic bitboards when necessary.
44
- Added `cache_game_state` default feature to improve performance when using `Game` by 10-20x for reasonably sized games (more for larger games).
5+
- Legality checking for unsanitized inputs is 4-5x faster.
56

67
v0.0.2
78
- Added `Board::en_passant_target` to match standard meaning - for now, `Board::en_passant` should still be preferred in the hot path.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ Some of the improvements made since the fork:
182182
- Build times are _drastically_ improved. rust-analyzer actually works now (thanks KarelPeeters)
183183
- Checking the `status` of the `Board` is 2-3x faster for a fully populated board (thanks AlexanderHarrison)
184184
- Using `Game` is 10-20x faster for reasonably sized games when using the new `cache_game_state` feature (more for larger games)
185+
- Legality checking of unsanitized moves is 4-5x faster
185186
- `Game::make_move` now returns `Option<String>` with the SAN representation of the move. `Board::make_move` still returns a bool to avoid overhead in the hot path
186187
- Optional instrumentation added to `Game`, using [tracing](https://github.com/tokio-rs/tracing) - just use the `instrument_game` feature.
187188
- `Board::en_passant_target` and `Board::has_checkers` added as convenience methods. `Board::en_passant` is slightly faster than `Board::en_passant_target` for now.

benches/benchmarks/game.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use candidate::{ChessMove, Game};
2-
use criterion::{black_box, criterion_group, Criterion};
1+
use candidate::{ChessMove, Game, MoveGen};
2+
use criterion::{black_box, criterion_group, BenchmarkId, Criterion};
33

44
// meant to emulate a somewhat typical usage (checking side to move, etc)
5-
fn game_playthrough_test(pgn: &str) {
5+
// note: checking the side didn't seem to make a difference in the benchmarks
6+
fn game_playthrough_test(pgn: &str) -> Game {
67
let mut game = Game::new();
78
let moves = pgn.split_whitespace().filter(|s| !s.ends_with("."));
89

@@ -13,14 +14,45 @@ fn game_playthrough_test(pgn: &str) {
1314

1415
game.make_move(mv);
1516
}
17+
game
18+
}
19+
#[cfg(feature = "cache_game_state")]
20+
fn legality_benchmarks(c: &mut Criterion) {
21+
let mut group = c.benchmark_group("legality");
22+
let pgn = "1. d4 Nf6 2. c4 g6 3. Nc3 Bg7 4. e4 d6 5. Nf3 O-O 6. Be2 e5 7. O-O Nc6 8. d5 Ne7 9. Nd2 a5 10. Rb1 Nd7 11. a3 f5 12. b4 Kh8 13. f3 Ng8 14. Qc2 Ngf6 15. Nb5 axb4 16. axb4 Nh5 17. g3 Ndf6 18. c5 Bd7 19. Rb3 Nxg3 20. hxg3 Nh5 21. f4 exf4 22. c6 bxc6 23. dxc6 Nxg3 24. Rxg3 fxg3 25. cxd7 g2 26. Rf3 Qxd7 27. Bb2 fxe4 28. Rxf8+ Rxf8 29. Bxg7+ Qxg7 30. Qxe4 Qf6 31. Nf3 Qf4 32. Qe7 Rf7 33. Qe6 Rf6 34. Qe8+ Rf8 35. Qe7 Rf7 36. Qe6 Rf6 37. Qb3 g5 38. Nxc7 g4 39. Nd5 Qc1+ 40. Qd1 Qxd1+ 41. Bxd1 Rf5 42. Ne3 Rf4 43. Ne1 Rxb4 44. Bxg4 h5 45. Bf3 d5 46. N3xg2 h4 47. Nd3 Ra4 48. Ngf4 Kg7 49. Kg2 Kf6 50. Bxd5 Ra5 51. Bc6 Ra6 52. Bb7 Ra3 53. Be4 Ra4 54. Bd5 Ra5 55. Bc6 Ra6 56. Bf3 Kg5 57. Bb7 Ra1 58. Bc8 Ra4 59. Kf3 Rc4 60. Bd7 Kf6 61. Kg4 Rd4 62. Bc6 Rd8 63. Kxh4 Rg8 64. Be4 Rg1 65. Nh5+ Ke6 66. Ng3 Kf6 67. Kg4 Ra1 68. Bd5 Ra5 69. Bf3 Ra1 70. Kf4 Ke6 71. Nc5+ Kd6 72. Nge4+ Ke7 73. Ke5 Rf1 74. Bg4 Rg1 75. Be6 Re1 76. Bc8 Rc1 77. Kd4 Rd1+ 78. Nd3 Kf7 79. Ke3 Ra1 80. Kf4 Ke7 81. Nb4 Rc1 82. Nd5+ Kf7 83. Bd7 Rf1+ 84. Ke5 Ra1 85. Ng5+ Kg6 86. Nf3 Kg7 87. Bg4 Kg6 88. Nf4+ Kg7 89. Nd4 Re1+ 90. Kf5 Rc1 91. Be2 Re1 92. Bh5 Ra1 93. Nfe6+ Kh6 94. Be8 Ra8 95. Bc6 Ra1 96. Kf6 Kh7 97. Ng5+ Kh8 98. Nde6 Ra6 99. Be8 Ra8 100. Bh5 Ra1 101. Bg6 Rf1+ 102. Ke7 Ra1 103. Nf7+ Kg8 104. Nh6+ Kh8 105. Nf5 Ra7+ 106. Kf6 Ra1 107. Ne3 Re1 108. Nd5 Rg1 109. Bf5 Rf1 110. Ndf4 Ra1 111. Ng6+ Kg8 112. Ne7+ Kh8 113. Ng5";
23+
let game = game_playthrough_test(pgn);
24+
let san_moves: Vec<&str> = pgn
25+
.split_whitespace()
26+
.filter(|s| !s.ends_with("."))
27+
.collect();
28+
let mut moves: Vec<ChessMove> = vec![];
29+
for (index, san) in san_moves.iter().enumerate() {
30+
moves.push(ChessMove::from_san(&game.get_boards()[index], san_moves[index]).unwrap());
31+
}
32+
33+
group.bench_function(BenchmarkId::new("legality_check", "current"), |b| {
34+
b.iter(|| {
35+
for (index, m) in moves.iter().enumerate() {
36+
let board = game.get_boards()[index];
37+
assert_eq!(board.legal(*m), true);
38+
}
39+
})
40+
});
41+
group.bench_function(BenchmarkId::new("legality_check", "old_gen_all"), |b| {
42+
b.iter(|| {
43+
for (index, m) in moves.iter().enumerate() {
44+
let board = game.get_boards()[index];
45+
assert_eq!(MoveGen::new_legal(&board).any(|mv| mv == *m), true);
46+
}
47+
})
48+
});
1649
}
1750

1851
fn game_benchmarks(c: &mut Criterion) {
1952
let mut group = c.benchmark_group("game");
2053

2154
group.bench_function("1", |b| b.iter(|| game_playthrough_test("1. Nc3 d5 2. e3 Nc6 3. Nf3 Nf6 4. Bb5 a6 5. Bxc6+ bxc6 6. Ne5 Qd6 7. d4 Nd7 8. f4 Nxe5 9. dxe5 Qg6 10. O-O Bf5 11. e4 Bxe4 12. Nxe4 Qxe4 13. Re1 Qb4 14. e6 f6 15. Be3 g6 16. Qd4 Qxd4 17. Bxd4 Bh6 18. g3 g5 19. f5 g4 20. Rad1 Rg8 21. b3 Rb8 22. c4 dxc4 23. bxc4 Rd8 24. Kg2 Rc8 25. Bc5 Rg5 26. Rd7 Bf8 27. Rf1 a5 28. Kg1 a4 29. Bb4 Rh5 30. Rf4 Rg5 31. Rf1 Rh5 32. Rf4 Rg5 33. Ba5")));
2255
group.bench_function("2", |b| b.iter(|| game_playthrough_test("1. d4 Nf6 2. c4 g6 3. Nc3 Bg7 4. e4 d6 5. Nf3 O-O 6. Be2 e5 7. O-O Nc6 8. d5 Ne7 9. Nd2 a5 10. Rb1 Nd7 11. a3 f5 12. b4 Kh8 13. f3 Ng8 14. Qc2 Ngf6 15. Nb5 axb4 16. axb4 Nh5 17. g3 Ndf6 18. c5 Bd7 19. Rb3 Nxg3 20. hxg3 Nh5 21. f4 exf4 22. c6 bxc6 23. dxc6 Nxg3 24. Rxg3 fxg3 25. cxd7 g2 26. Rf3 Qxd7 27. Bb2 fxe4 28. Rxf8+ Rxf8 29. Bxg7+ Qxg7 30. Qxe4 Qf6 31. Nf3 Qf4 32. Qe7 Rf7 33. Qe6 Rf6 34. Qe8+ Rf8 35. Qe7 Rf7 36. Qe6 Rf6 37. Qb3 g5 38. Nxc7 g4 39. Nd5 Qc1+ 40. Qd1 Qxd1+ 41. Bxd1 Rf5 42. Ne3 Rf4 43. Ne1 Rxb4 44. Bxg4 h5 45. Bf3 d5 46. N3xg2 h4 47. Nd3 Ra4 48. Ngf4 Kg7 49. Kg2 Kf6 50. Bxd5 Ra5 51. Bc6 Ra6 52. Bb7 Ra3 53. Be4 Ra4 54. Bd5 Ra5 55. Bc6 Ra6 56. Bf3 Kg5 57. Bb7 Ra1 58. Bc8 Ra4 59. Kf3 Rc4 60. Bd7 Kf6 61. Kg4 Rd4 62. Bc6 Rd8 63. Kxh4 Rg8 64. Be4 Rg1 65. Nh5+ Ke6 66. Ng3 Kf6 67. Kg4 Ra1 68. Bd5 Ra5 69. Bf3 Ra1 70. Kf4 Ke6 71. Nc5+ Kd6 72. Nge4+ Ke7 73. Ke5 Rf1 74. Bg4 Rg1 75. Be6 Re1 76. Bc8 Rc1 77. Kd4 Rd1+ 78. Nd3 Kf7 79. Ke3 Ra1 80. Kf4 Ke7 81. Nb4 Rc1 82. Nd5+ Kf7 83. Bd7 Rf1+ 84. Ke5 Ra1 85. Ng5+ Kg6 86. Nf3 Kg7 87. Bg4 Kg6 88. Nf4+ Kg7 89. Nd4 Re1+ 90. Kf5 Rc1 91. Be2 Re1 92. Bh5 Ra1 93. Nfe6+ Kh6 94. Be8 Ra8 95. Bc6 Ra1 96. Kf6 Kh7 97. Ng5+ Kh8 98. Nde6 Ra6 99. Be8 Ra8 100. Bh5 Ra1 101. Bg6 Rf1+ 102. Ke7 Ra1 103. Nf7+ Kg8 104. Nh6+ Kh8 105. Nf5 Ra7+ 106. Kf6 Ra1 107. Ne3 Re1 108. Nd5 Rg1 109. Bf5 Rf1 110. Ndf4 Ra1 111. Ng6+ Kg8 112. Ne7+ Kh8 113. Ng5")));
23-
// group.bench_function("1", |b| b.iter(|| game_playthrough_test("")));
2456
}
2557

2658
criterion_group!(game, game_benchmarks);

src/board.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ impl Board {
851851
/// ```
852852
#[inline]
853853
pub fn legal(&self, m: ChessMove) -> bool {
854-
MoveGen::new_legal(&self).any(|x| x == m)
854+
MoveGen::legal_unsanitized(self, m)
855855
}
856856

857857
/// Make a chess move onto a new board.

src/game.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ impl Game {
6565
}
6666
}
6767

68+
pub fn get_boards(&self) -> &Vec<Board> {
69+
&self.boards
70+
}
71+
6872
/// Create a new `Game` with a specific starting position.
6973
///
7074
/// ```

src/movegen/movegen.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,50 @@ impl MoveGen {
218218
}
219219
}
220220

221+
pub fn legal_unsanitized(board: &Board, chess_move: ChessMove) -> bool {
222+
let piece = board.piece_on(chess_move.get_source());
223+
if piece.is_none() {
224+
return false;
225+
}
226+
let piece = piece.unwrap();
227+
let checkers = *board.checkers();
228+
let mask = !board.color_combined(board.side_to_move())
229+
& BitBoard::from_square(chess_move.get_dest());
230+
let mut movelist = NoDrop::new(ArrayVec::<[SquareAndBitBoard; 18]>::new());
231+
232+
if checkers == EMPTY {
233+
match piece {
234+
Piece::Pawn => PawnType::legals::<NotInCheckType>(&mut movelist, &board, mask),
235+
Piece::Knight => KnightType::legals::<NotInCheckType>(&mut movelist, &board, mask),
236+
Piece::Bishop => BishopType::legals::<NotInCheckType>(&mut movelist, &board, mask),
237+
Piece::Rook => RookType::legals::<NotInCheckType>(&mut movelist, &board, mask),
238+
Piece::Queen => QueenType::legals::<NotInCheckType>(&mut movelist, &board, mask),
239+
Piece::King => KingType::legals::<NotInCheckType>(&mut movelist, &board, mask),
240+
}
241+
} else if checkers.popcnt() == 1 {
242+
match piece {
243+
Piece::Pawn => PawnType::legals::<InCheckType>(&mut movelist, &board, mask),
244+
Piece::Knight => KnightType::legals::<InCheckType>(&mut movelist, &board, mask),
245+
Piece::Bishop => BishopType::legals::<InCheckType>(&mut movelist, &board, mask),
246+
Piece::Rook => RookType::legals::<InCheckType>(&mut movelist, &board, mask),
247+
Piece::Queen => QueenType::legals::<InCheckType>(&mut movelist, &board, mask),
248+
Piece::King => KingType::legals::<InCheckType>(&mut movelist, &board, mask),
249+
}
250+
} else {
251+
if let Piece::King = piece {
252+
KingType::legals::<InCheckType>(&mut movelist, &board, mask);
253+
}
254+
};
255+
256+
MoveGen {
257+
moves: movelist,
258+
promotion_index: 0,
259+
iterator_mask: mask,
260+
index: 0,
261+
}
262+
.any(|x| x == chess_move)
263+
}
264+
221265
/// This function checks the legality *only for moves generated by `MoveGen`*.
222266
///
223267
/// Calling this function for moves not generated by `MoveGen` will result in possibly

0 commit comments

Comments
 (0)