Avoid branching before ValueList::push_back

Instead of checking whether a threat index is valid and then writing to
a vector if it isn't, we can instead always write to the buffer of the
vector, and only increase the size of the vector if the index is valid.

This saves some branch mispredictions.

passed STC:
https://tests.stockfishchess.org/tests/view/69ceb1689f7a7e3fdfc9a44b
LLR: 2.95 (-2.94,2.94) <0.00,2.00>
Total: 205760 W: 53109 L: 52561 D: 100090
Ptnml(0-2): 603, 22552, 56076, 22992, 657

local speedtest shows
```
Result of 100 runs (cycles)
===========================
base (...ockfish.orig) =     296886  +/- 1645
test (...kfish.df0822) =     298862  +/- 1662
speedup %              =      +0.67  +/- 0.19
                         [95% CI; t-statistic]

p(speedup > 0)   =  1.0000
p(speedup > .5%) =  0.9562

CPU: 16 x Intel(R) Core(TM) Ultra 9 185H
Hyperthreading: on
```

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

No functional change
This commit is contained in:
Carlos Esparza
2026-04-09 21:56:05 +02:00
committed by Disservin
parent ead7e650da
commit df01cb7ed3
2 changed files with 14 additions and 16 deletions
+6
View File
@@ -201,6 +201,12 @@ class ValueList {
assert(size_ < MaxSize);
values_[size_++] = value;
}
// pushes back value if value < max
void push_back_if_lt(const T& value, const T& max) {
assert(size_ < MaxSize);
values_[size_] = value;
size_ += (value < max);
}
const T* begin() const { return values_; }
const T* end() const { return values_ + size_; }
const T& operator[](int index) const { return values_[index]; }
+8 -16
View File
@@ -22,7 +22,6 @@
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <utility>
@@ -236,8 +235,7 @@ void FullThreats::append_active_indices(Color perspective, const Position& pos,
Piece attacked = pos.piece_on(to);
IndexType index = make_index(perspective, attacker, from, to, attacked, ksq);
if (index < Dimensions)
active.push_back(index);
active.push_back_if_lt(index, Dimensions);
}
while (attacks_right)
@@ -247,8 +245,7 @@ void FullThreats::append_active_indices(Color perspective, const Position& pos,
Piece attacked = pos.piece_on(to);
IndexType index = make_index(perspective, attacker, from, to, attacked, ksq);
if (index < Dimensions)
active.push_back(index);
active.push_back_if_lt(index, Dimensions);
}
// Set of pawns which are prevented from movement by a pawn in front of them
@@ -261,8 +258,7 @@ void FullThreats::append_active_indices(Color perspective, const Position& pos,
assert(type_of(attacked) == PAWN);
IndexType index = make_index(perspective, attacker, from, to, attacked, ksq);
if (index < Dimensions)
active.push_back(index);
active.push_back_if_lt(index, Dimensions);
}
}
else
@@ -279,8 +275,7 @@ void FullThreats::append_active_indices(Color perspective, const Position& pos,
IndexType index =
make_index(perspective, attacker, from, to, attacked, ksq);
if (index < Dimensions)
active.push_back(index);
active.push_back_if_lt(index, Dimensions);
}
}
}
@@ -342,13 +337,10 @@ void FullThreats::append_changed_indices(Color perspective,
auto& insert = add ? added : removed;
const IndexType index = make_index(perspective, attacker, from, to, attacked, ksq);
if (index < Dimensions)
{
if (prefetchBase)
prefetch<PrefetchRw::READ, PrefetchLoc::LOW>(
prefetchBase + static_cast<std::ptrdiff_t>(index) * prefetchStride);
insert.push_back(index);
}
if (prefetchBase)
prefetch<PrefetchRw::READ, PrefetchLoc::LOW>(reinterpret_cast<const void*>(
reinterpret_cast<uintptr_t>(prefetchBase) + index * prefetchStride));
insert.push_back_if_lt(index, Dimensions);
}
}