Skip to content

Commit 531747e

Browse files
mstemberaDisservin
authored andcommitted
Improve thread voting inefficiencies
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
1 parent 91a4cea commit 531747e

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/thread.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,9 @@ void ThreadPool::start_thinking(const OptionsMap& options,
210210

211211
Thread* ThreadPool::get_best_thread() const {
212212

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

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

235-
const auto bestThreadPV = bestThread->worker->rootMoves[0].pv;
236-
const auto newThreadPV = th->worker->rootMoves[0].pv;
234+
const auto& bestThreadPV = bestThread->worker->rootMoves[0].pv;
235+
const auto& newThreadPV = th->worker->rootMoves[0].pv;
237236

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

241-
242240
const bool bestThreadInProvenWin = bestThreadScore >= VALUE_TB_WIN_IN_MAX_PLY;
243241
const bool newThreadInProvenWin = newThreadScore >= VALUE_TB_WIN_IN_MAX_PLY;
244242

src/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ class Move {
397397
constexpr std::uint16_t raw() const { return data; }
398398

399399
struct MoveHash {
400-
std::size_t operator()(const Move& m) const { return m.data; }
400+
std::size_t operator()(const Move& m) const { return make_key(m.data); }
401401
};
402402

403403
protected:

0 commit comments

Comments
 (0)