Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various micro-optimizations #536

Merged
merged 10 commits into from
Oct 3, 2023
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,3 @@ bench = false
[[bench]]
name = "ttd"
harness = false

[[bench]]
name = "ttm"
harness = false
2 changes: 1 addition & 1 deletion benches/ttd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn ttd(c: &mut Criterion, fens: &[&str]) {
c.benchmark_group("benches").bench_function("ttd", |b| {
b.iter_batched_ref(
|| (Engine::with_options(options), positions.next().unwrap()),
|(s, pos)| s.search::<1>(pos, Depth::new(10).into()),
|(s, pos)| s.search(pos, Depth::new(12).into()),
BatchSize::SmallInput,
);
});
Expand Down
1,267 changes: 0 additions & 1,267 deletions benches/ttm.rs

This file was deleted.

40 changes: 25 additions & 15 deletions bin/uci.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::io::Io;
use anyhow::{Context, Error as Anyhow};
use lib::chess::{Color, Move, Position};
use lib::chess::{Color, Position};
use lib::nnue::Evaluator;
use lib::search::{Depth, Engine, Limits, Options, Pv};
use lib::search::{Depth, Engine, Limits, Options};
use rayon::max_num_threads;
use std::io::{stdin, stdout, Stdin, Stdout};
use std::{num::NonZeroUsize, time::Duration};
Expand All @@ -14,7 +14,7 @@ pub struct Uci {
engine: Engine,
options: Options,
position: Position,
moves: Vec<Move>,
moves: Vec<UciMove>,
}

impl Default for Uci {
Expand Down Expand Up @@ -54,17 +54,31 @@ impl Uci {
Ok(())
}

fn play(&mut self, uci: &UciMove) -> Result<(), Anyhow> {
let m = self
.position
.moves()
.find(|m| UciMove::from(*m) == *uci)
.context("invalid move")?;

self.position.play(m)?;
self.moves.push(*uci);
Ok(())
}

fn go(&mut self, limits: Limits) -> Result<(), Anyhow> {
let pv: Pv<1> = self.engine.search(&self.position, limits);
let best = *pv.first().expect("expected some legal move");
let pv = self.engine.search(&self.position, limits);
let best = *pv.first().context("the engine failed to find a move")?;

let score = match pv.score().mate() {
Some(p) if p > 0 => UciInfoAttribute::from_mate((p + 1).get() / 2),
Some(p) => UciInfoAttribute::from_mate((p - 1).get() / 2),
None => UciInfoAttribute::from_centipawns(pv.score().get().into()),
};

self.io.send(UciMessage::Info(vec![score]))?;
let pv = UciInfoAttribute::Pv(pv.into_iter().map(UciMove::from).collect());

self.io.send(UciMessage::Info(vec![score, pv]))?;
self.io.send(UciMessage::best_move(best.into()))?;

Ok(())
Expand Down Expand Up @@ -142,21 +156,17 @@ impl Uci {
startpos: true,
fen: None,
moves,
} => match Vec::from_iter(moves.into_iter().map(Move::from)).as_slice() {
} => match moves.as_slice() {
[history @ .., m, n] if history == self.moves => {
self.position.play(*m)?;
self.moves.push(*m);

self.position.play(*n)?;
self.moves.push(*n);
self.play(m)?;
self.play(n)?;
}

ms => {
self.position = Position::default();
self.moves.clear();
for &m in ms {
self.position.play(m)?;
self.moves.push(m);
for m in ms {
self.play(m)?;
}
}
},
Expand Down
2 changes: 0 additions & 2 deletions lib/chess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod r#move;
mod outcome;
mod piece;
mod position;
mod promotion;
mod rank;
mod role;
mod square;
Expand All @@ -16,7 +15,6 @@ pub use file::*;
pub use outcome::*;
pub use piece::*;
pub use position::*;
pub use promotion::*;
pub use r#move::*;
pub use rank::*;
pub use role::*;
Expand Down
1 change: 1 addition & 0 deletions lib/chess/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::ops::Not;
/// Denotes the color of a chess [`Piece`][`crate::Piece`].
#[derive(Debug, Display, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[repr(u8)]
pub enum Color {
#[display(fmt = "white")]
White,
Expand Down
1 change: 1 addition & 0 deletions lib/chess/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use proptest::sample::select;
#[derive(DebugCustom, Display, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[debug(fmt = "{self}")]
#[repr(transparent)]
pub struct File(#[cfg_attr(test, strategy(select(sm::File::ALL.as_ref())))] sm::File);

impl File {
Expand Down
Loading