Skip to content

Commit 28933a5

Browse files
lucasartzamar
authored andcommitted
Retire RootNode template
There is no reason to compile 3 different copies of search(). PV nodes are on the cold path, and PvNode is a template parameter, so there is no cost in computing: const bool RootNode = PvNode && (ss-1)->ply == 0; And this simplifies code a tiny bit as well. Speed impact is negligible on my machine (i7-3770k, linux 4.2, gcc 5.2): nps +/- test 2378605 3118 master 2383128 2793 diff -4523 2746 Bench: 7751425 No functional change. Resolves #568
1 parent 12eb345 commit 28933a5

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/search.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ using namespace Search;
6262
namespace {
6363

6464
// Different node types, used as a template parameter
65-
enum NodeType { Root, PV, NonPV };
65+
enum NodeType { NonPV, PV };
6666

6767
// Razoring and futility margin based on depth
6868
const int razor_margin[4] = { 483, 570, 603, 554 };
@@ -441,7 +441,7 @@ void Thread::search() {
441441
// high/low anymore.
442442
while (true)
443443
{
444-
bestValue = ::search<Root>(rootPos, ss, alpha, beta, rootDepth, false);
444+
bestValue = ::search<PV>(rootPos, ss, alpha, beta, rootDepth, false);
445445

446446
// Bring the best move to the front. It is critical that sorting
447447
// is done with a stable algorithm because all the values but the
@@ -589,8 +589,8 @@ namespace {
589589
template <NodeType NT>
590590
Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode) {
591591

592-
const bool RootNode = NT == Root;
593-
const bool PvNode = NT == PV || NT == Root;
592+
const bool PvNode = NT == PV;
593+
const bool rootNode = PvNode && (ss-1)->ply == 0;
594594

595595
assert(-VALUE_INFINITE <= alpha && alpha < beta && beta <= VALUE_INFINITE);
596596
assert(PvNode || (alpha == beta - 1));
@@ -632,7 +632,7 @@ namespace {
632632
if (PvNode && thisThread->maxPly < ss->ply)
633633
thisThread->maxPly = ss->ply;
634634

635-
if (!RootNode)
635+
if (!rootNode)
636636
{
637637
// Step 2. Check for aborted search and immediate draw
638638
if (Signals.stop.load(std::memory_order_relaxed) || pos.is_draw() || ss->ply >= MAX_PLY)
@@ -664,7 +664,7 @@ namespace {
664664
posKey = excludedMove ? pos.exclusion_key() : pos.key();
665665
tte = TT.probe(posKey, ttHit);
666666
ttValue = ttHit ? value_from_tt(tte->value(), ss->ply) : VALUE_NONE;
667-
ttMove = RootNode ? thisThread->rootMoves[thisThread->PVIdx].pv[0]
667+
ttMove = rootNode ? thisThread->rootMoves[thisThread->PVIdx].pv[0]
668668
: ttHit ? tte->move() : MOVE_NONE;
669669

670670
// At non-PV nodes we check for an early TT cutoff
@@ -685,7 +685,7 @@ namespace {
685685
}
686686

687687
// Step 4a. Tablebase probe
688-
if (!RootNode && TB::Cardinality)
688+
if (!rootNode && TB::Cardinality)
689689
{
690690
int piecesCnt = pos.count<ALL_PIECES>(WHITE) + pos.count<ALL_PIECES>(BLACK);
691691

@@ -762,7 +762,7 @@ namespace {
762762
}
763763

764764
// Step 7. Futility pruning: child node (skipped when in check)
765-
if ( !RootNode
765+
if ( !rootNode
766766
&& depth < 7 * ONE_PLY
767767
&& eval - futility_margin(depth) >= beta
768768
&& eval < VALUE_KNOWN_WIN // Do not return unproven wins
@@ -846,7 +846,7 @@ namespace {
846846
{
847847
Depth d = depth - 2 * ONE_PLY - (PvNode ? DEPTH_ZERO : depth / 4);
848848
ss->skipEarlyPruning = true;
849-
search<PvNode ? PV : NonPV>(pos, ss, alpha, beta, d, true);
849+
search<NT>(pos, ss, alpha, beta, d, true);
850850
ss->skipEarlyPruning = false;
851851

852852
tte = TT.probe(posKey, ttHit);
@@ -866,7 +866,7 @@ namespace {
866866
|| ss->staticEval == VALUE_NONE
867867
||(ss-2)->staticEval == VALUE_NONE;
868868

869-
singularExtensionNode = !RootNode
869+
singularExtensionNode = !rootNode
870870
&& depth >= 8 * ONE_PLY
871871
&& ttMove != MOVE_NONE
872872
/* && ttValue != VALUE_NONE Already implicit in the next condition */
@@ -887,13 +887,13 @@ namespace {
887887
// At root obey the "searchmoves" option and skip moves not listed in Root
888888
// Move List. As a consequence any illegal move is also skipped. In MultiPV
889889
// mode we also skip PV moves which have been already searched.
890-
if (RootNode && !std::count(thisThread->rootMoves.begin() + thisThread->PVIdx,
890+
if (rootNode && !std::count(thisThread->rootMoves.begin() + thisThread->PVIdx,
891891
thisThread->rootMoves.end(), move))
892892
continue;
893893

894894
ss->moveCount = ++moveCount;
895895

896-
if (RootNode && thisThread == Threads.main() && Time.elapsed() > 3000)
896+
if (rootNode && thisThread == Threads.main() && Time.elapsed() > 3000)
897897
sync_cout << "info depth " << depth / ONE_PLY
898898
<< " currmove " << UCI::move(move, pos.is_chess960())
899899
<< " currmovenumber " << moveCount + thisThread->PVIdx << sync_endl;
@@ -937,7 +937,7 @@ namespace {
937937
newDepth = depth - ONE_PLY + extension;
938938

939939
// Step 13. Pruning at shallow depth
940-
if ( !RootNode
940+
if ( !rootNode
941941
&& !captureOrPromotion
942942
&& !inCheck
943943
&& !givesCheck
@@ -979,7 +979,7 @@ namespace {
979979
prefetch(TT.first_entry(pos.key_after(move)));
980980

981981
// Check for legality just before making the move
982-
if (!RootNode && !pos.legal(move, ci.pinned))
982+
if (!rootNode && !pos.legal(move, ci.pinned))
983983
{
984984
ss->moveCount = --moveCount;
985985
continue;
@@ -1038,7 +1038,7 @@ namespace {
10381038
// For PV nodes only, do a full PV search on the first move or after a fail
10391039
// high (in the latter case search only if value < beta), otherwise let the
10401040
// parent node fail low with value <= alpha and try another move.
1041-
if (PvNode && (moveCount == 1 || (value > alpha && (RootNode || value < beta))))
1041+
if (PvNode && (moveCount == 1 || (value > alpha && (rootNode || value < beta))))
10421042
{
10431043
(ss+1)->pv = pv;
10441044
(ss+1)->pv[0] = MOVE_NONE;
@@ -1061,7 +1061,7 @@ namespace {
10611061
if (Signals.stop.load(std::memory_order_relaxed))
10621062
return VALUE_ZERO;
10631063

1064-
if (RootNode)
1064+
if (rootNode)
10651065
{
10661066
RootMove& rm = *std::find(thisThread->rootMoves.begin(),
10671067
thisThread->rootMoves.end(), move);
@@ -1105,7 +1105,7 @@ namespace {
11051105

11061106
bestMove = move;
11071107

1108-
if (PvNode && !RootNode) // Update pv even in fail-high case
1108+
if (PvNode && !rootNode) // Update pv even in fail-high case
11091109
update_pv(ss->pv, move, (ss+1)->pv);
11101110

11111111
if (PvNode && value < beta) // Update alpha! Always alpha < beta
@@ -1176,7 +1176,6 @@ namespace {
11761176

11771177
const bool PvNode = NT == PV;
11781178

1179-
assert(NT == PV || NT == NonPV);
11801179
assert(InCheck == !!pos.checkers());
11811180
assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
11821181
assert(PvNode || (alpha == beta - 1));

0 commit comments

Comments
 (0)