1
0
Fork 0

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 <mcostalba@gmail.com>
sf_2.3.1_base
Marco Costalba 2011-01-09 11:22:59 +01:00
parent c89762288b
commit 15153a1de7
6 changed files with 30 additions and 36 deletions

View 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();
}

View 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)

View 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;

View 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);

View 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;

View 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));
}