From 571f54b176b5c30aaac35bc7bbd255c79cdcb926 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 28 Jul 2018 15:33:39 +0200 Subject: [PATCH] Improve Stats definition Use operator const T&() instead of operator T() to avoid possible costly hidden copies of non-scalar nested types. Currently StatsEntry has a single member T, so assuming sizeof(StatsEntry) == sizeof(T) it happens to work, but it's better to use the size of the proper entry type in std::fill. Note that current code works because std::array items are ensured to be allocated in contiguous memory and there is no padding among nested arrays. The latter condition does not seem to be strictly enforced by the standard, so be careful here. Finally use address-of operator instead of get() to fully hide the wrapper class StatsEntry at calling sites. For completness add the arrow operator too and simplify the C++ code a bit more. Same binary code as previous master under the Clang compiler. No functional change. --- src/movepick.h | 25 +++++++++++++------------ src/search.cpp | 14 +++++++------- src/thread.cpp | 4 ++-- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/movepick.h b/src/movepick.h index 141454a8..9a9144d2 100644 --- a/src/movepick.h +++ b/src/movepick.h @@ -36,18 +36,16 @@ template class StatsEntry { - static const bool IsInt = std::is_integral::value; - typedef typename std::conditional::type TT; - T entry; public: - T* get() { return &entry; } void operator=(const T& v) { entry = v; } - operator TT() const { return entry; } + T* operator&() { return &entry; } + T* operator->() { return &entry; } + operator const T&() const { return entry; } void operator<<(int bonus) { - assert(abs(bonus) <= D); // Ensure range is [-D, D] + assert(abs(bonus) <= D); // Ensure range is [-D, D] static_assert(D <= std::numeric_limits::max(), "D overflows T"); entry += bonus - entry * abs(bonus) / D; @@ -64,18 +62,21 @@ public: template struct Stats : public std::array, Size> { - T* get() { return this->at(0).get(); } + typedef Stats stats; void fill(const T& v) { - T* p = get(); - std::fill(p, p + sizeof(*this) / sizeof(*p), v); + + // For standard-layout 'this' points to first struct member + assert(std::is_standard_layout::value); + + typedef StatsEntry entry; + entry* p = reinterpret_cast(this); + std::fill(p, p + sizeof(*this) / sizeof(entry), v); } }; template -struct Stats : public std::array, Size> { - T* get() { return this->at(0).get(); } -}; +struct Stats : public std::array, Size> {}; /// In stats table, D=0 means that the template parameter is not used enum StatsParams { NOT_USED = 0 }; diff --git a/src/search.cpp b/src/search.cpp index b4096eba..e36c6959 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -290,7 +290,7 @@ void Thread::search() { std::memset(ss-4, 0, 7 * sizeof(Stack)); for (int i = 4; i > 0; i--) - (ss-i)->continuationHistory = this->continuationHistory[NO_PIECE][0].get(); // Use as sentinel + (ss-i)->continuationHistory = &this->continuationHistory[NO_PIECE][0]; // Use as sentinel bestValue = delta = alpha = -VALUE_INFINITE; beta = VALUE_INFINITE; @@ -587,7 +587,7 @@ namespace { (ss+1)->ply = ss->ply + 1; ss->currentMove = (ss+1)->excludedMove = bestMove = MOVE_NONE; - ss->continuationHistory = thisThread->continuationHistory[NO_PIECE][0].get(); + ss->continuationHistory = &thisThread->continuationHistory[NO_PIECE][0]; (ss+2)->killers[0] = (ss+2)->killers[1] = MOVE_NONE; Square prevSq = to_sq((ss-1)->currentMove); @@ -751,7 +751,7 @@ namespace { Depth R = ((823 + 67 * depth / ONE_PLY) / 256 + std::min((eval - beta) / PawnValueMg, 3)) * ONE_PLY; ss->currentMove = MOVE_NULL; - ss->continuationHistory = thisThread->continuationHistory[NO_PIECE][0].get(); + ss->continuationHistory = &thisThread->continuationHistory[NO_PIECE][0]; pos.do_null_move(st); @@ -802,7 +802,7 @@ namespace { probCutCount++; ss->currentMove = move; - ss->continuationHistory = thisThread->continuationHistory[pos.moved_piece(move)][to_sq(move)].get(); + ss->continuationHistory = &thisThread->continuationHistory[pos.moved_piece(move)][to_sq(move)]; assert(depth >= 5 * ONE_PLY); @@ -970,7 +970,7 @@ moves_loop: // When in check, search starts from here // Update the current move (this must be done after singular extension search) ss->currentMove = move; - ss->continuationHistory = thisThread->continuationHistory[movedPiece][to_sq(move)].get(); + ss->continuationHistory = &thisThread->continuationHistory[movedPiece][to_sq(move)]; // Step 15. Make the move pos.do_move(move, st, givesCheck); @@ -1213,7 +1213,7 @@ moves_loop: // When in check, search starts from here Thread* thisThread = pos.this_thread(); (ss+1)->ply = ss->ply + 1; ss->currentMove = bestMove = MOVE_NONE; - ss->continuationHistory = thisThread->continuationHistory[NO_PIECE][0].get(); + ss->continuationHistory = &thisThread->continuationHistory[NO_PIECE][0]; inCheck = pos.checkers(); moveCount = 0; @@ -1348,7 +1348,7 @@ moves_loop: // When in check, search starts from here } ss->currentMove = move; - ss->continuationHistory = thisThread->continuationHistory[pos.moved_piece(move)][to_sq(move)].get(); + ss->continuationHistory = &thisThread->continuationHistory[pos.moved_piece(move)][to_sq(move)]; // Make and search the move pos.do_move(move, st, givesCheck); diff --git a/src/thread.cpp b/src/thread.cpp index 8b13f6e3..ed7c5f33 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -63,9 +63,9 @@ void Thread::clear() { for (auto& to : continuationHistory) for (auto& h : to) - h.get()->fill(0); + h->fill(0); - continuationHistory[NO_PIECE][0].get()->fill(Search::CounterMovePruneThreshold - 1); + continuationHistory[NO_PIECE][0]->fill(Search::CounterMovePruneThreshold - 1); } /// Thread::start_searching() wakes up the thread that will start the search -- 2.39.2