~bfiedler/rschess

47cd447741b5fc102a21ed1c0b73b7c377b1c431 — Ben Fiedler 1 year, 9 days ago 2db92bf main
Use swap_remove instead of duplicating move lists

This gives a 2x speedup for free
3 files changed, 9 insertions(+), 9 deletions(-)

M .gitignore
M src/main.rs
M src/position.rs
M .gitignore => .gitignore +1 -0
@@ 2,3 2,4 @@
/in
*.svg
/results
perf.data

M src/main.rs => src/main.rs +0 -1
@@ 58,7 58,6 @@ fn main() {
        }
        p.make_move(newmv);
    }
    dbg!(p);
    let n = Node { pos: p };
    let (cnt, mvs) = n.divide(depth);
    for mv in mvs {

M src/position.rs => src/position.rs +8 -8
@@ 273,8 273,6 @@ impl Position {
    }

    pub fn generate_legal_moves(&self) -> Vec<Move> {
        let mut legal_moves = Vec::new();

        let ksq = self.get_pieces(self.to_move, PieceType::King).to_sq();
        let king_attackers =
            self.attackers_for(ksq) & self.get_pieces(!self.to_move, PieceType::ALL);


@@ 284,24 282,26 @@ impl Position {
        let pinned = self.blockers_for(ksq, opponent_sliders)
            & self.get_pieces(self.to_move, PieceType::ALL);

        let moves = if king_attackers.is_not_empty() {
        let mut moves = if king_attackers.is_not_empty() {
            self.generate_evasions()
        } else {
            self.generate_moves()
        };

        for mv in moves {
        let mut i = 0;
        while i < moves.len() {
            let mv = moves[i];
            let from = mv.from();
            if (pinned & from).is_not_empty() || from == ksq || mv.is_en_passant() {
                if !self.is_legal_move(mv) {
                    continue;
                    moves.swap_remove(i);
                    i = i.wrapping_sub(1);
                }
            }

            legal_moves.push(mv);
            i = i.wrapping_add(1);
        }

        legal_moves
        moves
    }

    pub fn is_legal_move(&self, mv: Move) -> bool {