From: Marco Costalba Date: Sun, 9 Jan 2011 10:22:59 +0000 (+0100) Subject: Don't copy Position in pretty_pv() X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=15153a1de71d2d36c1f95eecfcf036fb76565309 Don't copy Position in pretty_pv() 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 --- diff --git a/src/move.cpp b/src/move.cpp index a2370268..1b33ff68 100644 --- a/src/move.cpp +++ b/src/move.cpp @@ -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(); } diff --git a/src/move.h b/src/move.h index 2adea527..223d2a28 100644 --- a/src/move.h +++ b/src/move.h @@ -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) diff --git a/src/position.cpp b/src/position.cpp index f304d94a..80dcdd57 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -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; diff --git a/src/position.h b/src/position.h index 340cbdb1..23d3094f 100644 --- a/src/position.h +++ b/src/position.h @@ -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); diff --git a/src/search.cpp b/src/search.cpp index 011bec69..180414c7 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -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; diff --git a/src/uci.cpp b/src/uci.cpp index c1073ad5..c6d090d2 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -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()); } - 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)); }