From 347fcaa392d32a03e7d405e413115b444565c14c Mon Sep 17 00:00:00 2001 From: anematode Date: Thu, 2 Apr 2026 20:25:55 +0200 Subject: [PATCH] Stack allocate NNUE buffer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Passed simp STC LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 29632 W: 7772 L: 7552 D: 14308 Ptnml(0-2): 76, 3155, 8147, 3349, Id expect there to be a small speedup (bc the compiler can skip some stores) but i haven’t tried to measure it closes https://github.com/official-stockfish/Stockfish/pull/6688 No functional change --- src/engine.cpp | 4 ++-- src/nnue/nnue_architecture.h | 11 ++--------- src/search.cpp | 8 +++----- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index d1f6ea4a7..786e88e21 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -48,8 +48,8 @@ namespace Stockfish { namespace NN = Eval::NNUE; -constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048; -int MaxThreads = std::max(1024, 4 * int(get_hardware_concurrency())); +constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048; +int MaxThreads = std::max(1024, 4 * int(get_hardware_concurrency())); // The default configuration will attempt to group L3 domains up to 32 threads. // This size was found to be a good balance between the Elo gain of increased diff --git a/src/nnue/nnue_architecture.h b/src/nnue/nnue_architecture.h index 389c84bbd..f81dcc0f2 100644 --- a/src/nnue/nnue_architecture.h +++ b/src/nnue/nnue_architecture.h @@ -109,17 +109,10 @@ struct NetworkArchitecture { alignas(CacheLineSize) typename decltype(ac_1)::OutputBuffer ac_1_out; alignas(CacheLineSize) typename decltype(fc_2)::OutputBuffer fc_2_out; - Buffer() { std::memset(this, 0, sizeof(*this)); } + Buffer() { std::memset(ac_sqr_0_out, 0, sizeof(ac_sqr_0_out)); } }; -#if defined(__clang__) && (__APPLE__) - // workaround for a bug reported with xcode 12 - static thread_local auto tlsBuffer = std::make_unique(); - // Access TLS only once, cache result. - Buffer& buffer = *tlsBuffer; -#else - alignas(CacheLineSize) static thread_local Buffer buffer; -#endif + Buffer buffer; fc_0.propagate(transformedFeatures, buffer.fc_0_out); ac_sqr_0.propagate(buffer.fc_0_out, buffer.ac_sqr_0_out); diff --git a/src/search.cpp b/src/search.cpp index 38e77c6d9..d14b7ad28 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -2035,9 +2035,8 @@ void syzygy_extend_pv(const OptionsMap& options, for (const auto& m : MoveList(pos)) legalMoves.emplace_back(m); - TB::Config config = - TB::rank_root_moves(options, pos, legalMoves, false, time_abort); - RootMove& rm = *std::find(legalMoves.begin(), legalMoves.end(), pvMove); + TB::Config config = TB::rank_root_moves(options, pos, legalMoves, false, time_abort); + RootMove& rm = *std::find(legalMoves.begin(), legalMoves.end(), pvMove); if (legalMoves[0].tbRank != rm.tbRank) break; @@ -2096,8 +2095,7 @@ void syzygy_extend_pv(const OptionsMap& options, [](const Search::RootMove& a, const Search::RootMove& b) { return a.tbRank > b.tbRank; }); // The winning side tries to minimize DTZ, the losing side maximizes it - TB::Config config = - TB::rank_root_moves(options, pos, legalMoves, true, time_abort); + TB::Config config = TB::rank_root_moves(options, pos, legalMoves, true, time_abort); // If DTZ is not available we might not find a mate, so we bail out if (!config.rootInTB || config.cardinality > 0)