Skip to content

Commit cb4b6d3

Browse files
committed
Add optional tracing instrumentation
1 parent 63f082a commit cb4b6d3

4 files changed

Lines changed: 71 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v0.0.3
2+
- Added optional `tracing` instrumentation to Game - use the `instrument_game` feature to enable.
3+
14
v0.0.2
25
- Added `Board::en_passant_target` to match standard meaning - for now, `Board::en_passant` should still be preferred in the hot path.
36
- Added `Board::has_checkers` as convenience function.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ documentation = "https://jordanbray.github.io/chess/chess/index.html"
1717
arrayvec = "0.5.1"
1818
nodrop = "0.1.14"
1919
failure = "0.1.6"
20+
tracing = { version = "0.1.37", optional = true }
21+
22+
[features]
23+
default = []
24+
instrument_game = ["dep:tracing"]
2025

2126
[profile.release]
2227
opt-level = 3

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ Some of the improvements made since the fork:
178178
- Build times are _drastically_ improved. rust-analyzer actually works now (thanks KarelPeeters)
179179
- Checking the `status` of the `Board` is 2-3x faster for a fully populated board (thanks AlexanderHarrison)
180180
- `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
181+
- Optional instrumentation added to `Game`, using [tracing](https://github.com/tokio-rs/tracing) - just use the `instrument_game` feature.
181182
- `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.
182183
- Performance benchmarks added
183184

src/game.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use crate::error::Error;
55
use crate::movegen::MoveGen;
66
use crate::piece::Piece;
77
use std::str::FromStr;
8+
#[cfg(any(feature = "instrument_game", feature = "instrument_all"))]
9+
use tracing::instrument;
810

