Skip to content

Commit 800410e

Browse files
committed
Use a per-thread array for generated moves
This greately reduces stack usage and is a prerequisite for next patch. Verified with 40K games both in single and SMP case that there are no regressions. No functional change.
1 parent 7b2cda9 commit 800410e

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

src/movepick.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats&
7575

7676
assert(d > DEPTH_ZERO);
7777

78-
cur = end = moves;
78+
cur = end = moves = pos.this_thread()->get_moves_array();
7979
endBadCaptures = moves + MAX_MOVES - 1;
8080
countermoves = cm;
8181
ss = s;
@@ -91,10 +91,11 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats&
9191
}
9292

9393
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
94-
Square sq) : pos(p), history(h), cur(moves), end(moves) {
94+
Square sq) : pos(p), history(h) {
9595

9696
assert(d <= DEPTH_ZERO);
9797

98+
cur = end = moves = pos.this_thread()->get_moves_array();
9899
if (p.checkers())
99100
stage = EVASION;
100101

@@ -123,10 +124,11 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats&
123124
}
124125

125126
MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, PieceType pt)
126-
: pos(p), history(h), cur(moves), end(moves) {
127+
: pos(p), history(h) {
127128

128129
assert(!pos.checkers());
129130

131+
cur = end = moves = pos.this_thread()->get_moves_array();
130132
stage = PROBCUT;
131133

132134
// In ProbCut we generate only captures better than parent's captured piece
@@ -139,6 +141,7 @@ MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, Piece
139141
end += (ttMove != MOVE_NONE);
140142
}
141143

144+
MovePicker::~MovePicker() { pos.this_thread()->free_moves_array(); }
142145

143146
/// score() assign a numerical move ordering score to each move in a move list.
144147
/// The moves with highest scores will be picked first.

src/movepick.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class MovePicker {
8787
MovePicker(const Position&, Move, Depth, const HistoryStats&, Square);
8888
MovePicker(const Position&, Move, const HistoryStats&, PieceType);
8989
MovePicker(const Position&, Move, Depth, const HistoryStats&, Move*, Search::Stack*);
90+
~MovePicker();
9091

9192
template<bool SpNode> Move next_move();
9293

@@ -103,8 +104,7 @@ class MovePicker {
103104
ExtMove killers[4];
104105
Square recaptureSquare;
105106
int captureThreshold, stage;
106-
ExtMove *cur, *end, *endQuiets, *endBadCaptures;
107-
ExtMove moves[MAX_MOVES];
107+
ExtMove *moves, *cur, *end, *endQuiets, *endBadCaptures;
108108
};
109109

110110
#endif // #ifndef MOVEPICK_H_INCLUDED

src/thread.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ void ThreadBase::wait_for(volatile const bool& b) {
8383
Thread::Thread() /* : splitPoints() */ { // Value-initialization bug in MSVC
8484

8585
searching = false;
86-
maxPly = splitPointsSize = 0;
86+
maxPly = splitPointsSize = curPage = 0;
8787
activeSplitPoint = NULL;
8888
activePosition = NULL;
8989
idx = Threads.size();
90+
movePages.resize(MAX_PLY_PLUS_6 * MAX_MOVES);
9091
}
9192

9293

src/thread.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ struct Thread : public ThreadBase {
115115
virtual void idle_loop();
116116
bool cutoff_occurred() const;
117117
bool is_available_to(const Thread* master) const;
118+
ExtMove* get_moves_array() { return &movePages[curPage += MAX_MOVES]; }
119+
void free_moves_array() { curPage -= MAX_MOVES; }
118120

119121
template <bool Fake>
120122
void split(Position& pos, const Search::Stack* ss, Value alpha, Value beta, Value* bestValue, Move* bestMove,
@@ -125,6 +127,8 @@ struct Thread : public ThreadBase {
125127
Endgames endgames;
126128
Pawns::Table pawnsTable;
127129
Position* activePosition;
130+
std::vector<ExtMove> movePages;
131+
int curPage;
128132
size_t idx;
129133
int maxPly;
130134
SplitPoint* volatile activeSplitPoint;

0 commit comments

Comments
 (0)