]> git.sesse.net Git - stockfish/commitdiff
Do not copy the whole old state in do_move()
authorMarco Costalba <mcostalba@gmail.com>
Mon, 23 Feb 2009 11:06:06 +0000 (12:06 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Mon, 23 Feb 2009 20:44:29 +0000 (21:44 +0100)
Instead of copy the old state in the new one, copy only
fields that will be updated incrementally, not the ones
that will be recalculcated anyway.

This let us copy 13 bytes instead of 28 for each do_move()
call.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/position.cpp
src/position.h

index 0a7ebadda7f3b5614668781f715f6c067cc7ad3f..7c8399e0918d17231d748b2294952fd4119a7640 100644 (file)
@@ -676,6 +676,25 @@ inline void Position::update_checkers(Bitboard* pCheckersBB, Square ksq, Square
 }
 
 
 }
 
 
+/// Position::init_new_state() copies from the current state the fields
+/// that will be updated incrementally, skips the fields, like bitboards
+/// that will be recalculated form scratch anyway.
+
+void Position::init_new_state(StateInfo& newSt) {
+
+  newSt.key          = st->key;
+  newSt.pawnKey      = st->pawnKey;
+  newSt.materialKey  = st->materialKey;
+  newSt.castleRights = st->castleRights;
+  newSt.rule50       = st->rule50;
+  newSt.epSquare     = st->epSquare;
+  newSt.mgValue      = st->mgValue;
+  newSt.egValue      = st->egValue;
+  newSt.capture      = NO_PIECE_TYPE;
+  newSt.previous     = st;
+}
+
+
 /// Position::do_move() makes a move, and saves all information necessary
 /// to a StateInfo object. The move is assumed to be legal.
 /// Pseudo-legal moves should be filtered out before this function is called.
 /// Position::do_move() makes a move, and saves all information necessary
 /// to a StateInfo object. The move is assumed to be legal.
 /// Pseudo-legal moves should be filtered out before this function is called.
@@ -685,17 +704,14 @@ void Position::do_move(Move m, StateInfo& newSt) {
   assert(is_ok());
   assert(move_is_ok(m));
 
   assert(is_ok());
   assert(move_is_ok(m));
 
-  // Get now the current (pre-move) dc candidates that we will use
+  // Get now the current (before to move) dc candidates that we will use
   // in update_checkers().
   Bitboard oldDcCandidates = discovered_check_candidates(side_to_move());
 
   // in update_checkers().
   Bitboard oldDcCandidates = discovered_check_candidates(side_to_move());
 
-  // Copy the old state to our new StateInfo object (except the
-  // captured piece, which is taken care of later.
-  // TODO do not copy pinners and checkersBB because are recalculated
-  // anyway.
-  newSt = *st;
-  newSt.capture = NO_PIECE_TYPE;
-  newSt.previous = st;
+  // Copy some fields of old state to our new StateInfo object (except the
+  // captured piece, which is taken care of later) and switch state pointer
+  // to point to the new, ready to be updated, state.
+  init_new_state(newSt);
   st = &newSt;
 
   // Save the current key to the history[] array, in order to be able to
   st = &newSt;
 
   // Save the current key to the history[] array, in order to be able to
index 981bad3e846f31cb8459efd82f5884ba5d17aa62..ae750697aa894b423fa52e15f5fb4a7d8f93f0eb 100644 (file)
@@ -83,10 +83,10 @@ struct StateInfo {
   Key key, pawnKey, materialKey;
   int castleRights, rule50;
   Square epSquare;
   Key key, pawnKey, materialKey;
   int castleRights, rule50;
   Square epSquare;
-  Move lastMove;
   Value mgValue, egValue;
   PieceType capture;
   StateInfo* previous;
   Value mgValue, egValue;
   PieceType capture;
   StateInfo* previous;
+  Move lastMove;
 };
 
 
 };
 
 
@@ -294,6 +294,7 @@ private:
   void allow_ooo(Color c);
 
   // Helper functions for doing and undoing moves
   void allow_ooo(Color c);
 
   // Helper functions for doing and undoing moves
+  void init_new_state(StateInfo& newSt);
   void do_capture_move(Move m, PieceType capture, Color them, Square to);
   void do_castle_move(Move m);
   void do_promotion_move(Move m);
   void do_capture_move(Move m, PieceType capture, Color them, Square to);
   void do_castle_move(Move m);
   void do_promotion_move(Move m);