From 531747ee7889d9b61b9841a57bb6d582459999d6 Mon Sep 17 00:00:00 2001 From: mstembera Date: Sat, 10 Feb 2024 15:06:38 -0800 Subject: [PATCH] Improve thread voting inefficiencies Initialize the unordered map to a reasonable number of buckets and make the move hashes well distributed. For more see https://github.com/official-stockfish/Stockfish/pull/4958#issuecomment-1937351190 Also make bestThreadPV and newThreadPV references so we don't copy entire vectors. closes https://github.com/official-stockfish/Stockfish/pull/5048 No functional change --- src/thread.cpp | 8 +++----- src/types.h | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/thread.cpp b/src/thread.cpp index 3cce7c56..2e42abd4 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -210,10 +210,9 @@ void ThreadPool::start_thinking(const OptionsMap& options, Thread* ThreadPool::get_best_thread() const { - std::unordered_map votes; - Thread* bestThread = threads.front(); Value minScore = VALUE_NONE; + std::unordered_map votes(2 * std::min(size(), bestThread->worker->rootMoves.size())); // Find the minimum score of all threads for (Thread* th : threads) @@ -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; diff --git a/src/types.h b/src/types.h index e83b306d..8b0ffb0c 100644 --- a/src/types.h +++ b/src/types.h @@ -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: