Skip to content

Commit b083049

Browse files
anematodevondele
authored andcommitted
Use non-locking operations for nodes count and tbHits
fetch_add and friends must produce a locking instruction because they guarantee consistency in the presence of multiple writers. Because only one thread ever writes to nodes and tbHits, we may use relaxed load and store to emit regular loads and stores (on both x86-64 and arm64), while avoiding undefined behavior and miscounting nodes. Credit must go to Arseniy Surkov of Reckless (codedeliveryservice) for uncovering this performance gotcha. failed yellow as a gainer on fishtest: LLR: -2.95 (-2.94,2.94) <0.00,2.00> Total: 219616 W: 57165 L: 57105 D: 105346 Ptnml(0-2): 732, 24277, 59720, 24357, 722 https://tests.stockfishchess.org/tests/view/69086abfea4b268f1fac2809 potential small speedup on large core system: ``` ==== master ==== 1 Nodes/second : 294269736 2 Nodes/second : 295217629 Average (over 2): 294743682 ==== patch ==== 1 Nodes/second : 299003603 2 Nodes/second : 298111320 Average (over 2): 298557461 ``` closes #6392 No functional change
1 parent 1d504b9 commit b083049

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/search.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ void Search::Worker::do_move(Position& pos, const Move move, StateInfo& st, Stac
529529
void Search::Worker::do_move(
530530
Position& pos, const Move move, StateInfo& st, const bool givesCheck, Stack* const ss) {
531531
bool capture = pos.capture_stage(move);
532-
nodes.fetch_add(1, std::memory_order_relaxed);
532+
// Preferable over fetch_add to avoid locking instructions
533+
nodes.store(nodes.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed);
533534

534535
auto [dirtyPiece, dirtyThreats] = accumulatorStack.push();
535536
pos.do_move(move, st, givesCheck, dirtyPiece, dirtyThreats, &tt);
@@ -749,7 +750,8 @@ Value Search::Worker::search(
749750

750751
if (err != TB::ProbeState::FAIL)
751752
{
752-
tbHits.fetch_add(1, std::memory_order_relaxed);
753+
// Preferable over fetch_add to avoid locking instructions
754+
tbHits.store(tbHits.load(std::memory_order_relaxed) + 1, std::memory_order_relaxed);
753755

754756
int drawScore = tbConfig.useRule50 ? 1 : 0;
755757

0 commit comments

Comments
 (0)