Don't copy around DirtyThreats

The contents of DirtyThreats gets memcpyed twice for each call to do_move.
(Return value optimization doesn't apply to the do_move function itself because
it constructs a std::pair, so it gets copied; and the calls to reset also
require a copy.) This patch inserts the dirty info in place.

Sometimes the caller of do_move ignores the DirtyThreats info, so we pass in
scratch objects. I found that stack-allocating these scratch objects was bad on
Fishtest, so I begrudgingly put them in the Position struct. Both templating
the do_move function on whether dirty threats are needed and putting a
null-check branch for each use of dirty threats were slowdowns locally. Of
course, nothing prevents a future attempt at cleaning this up.

passed STC
LLR: 2.96 (-2.94,2.94) <0.00,2.00>
Total: 68448 W: 17770 L: 17418 D: 33260
Ptnml(0-2): 198, 7425, 18630, 7769, 202
https://tests.stockfishchess.org/tests/view/6914c01a7ca87818523318ba

closes https://github.com/official-stockfish/Stockfish/pull/6414

No functional change
This commit is contained in:
Timothy Herchen
2025-11-13 22:17:03 +01:00
committed by Joost VandeVondele
parent 3ae7684714
commit fa4f05d3ef
5 changed files with 55 additions and 37 deletions
+6 -3
View File
@@ -21,6 +21,7 @@
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <new>
#include <type_traits>
#include "../bitboard.h"
@@ -122,11 +123,13 @@ void AccumulatorStack::reset() noexcept {
size = 1;
}
void AccumulatorStack::push(const DirtyBoardData& dirtyBoardData) noexcept {
std::pair<DirtyPiece&, DirtyThreats&> AccumulatorStack::push() noexcept {
assert(size < MaxSize);
psq_accumulators[size].reset(dirtyBoardData.dp);
threat_accumulators[size].reset(dirtyBoardData.dts);
auto& dp = psq_accumulators[size].reset();
auto& dts = threat_accumulators[size].reset();
new (&dts) DirtyThreats;
size++;
return {dp, dts};
}
void AccumulatorStack::pop() noexcept {
+11 -3
View File
@@ -25,6 +25,7 @@
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <utility>
#include "../types.h"
#include "nnue_architecture.h"
@@ -141,6 +142,12 @@ struct AccumulatorState {
accumulatorBig.computed.fill(false);
accumulatorSmall.computed.fill(false);
}
typename FeatureSet::DiffType& reset() noexcept {
accumulatorBig.computed.fill(false);
accumulatorSmall.computed.fill(false);
return diff;
}
};
class AccumulatorStack {
@@ -150,9 +157,10 @@ class AccumulatorStack {
template<typename T>
[[nodiscard]] const AccumulatorState<T>& latest() const noexcept;
void reset() noexcept;
void push(const DirtyBoardData& dirtyBoardData) noexcept;
void pop() noexcept;
void reset() noexcept;
void push(const DirtyBoardData& dirtyBoardData) noexcept;
std::pair<DirtyPiece&, DirtyThreats&> push() noexcept;
void pop() noexcept;
template<IndexType Dimensions>
void evaluate(const Position& pos,