Skip to content

Commit

Permalink
Improve thread voting inefficiencies
Browse files Browse the repository at this point in the history
Initialize the unordered map to a reasonable
number of buckets and make the move hashes well
distributed. For more see
#4958 (comment)
Also make bestThreadPV and newThreadPV references
so we don't copy entire vectors.

closes #5048

No functional change
  • Loading branch information
mstembera authored and Disservin committed Feb 11, 2024
1 parent 91a4cea commit 531747e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/thread.cpp
Expand Up @@ -210,10 +210,9 @@ void ThreadPool::start_thinking(const OptionsMap& options,

Thread* ThreadPool::get_best_thread() const {

std::unordered_map<Move, int64_t, Move::MoveHash> votes;

Thread* bestThread = threads.front();
Value minScore = VALUE_NONE;
std::unordered_map<Move, int64_t, Move::MoveHash> votes(2 * std::min(size(), bestThread->worker->rootMoves.size()));

// Find the minimum score of all threads
for (Thread* th : threads)
Expand All @@ -232,13 +231,12 @@ Thread* ThreadPool::get_best_thread() const {
const auto bestThreadScore = bestThread->worker->rootMoves[0].score;
const auto newThreadScore = th->worker->rootMoves[0].score;

const auto bestThreadPV = bestThread->worker->rootMoves[0].pv;
const auto newThreadPV = th->worker->rootMoves[0].pv;
const auto& bestThreadPV = bestThread->worker->rootMoves[0].pv;
const auto& newThreadPV = th->worker->rootMoves[0].pv;

const auto bestThreadMoveVote = votes[bestThreadPV[0]];
const auto newThreadMoveVote = votes[newThreadPV[0]];


const bool bestThreadInProvenWin = bestThreadScore >= VALUE_TB_WIN_IN_MAX_PLY;
const bool newThreadInProvenWin = newThreadScore >= VALUE_TB_WIN_IN_MAX_PLY;

Expand Down
2 changes: 1 addition & 1 deletion src/types.h
Expand Up @@ -397,7 +397,7 @@ class Move {
constexpr std::uint16_t raw() const { return data; }

struct MoveHash {
std::size_t operator()(const Move& m) const { return m.data; }
std::size_t operator()(const Move& m) const { return make_key(m.data); }
};

protected:
Expand Down

0 comments on commit 531747e

Please sign in to comment.