made SortMoves 3% faster | bench 4800210

This commit is contained in:
rektdie committed 2025-05-20 08:26:02 +02:00
1 parent b7eafdbd29
commit ccd5ed7823
1 file changed
+16 -17
+16 -17
View File
@@ -52,30 +52,29 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext& ctx) {
} }
static void SortMoves(Board &board, int ply, SearchContext& ctx) { static void SortMoves(Board &board, int ply, SearchContext& ctx) {
std::array<int, MAX_MOVES> scores; if (board.currentMoveIndex <= 1) {
std::array<int, MAX_MOVES> indices; return;
}
int scores[MAX_MOVES];
// Initialize scores and indices
for (int i = 0; i < board.currentMoveIndex; i++) { for (int i = 0; i < board.currentMoveIndex; i++) {
scores[i] = ScoreMove(board, board.moveList[i], ply, ctx); scores[i] = ScoreMove(board, board.moveList[i], ply, ctx);
indices[i] = i;
} }
// Sort indices based on scores for (int i = 1; i < board.currentMoveIndex; i++) {
std::stable_sort(indices.begin(), indices.begin() + board.currentMoveIndex, Move tempMove = board.moveList[i];
[&scores](int a, int b) { int tempScore = scores[i];
return scores[a] > scores[b]; int j = i - 1;
});
// Create a temporary move list and reorder the moveList based on the sorted indices while (j >= 0 && scores[j] < tempScore) {
std::array<Move, MAX_MOVES> sortedMoves; board.moveList[j + 1] = board.moveList[j];
for (int i = 0; i < board.currentMoveIndex; i++) { scores[j + 1] = scores[j];
sortedMoves[i] = board.moveList[indices[i]]; j--;
} }
// Assign the sorted moves back to the board's move list board.moveList[j + 1] = tempMove;
for (int i = 0; i < board.currentMoveIndex; i++) { scores[j + 1] = tempScore;
board.moveList[i] = sortedMoves[i];
} }
} }