]> git.sesse.net Git - stockfish/commitdiff
Don't copy Position in pretty_pv()
authorMarco Costalba <mcostalba@gmail.com>
Sun, 9 Jan 2011 10:22:59 +0000 (11:22 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sun, 9 Jan 2011 11:55:22 +0000 (12:55 +0100)
Also let do_setup_move() don't reuse same StateInfo so that
we can remove the check about different StateInfo objects
before memcpy() in do_move.

Functional change due to harmless additionals
do_move() / undo_move() steps.

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

index a23702685d32b28c8aebb4313bd04e6552e3c5ac..1b33ff68f721993c42e2712b284fcd50c0d3d5a8 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "move.h"
 #include "movegen.h"
+#include "search.h"
 
 using std::string;
 
@@ -178,7 +179,7 @@ const string move_to_san(Position& pos, Move m) {
 /// It is used to write search information to the log file (which is created
 /// when the UCI parameter "Use Search Log" is "true").
 
-const string pretty_pv(const Position& pos, int time, int depth,
+const string pretty_pv(Position& pos, int time, int depth,
                        Value score, ValueType type, Move pv[]) {
 
   const int64_t K = 1000;
@@ -187,13 +188,12 @@ const string pretty_pv(const Position& pos, int time, int depth,
   const size_t maxLength = 80 - startColumn;
   const string lf = string("\n") + string(startColumn, ' ');
 
-  StateInfo st;
+  StateInfo state[PLY_MAX_PLUS_2], *st = state;
+  Move* m = pv;
   std::stringstream s;
   string san;
   size_t length = 0;
 
-  Position p(pos, pos.thread());
-
   // First print depth, score, time and searched nodes...
   s << std::setw(2) << depth
     << (type == VALUE_TYPE_LOWER ? " >" : type == VALUE_TYPE_UPPER ? " <" : "  ")
@@ -208,9 +208,9 @@ const string pretty_pv(const Position& pos, int time, int depth,
       s << std::setw(7) << pos.nodes_searched() / M << " M ";
 
   // ...then print the full PV line in short algebraic notation
-  for (Move* m = pv; *m != MOVE_NONE; m++)
+  while (*m != MOVE_NONE)
   {
-      san = move_to_san(p, *m);
+      san = move_to_san(pos, *m);
       length += san.length() + 1;
 
       if (length > maxLength)
@@ -220,9 +220,12 @@ const string pretty_pv(const Position& pos, int time, int depth,
       }
       s << san << ' ';
 
-      p.do_move(*m, st);
+      pos.do_move(*m++, *st++);
   }
 
+  // Restore original position before to leave
+  while (m != pv) pos.undo_move(*--m);
+
   return s.str();
 }
 
index 2adea52752c2f559567022ae543fcc161cf85a56..223d2a28ef54838c5e1efeeb22b50a9f2e44b273 100644 (file)
@@ -192,6 +192,6 @@ class Position;
 extern const std::string move_to_uci(Move m, bool chess960);
 extern Move move_from_uci(const Position& pos, const std::string& str);
 extern const std::string move_to_san(Position& pos, Move m);
-extern const std::string pretty_pv(const Position& pos, int time, int depth, Value score, ValueType type, Move pv[]);
+extern const std::string pretty_pv(Position& pos, int time, int depth, Value score, ValueType type, Move pv[]);
 
 #endif // !defined(MOVE_H_INCLUDED)
index f304d94a03c0f3f8af989429a221573f8292bfc7..80dcdd5724afab805dea2e90fe374baa08f90c71 100644 (file)
@@ -776,7 +776,9 @@ bool Position::move_is_check(Move m, const CheckInfo& ci) const {
 /// It should be used when setting up a position on board.
 /// You can't undo the move.
 
-void Position::do_setup_move(Move m, StateInfo& newSt) {
+void Position::do_setup_move(Move m) {
+
+  StateInfo newSt;
 
   do_move(m, newSt);
 
@@ -787,6 +789,10 @@ void Position::do_setup_move(Move m, StateInfo& newSt) {
 
   // Update the number of plies played from the starting position
   startPosPlyCounter++;
+
+  // Our StateInfo newSt is about going out of scope so copy
+  // its content inside pos before it disappears.
+  detach();
 }
 
 /// Position::do_move() makes a move, and saves all information necessary
@@ -803,6 +809,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
 
   assert(is_ok());
   assert(move_is_ok(m));
+  assert(&newSt != st);
 
   nodes++;
   Key key = st->key;
@@ -818,8 +825,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
     Value npMaterial[2];
   };
 
-  if (&newSt != st)
-      memcpy(&newSt, st, sizeof(ReducedStateInfo));
+  memcpy(&newSt, st, sizeof(ReducedStateInfo));
 
   newSt.previous = st;
   st = &newSt;
index 340cbdb1e9c0a25a0eead1e7945d9dafb9dccc7d..23d3094fed32630772405ceeeab4404f5095782c 100644 (file)
@@ -222,8 +222,7 @@ public:
   bool square_is_weak(Square s, Color c) const;
 
   // Doing and undoing moves
-  void detach();
-  void do_setup_move(Move m, StateInfo& St);
+  void do_setup_move(Move m);
   void do_move(Move m, StateInfo& st);
   void do_move(Move m, StateInfo& st, const CheckInfo& ci, bool moveIsCheck);
   void undo_move(Move m);
@@ -278,6 +277,7 @@ private:
 
   // Initialization helper functions (used while setting up a position)
   void clear();
+  void detach();
   void put_piece(Piece p, Square s);
   void do_allow_oo(Color c);
   void do_allow_ooo(Color c);
index 011bec694ff3c52639c8a4a86a74a4515b910f3b..180414c76ab93268f663f8452c35e7e67dea4508 100644 (file)
@@ -129,7 +129,7 @@ namespace {
 
     void extract_pv_from_tt(Position& pos);
     void insert_pv_in_tt(Position& pos);
-    std::string pv_info_to_uci(const Position& pos, Value alpha, Value beta, int pvLine = 0);
+    std::string pv_info_to_uci(Position& pos, Value alpha, Value beta, int pvLine = 0);
 
     int64_t nodes;
     Value pv_score;
@@ -2633,7 +2633,7 @@ split_point_start: // At split points actual search starts from here
   // formatted according to UCI specification and eventually writes the info
   // to a log file. It is called at each iteration or after a new pv is found.
 
-  std::string RootMove::pv_info_to_uci(const Position& pos, Value alpha, Value beta, int pvLine) {
+  std::string RootMove::pv_info_to_uci(Position& pos, Value alpha, Value beta, int pvLine) {
 
     std::stringstream s, l;
     Move* m = pv;
index c1073ad596831fafbb2750c8402b7b07d6515cc7..c6d090d2982c33594f3ba1f0c345d029cf59da29 100644 (file)
@@ -140,7 +140,7 @@ namespace {
 
   void set_position(Position& pos, UCIParser& up) {
 
-    string token;
+    string fen, token;
 
     if (!(up >> token) || (token != "startpos" && token != "fen"))
         return;
@@ -148,34 +148,19 @@ namespace {
     if (token == "startpos")
     {
         pos.from_fen(StartPositionFEN, false);
-        if (!(up >> token))
-            return;
+        up >> token; // Consume "moves" token
     }
     else // fen
     {
-        string fen;
         while (up >> token && token != "moves")
-        {
-            fen += token;
-            fen += ' ';
-        }
+            fen += token + string(" ");
+
         pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
     }
 
-    if (token != "moves")
-        return;
-
-    // Parse optional move list
-    Move move;
-    StateInfo st;
+    // Parse move list (if any)
     while (up >> token)
-    {
-        move = move_from_uci(pos, token);
-        pos.do_setup_move(move, st);
-    }
-    // Our StateInfo st is about going out of scope so copy
-    // its content inside pos before it disappears.
-    pos.detach();
+        pos.do_setup_move(move_from_uci(pos, token));
   }