Skip to content

Commit 2ffa845

Browse files
committed
Add cache_game_state feature - closes #2
1 parent a9dfad0 commit 2ffa845

7 files changed

Lines changed: 81 additions & 24 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
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.
4+
- Added `cache_game_state` default feature to improve performance when using `Game` by 10-20x for reasonably sized games (more for larger games).
45

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

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ failure = "0.1.6"
2020
tracing = { version = "0.1.37", optional = true }
2121

2222
[features]
23-
default = []
23+
default = ["cache_game_state"]
24+
cache_game_state = []
2425
instrument_game = ["dep:tracing"]
2526

27+
2628
[profile.release]
2729
opt-level = 3
2830
debug = false

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Check CHANGELOG.md for details - all breaking changes should be marked as *BREAK
181181
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)
184+
- Using `Game` is 10-20x faster for reasonably sized games when using the new `cache_game_state` feature (more for larger games)
184185
- `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
185186
- Optional instrumentation added to `Game`, using [tracing](https://github.com/tokio-rs/tracing) - just use the `instrument_game` feature.
186187
- `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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use candidate::{ChessMove, Game};
2+
use criterion::{black_box, criterion_group, Criterion};
3+
4+
// meant to emulate a somewhat typical usage (checking side to move, etc)
5+
fn game_playthrough_test(pgn: &str) {
6+
let mut game = Game::new();
7+
let moves = pgn.split_whitespace().filter(|s| !s.ends_with("."));
8+
9+
for m in moves {
10+
let mv = ChessMove::from_san(&game.current_position(), m).unwrap();
11+
let side_to_move = game.side_to_move();
12+
black_box(side_to_move);
13+
14+
game.make_move(mv);
15+
}
16+
}
17+
18+
fn game_benchmarks(c: &mut Criterion) {
19+
let mut group = c.benchmark_group("game");
20+
21+
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")));
22+
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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+ Kg8 112. Ne7+ Kh8 113. Ng5")));
23+
// group.bench_function("1", |b| b.iter(|| game_playthrough_test("")));
24+
}
25+
26+
criterion_group!(game, game_benchmarks);

benches/benchmarks/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod board;
2+
pub mod game;
23
pub mod perft;

benches/standard.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ mod benchmarks;
55
criterion_main! {
66
benchmarks::perft::perft,
77
benchmarks::board::board,
8+
benchmarks::game::game,
89
}

src/game.rs

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub enum GameResult {
3939
pub struct Game {
4040
start_pos: Board,
4141
moves: Vec<Action>,
42+
#[cfg(feature = "cache_game_state")]
43+
boards: Vec<Board>,
4244
}
4345

4446
impl Game {
@@ -58,6 +60,8 @@ impl Game {
5860
Game {
5961
start_pos: Board::default(),
6062
moves: vec![],
63+
#[cfg(feature = "cache_game_state")]
64+
boards: vec![Board::default()],
6165
}
6266
}
6367

@@ -77,6 +81,8 @@ impl Game {
7781
Game {
7882
start_pos: board,
7983
moves: vec![],
84+
#[cfg(feature = "cache_game_state")]
85+
boards: vec![board],
8086
}
8187
}
8288

@@ -182,15 +188,22 @@ impl Game {
182188
instrument
183189
)]
184190
pub fn current_position(&self) -> Board {
185-
let mut copy = self.start_pos;
191+
#[cfg(feature = "cache_game_state")]
192+
{
193+
self.boards.last().expect("At least one board").clone()
194+
}
195+
#[cfg(not(feature = "cache_game_state"))]
196+
{
197+
let mut copy = self.start_pos;
186198

187-
for x in self.moves.iter() {
188-
if let Action::MakeMove(m) = *x {
189-
copy = copy.make_move_new(m);
199+
for x in self.moves.iter() {
200+
if let Action::MakeMove(m) = *x {
201+
copy = copy.make_move_new(m);
202+
}
190203
}
191-
}
192204

193-
copy
205+
copy
206+
}
194207
}
195208

196209
/// Determine if a player can legally declare a draw by 3-fold repetition or 50-move rule.
@@ -346,6 +359,11 @@ impl Game {
346359
return None;
347360
}
348361

362+
#[cfg(feature = "cache_game_state")]
363+
{
364+
self.boards.push(initial_position.make_move_new(chess_move));
365+
}
366+
349367
self.moves.push(Action::MakeMove(chess_move));
350368
Some(Self::generate_san(
351369
&initial_position,
@@ -427,24 +445,31 @@ impl Game {
427445
instrument
428446
)]
429447
pub fn side_to_move(&self) -> Color {
430-
let move_count = self
431-
.moves
432-
.iter()
433-
.filter(|m| match *m {
434-
Action::MakeMove(_) => true,
435-
_ => false,
436-
})
437-
.count()
438-
+ if self.start_pos.side_to_move() == Color::White {
439-
0
440-
} else {
441-
1
442-
};
448+
#[cfg(feature = "cache_game_state")]
449+
{
450+
self.current_position().side_to_move()
451+
}
452+
#[cfg(not(feature = "cache_game_state"))]
453+
{
454+
let move_count = self
455+
.moves
456+
.iter()
457+
.filter(|m| match *m {
458+
Action::MakeMove(_) => true,
459+
_ => false,
460+
})
461+
.count()
462+
+ if self.start_pos.side_to_move() == Color::White {
463+
0
464+
} else {
465+
1
466+
};
443467

444-
if move_count % 2 == 0 {
445-
Color::White
446-
} else {
447-
Color::Black
468+
if move_count % 2 == 0 {
469+
Color::White
470+
} else {
471+
Color::Black
472+
}
448473
}
449474
}
450475

0 commit comments

Comments
 (0)