Skip to content

Commit

Permalink
instrument with tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed May 19, 2024
1 parent d6718cb commit f2b419d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ byteorder = "1.2"
positioned-io = "0.3"
rustc-hash = "1.0"
once_cell = "1.12"
tracing = "0.1"

[dev-dependencies]
csv = "1"
Expand Down
85 changes: 50 additions & 35 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bitflags::bitflags;
use byteorder::{ByteOrder as _, ReadBytesExt as _, BE, LE};
use positioned_io::{RandomAccessFile, ReadAt, ReadBytesAtExt as _};
use shakmaty::{Bitboard, Color, File, Piece, Position, Rank, Role, Square};
use tracing::{trace, trace_span};

use crate::{
errors::{ProbeError, ProbeResult},
Expand Down Expand Up @@ -974,6 +975,8 @@ impl<T: TableTag, S: Position + Syzygy, F: ReadAt> Table<T, S, F> {
}
}

trace!(%material, "table opened");

// Result.
Ok(Table {
is_wdl: PhantomData,
Expand All @@ -988,6 +991,8 @@ impl<T: TableTag, S: Position + Syzygy, F: ReadAt> Table<T, S, F> {
/// Retrieves the value stored for `idx` by decompressing Huffman coded
/// symbols stored in the corresponding block of the table.
fn decompress_pairs(&self, d: &PairsData, idx: u64) -> ProbeResult<u16> {
trace!(idx, "decompress pairs");

// Special case: The table stores only a single value.
if d.flags.contains(Flag::SINGLE_VALUE) {
return Ok(u16::from(d.min_symlen));
Expand All @@ -1005,6 +1010,7 @@ impl<T: TableTag, S: Position + Syzygy, F: ReadAt> Table<T, S, F> {

let mut lit_idx = idx as i64 % i64::from(d.span) - i64::from(d.span) / 2;
lit_idx += offset;
trace!("sparse index read");

// Now move forwards/backwards to find the correct block.
while lit_idx < 0 {
Expand All @@ -1026,6 +1032,7 @@ impl<T: TableTag, S: Position + Syzygy, F: ReadAt> Table<T, S, F> {
break;
}
}
trace!("block located");

// Read block (and 4 bytes to allow a final symbol refill) into memory.
let mut block_buffer = [0; MAX_BLOCK_SIZE + 4];
Expand Down Expand Up @@ -1067,6 +1074,7 @@ impl<T: TableTag, S: Position + Syzygy, F: ReadAt> Table<T, S, F> {
buf |= u64::from(cursor.read_u32::<BE>()?) << (64 - buf_size);
}
}
trace!(sym, "symbol found");

// Decompress Huffman symbol.
while *u!(d.symlen.get(usize::from(sym))) != 0 {
Expand All @@ -1079,12 +1087,15 @@ impl<T: TableTag, S: Position + Syzygy, F: ReadAt> Table<T, S, F> {
sym = right;
}
}
trace!(sym, "symbol decompressed");

let w = d.btree + 3 * u64::from(sym);
match T::METRIC {
Metric::Wdl => Ok(u16::from(self.raf.read_u8_at(w)?)),
Metric::Dtz => Ok(self.raf.read_u16_at::<LE>(w)? & 0xfff),
}
let value = match T::METRIC {
Metric::Wdl => u16::from(self.raf.read_u8_at(w)?),
Metric::Dtz => self.raf.read_u16_at::<LE>(w)? & 0xfff,
};
trace!(value, "value loaded");
Ok(value)
}

/// Given a position, determine the unique (modulo symmetries) index into
Expand Down Expand Up @@ -1362,46 +1373,50 @@ impl<T: TableTag, S: Position + Syzygy, F: ReadAt> Table<T, S, F> {
}

pub fn probe_wdl(&self, pos: &S) -> ProbeResult<Wdl> {
assert_eq!(T::METRIC, Metric::Wdl);

let (side, idx) = self.encode(pos)?.expect("wdl tables are two sided");
let decompressed = self.decompress_pairs(side, idx)?;

Ok(match decompressed {
0 => Wdl::Loss,
1 => Wdl::BlessedLoss,
2 => Wdl::Draw,
3 => Wdl::CursedWin,
4 => Wdl::Win,
_ => throw!(),
trace_span!("wdl table").in_scope(|| {
assert_eq!(T::METRIC, Metric::Wdl);

let (side, idx) = self.encode(pos)?.expect("wdl tables are two sided");
let decompressed = self.decompress_pairs(side, idx)?;

Ok(match decompressed {
0 => Wdl::Loss,
1 => Wdl::BlessedLoss,
2 => Wdl::Draw,
3 => Wdl::CursedWin,
4 => Wdl::Win,
_ => throw!(),
})
})
}

pub fn probe_dtz(&self, pos: &S, wdl: DecisiveWdl) -> ProbeResult<Option<MaybeRounded<u32>>> {
assert_eq!(T::METRIC, Metric::Dtz);
trace_span!("dtz table").in_scope(|| {
assert_eq!(T::METRIC, Metric::Dtz);

let Some((side, idx)) = self.encode(pos)? else {
return Ok(None); // check other side
};
let Some((side, idx)) = self.encode(pos)? else {
return Ok(None); // check other side
};

let res = self.decompress_pairs(side, idx)?;
let res = self.decompress_pairs(side, idx)?;

let res = u32::from(match side.dtz_map {
None => res,
Some(ref map) => map.read(&self.raf, wdl, res)?,
});
let res = u32::from(match side.dtz_map {
None => res,
Some(ref map) => map.read(&self.raf, wdl, res)?,
});

let stores_plies = match wdl {
DecisiveWdl::Win => side.flags.contains(Flag::WIN_PLIES),
DecisiveWdl::Loss => side.flags.contains(Flag::LOSS_PLIES),
DecisiveWdl::CursedWin | DecisiveWdl::BlessedLoss => false,
};
let stores_plies = match wdl {
DecisiveWdl::Win => side.flags.contains(Flag::WIN_PLIES),
DecisiveWdl::Loss => side.flags.contains(Flag::LOSS_PLIES),
DecisiveWdl::CursedWin | DecisiveWdl::BlessedLoss => false,
};

Ok(Some(if stores_plies {
MaybeRounded::Precise(res)
} else {
MaybeRounded::Rounded(2 * res)
}))
Ok(Some(if stores_plies {
MaybeRounded::Precise(res)
} else {
MaybeRounded::Rounded(2 * res)
}))
})
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/tablebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use once_cell::sync::OnceCell;
use positioned_io::RandomAccessFile;
use rustc_hash::FxHashMap;
use shakmaty::{Move, Position, Role};
use tracing::trace_span;

use crate::{
errors::{ProbeResultExt as _, SyzygyError, SyzygyResult},
Expand Down Expand Up @@ -225,9 +226,11 @@ impl<S: Position + Clone + Syzygy> Tablebase<S> {
///
/// See [`SyzygyError`] for possible error conditions.
pub fn probe_wdl(&self, pos: &S) -> SyzygyResult<AmbiguousWdl> {
self.probe(pos)
.and_then(|entry| entry.dtz())
.map(|dtz| AmbiguousWdl::from_dtz_and_halfmoves(dtz, pos.halfmoves()))
trace_span!("probe wdl", pieces = pos.board().occupied().count()).in_scope(|| {
self.probe(pos)
.and_then(|entry| entry.dtz())
.map(|dtz| AmbiguousWdl::from_dtz_and_halfmoves(dtz, pos.halfmoves()))
})
}

/// Probe tables for the [`Dtz`] value of a position.
Expand All @@ -238,7 +241,8 @@ impl<S: Position + Clone + Syzygy> Tablebase<S> {
///
/// See [`SyzygyError`] for possible error conditions.
pub fn probe_dtz(&self, pos: &S) -> SyzygyResult<MaybeRounded<Dtz>> {
self.probe(pos).and_then(|entry| entry.dtz())
trace_span!("probe dtz", pieces = pos.board().occupied().count())
.in_scope(|| self.probe(pos).and_then(|entry| entry.dtz()))
}

/// Get the recommended tablebase move.
Expand Down

0 comments on commit f2b419d

Please sign in to comment.