From d3608c4e79a29110f4c4a369d7207c6dd8e01f34 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 19 May 2013 22:00:49 +0200 Subject: [PATCH 1/1] Microptimize MoveList loop Add MOVE_NONE at the tail, this allows to loop across MoveList checking for *it != MOVE_NONE, and because *it is used imediately after compiler is able to reuse it. With this small patch perft speed increased of 3% And it is also a semplification ! No functional change. --- src/book.cpp | 2 +- src/movegen.h | 3 +-- src/notation.cpp | 2 +- src/position.cpp | 2 +- src/search.cpp | 2 +- src/thread.cpp | 2 +- 6 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/book.cpp b/src/book.cpp index b906d766..caf2b066 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -436,7 +436,7 @@ Move PolyglotBook::probe(const Position& pos, const string& fName, bool pickBest move = make(from_sq(move), to_sq(move), PieceType(pt + 1)); // Add 'special move' flags and verify it is legal - for (MoveList it(pos); !it.end(); ++it) + for (MoveList it(pos); *it; ++it) if (move == (*it ^ type_of(*it))) return *it; diff --git a/src/movegen.h b/src/movegen.h index 9db23dd4..f8c4f22c 100644 --- a/src/movegen.h +++ b/src/movegen.h @@ -41,10 +41,9 @@ MoveStack* generate(const Position& pos, MoveStack* mlist); template struct MoveList { - explicit MoveList(const Position& pos) : cur(mlist), last(generate(pos, mlist)) {} + explicit MoveList(const Position& pos) : cur(mlist), last(generate(pos, mlist)) { last->move = MOVE_NONE; } void operator++() { cur++; } Move operator*() const { return cur->move; } - bool end() const { return cur == last; } size_t size() const { return last - mlist; } bool contains(Move m) const { for (const MoveStack* it(mlist); it != last; ++it) if (it->move == m) return true; diff --git a/src/notation.cpp b/src/notation.cpp index 0a97d9fd..c96b9dbe 100644 --- a/src/notation.cpp +++ b/src/notation.cpp @@ -89,7 +89,7 @@ Move move_from_uci(const Position& pos, string& str) { if (str.length() == 5) // Junior could send promotion piece in uppercase str[4] = char(tolower(str[4])); - for (MoveList it(pos); !it.end(); ++it) + for (MoveList it(pos); *it; ++it) if (str == move_to_uci(*it, pos.is_chess960())) return *it; diff --git a/src/position.cpp b/src/position.cpp index 80130fec..1a8c7e00 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -408,7 +408,7 @@ const string Position::pretty(Move move) const { ss << square_to_string(pop_lsb(&b)) << " "; ss << "\nLegal moves: "; - for (MoveList it(*this); !it.end(); ++it) + for (MoveList it(*this); *it; ++it) ss << move_to_san(*const_cast(this), *it) << " "; return ss.str(); diff --git a/src/search.cpp b/src/search.cpp index a932cf32..8ba5b41a 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -163,7 +163,7 @@ size_t Search::perft(Position& pos, Depth depth) { size_t cnt = 0; CheckInfo ci(pos); - for (MoveList it(pos); !it.end(); ++it) + for (MoveList it(pos); *it; ++it) { pos.do_move(*it, st, ci, pos.move_gives_check(*it, ci)); cnt += perft(pos, depth - ONE_PLY); diff --git a/src/thread.cpp b/src/thread.cpp index 71b9ce0c..782cf1ac 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -371,7 +371,7 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits, SetupStates = states; // Ownership transfer here RootMoves.clear(); - for (MoveList it(pos); !it.end(); ++it) + for (MoveList it(pos); *it; ++it) if ( searchMoves.empty() || std::count(searchMoves.begin(), searchMoves.end(), *it)) RootMoves.push_back(RootMove(*it)); -- 2.39.2