911
/// Contains all actions supported within the game
1012
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug, Eq)]
@@ -48,6 +50,10 @@ impl Game {
4850
/// let game = Game::new();
4951
/// assert_eq!(game.current_position(), Board::default());
5052
/// ```
53+
#[cfg_attr(
54+
any(feature = "instrument_game", feature = "instrument_all"),
55+
instrument
56+
)]
5157
pub fn new() -> Game {
5258
Game {
5359
start_pos: Board::default(),
@@ -63,6 +69,10 @@ impl Game {
6369
/// let game = Game::new_with_board(Board::default());
6470
/// assert_eq!(game.current_position(), Board::default());
6571
/// ```
72+
#[cfg_attr(
73+
any(feature = "instrument_game", feature = "instrument_all"),
74+
instrument
75+
)]
6676
pub fn new_with_board(board: Board) -> Game {
6777
Game {
6878
start_pos: board,
@@ -82,6 +92,10 @@ impl Game {
8292
/// game.resign(Color::Black);
8393
/// assert_eq!(game.actions().len(), 2);
8494
/// ```
95+
#[cfg_attr(
96+
any(feature = "instrument_game", feature = "instrument_all"),
97+
instrument
98+
)]
8599
pub fn actions(&self) -> &Vec<Action> {
86100
&self.moves
87101
}
@@ -94,6 +108,10 @@ impl Game {
94108
/// let game = Game::new();
95109
/// assert!(game.result().is_none());
96110
/// ```
111+
#[cfg_attr(
112+
any(feature = "instrument_game", feature = "instrument_all"),
113+
instrument
114+
)]
97115
pub fn result(&self) -> Option<GameResult> {
98116
match self.current_position().status() {
99117
BoardStatus::Checkmate => {
@@ -142,6 +160,10 @@ impl Game {
142160
/// assert!(game2.is_none());
143161
/// # }
144162
/// ```
163+
#[cfg_attr(
164+
any(feature = "instrument_game", feature = "instrument_all"),
165+
instrument
166+
)]
145167
#[deprecated(since = "3.1.0", note = "Please use Game::from_str(fen)? instead.")]
146168
pub fn new_from_fen(fen: &str) -> Option<Game> {
147169
Game::from_str(fen).ok()
@@ -155,6 +177,10 @@ impl Game {
155177
/// let game = Game::new();
156178
/// assert_eq!(game.current_position(), Board::default());
157179
/// ```
180+
#[cfg_attr(
181+
any(feature = "instrument_game", feature = "instrument_all"),
182+
instrument
183+
)]
158184
pub fn current_position(&self) -> Board {
159185
let mut copy = self.start_pos;
160186

@@ -194,6 +220,10 @@ impl Game {
194220
/// game.make_move(c6b8);
195221
/// assert_eq!(game.can_declare_draw(), true); // position has shown up three times
196222
/// ```
223+
#[cfg_attr(
224+
any(feature = "instrument_game", feature = "instrument_all"),
225+
instrument
226+
)]
197227
pub fn can_declare_draw(&self) -> bool {
198228
if self.result().is_some() {
199229
return false;
@@ -278,6 +308,10 @@ impl Game {
278308
/// assert_eq!(game.can_declare_draw(), true); // position has shown up three times
279309
/// game.declare_draw();
280310
/// ```
311+
#[cfg_attr(
312+
any(feature = "instrument_game", feature = "instrument_all"),
313+
instrument
314+
)]
281315
pub fn declare_draw(&mut self) -> bool {
282316
if self.can_declare_draw() {
283317
self.moves.push(Action::DeclareDraw);
@@ -298,6 +332,10 @@ impl Game {
298332
///
299333
/// game.make_move(movegen.next().expect("At least one legal move"));
300334
/// ```
335+
#[cfg_attr(
336+
any(feature = "instrument_game", feature = "instrument_all"),
337+
instrument
338+
)]
301339
pub fn make_move(&mut self, chess_move: ChessMove) -> Option<String> {
302340
if self.result().is_some() {
303341
return None;
@@ -318,6 +356,10 @@ impl Game {
318356

319357
// Generate SAN for a given board and move.
320358
// Move must be legal.
359+
#[cfg_attr(
360+
any(feature = "instrument_game", feature = "instrument_all"),
361+
instrument
362+
)]
321363
fn generate_san(initial_board: &Board, final_board: &Board, chess_move: ChessMove) -> String {
322364
let mut san = String::new();
323365

@@ -380,6 +422,10 @@ impl Game {
380422
/// let game = Game::new();
381423
/// assert_eq!(game.side_to_move(), Color::White);
382424
/// ```
425+
#[cfg_attr(
426+
any(feature = "instrument_game", feature = "instrument_all"),
427+
instrument
428+
)]
383429
pub fn side_to_move(&self) -> Color {
384430
let move_count = self
385431
.moves
@@ -411,6 +457,10 @@ impl Game {
411457
/// let mut game = Game::new();
412458
/// game.offer_draw(Color::White);
413459
/// ```
460+
#[cfg_attr(
461+
any(feature = "instrument_game", feature = "instrument_all"),
462+
instrument
463+
)]
414464
pub fn offer_draw(&mut self, color: Color) -> bool {
415465
if self.result().is_some() {
416466
return false;
@@ -434,6 +484,10 @@ impl Game {
434484
/// game2.make_move(movegen.next().expect("At least one legal move"));
435485
/// assert_eq!(game2.accept_draw(), false);
436486
/// ```
487+
#[cfg_attr(
488+
any(feature = "instrument_game", feature = "instrument_all"),
489+
instrument
490+
)]
437491
pub fn accept_draw(&mut self) -> bool {
438492
if self.result().is_some() {
439493
return false;
@@ -465,6 +519,10 @@ impl Game {
465519
/// let mut game = Game::new();
466520
/// game.resign(Color::White);
467521
/// ```
522+
#[cfg_attr(
523+
any(feature = "instrument_game", feature = "instrument_all"),
524+
instrument
525+
)]
468526
pub fn resign(&mut self, color: Color) -> bool {
469527
if self.result().is_some() {
470528
return false;
@@ -476,7 +534,10 @@ impl Game {
476534

477535
impl FromStr for Game {
478536
type Err = Error;
479-
537+
#[cfg_attr(
538+
any(feature = "instrument_game", feature = "instrument_all"),
539+
instrument
540+
)]
480541
fn from_str(fen: &str) -> Result<Self, Self::Err> {
481542
Ok(Game::new_with_board(Board::from_str(fen)?))
482543
}

0 commit comments

Comments
 (0)