diff --git a/include/config.h b/include/config.h index 23fbcaf1..486c2501 100644 --- a/include/config.h +++ b/include/config.h @@ -71,9 +71,6 @@ //#define TRANSPOSITION_TABLE_DEBUG #endif -// TODO: Fix disable Memory Tool build failed -#define MEMORY_POOL - //#define USE_STD_STACK //#define RAPID_GAME diff --git a/src/ai/movegen.cpp b/src/ai/movegen.cpp index 5b76f140..f4df48b3 100644 --- a/src/ai/movegen.cpp +++ b/src/ai/movegen.cpp @@ -32,40 +32,9 @@ void MoveList::generate(AIAlgorithm &ai, Game &tempGame, { const int MOVE_PRIORITY_TABLE_SIZE = Board::N_RINGS * Board::N_SEATS; square_t square = SQ_0; - size_t newCapacity = 24; - - // 留足余量空间避免多次重新分配,此动作本身也占用 CPU/内存 开销 - switch (tempGame.getPhase()) { - case PHASE_PLACING: - if (tempGame.getAction() == ACTION_CAPTURE) { - newCapacity = static_cast(tempGame.getPiecesOnBoardCount(tempGame.position.opponentId)); - } else { - newCapacity = static_cast(tempGame.getPiecesInHandCount(BLACK) + tempGame.getPiecesInHandCount(WHITE)); - } - break; - case PHASE_MOVING: - if (tempGame.getAction() == ACTION_CAPTURE) { - newCapacity = static_cast(tempGame.getPiecesOnBoardCount(tempGame.position.opponentId)); - } else { - newCapacity = 6; - } - break; - case PHASE_READY: - newCapacity = 24; - break; - default: - newCapacity = 24; - break; - }; // 如果有子节点,则返回,避免重复建立 -#ifdef MEMORY_POOL if (node->childrenSize) { -#else - node->children.reserve(newCapacity + 2 /* TODO: 未细调故再多留余量2 */); - - if (!node->children.empty()) { -#endif return; } diff --git a/src/ai/search.cpp b/src/ai/search.cpp index c0999b19..22d69a5d 100644 --- a/src/ai/search.cpp +++ b/src/ai/search.cpp @@ -48,9 +48,7 @@ vector history; AIAlgorithm::AIAlgorithm() { -#ifdef MEMORY_POOL memmgr.memmgr_init(); -#endif buildRoot(); } @@ -60,9 +58,7 @@ AIAlgorithm::~AIAlgorithm() deleteTree(root); root = nullptr; -#ifdef MEMORY_POOL memmgr.memmgr_exit(); -#endif } depth_t AIAlgorithm::changeDepth(depth_t origDepth) @@ -153,19 +149,16 @@ struct AIAlgorithm::Node *AIAlgorithm::addNode( player_t side ) { -#ifdef MEMORY_POOL Node *newNode = (Node *)memmgr.memmgr_alloc(sizeof(Node)); if (newNode == nullptr) { memmgr.memmgr_print_stats(); loggerDebug("Memory Manager Alloc failed\n"); // TODO: Deal with alloc failed + return nullptr; } - newNode->childrenSize = 0; // Important -#else - Node *newNode = new Node; -#endif + newNode->childrenSize = 0; // Important newNode->parent = parent; newNode->value = value; newNode->move = move; @@ -233,25 +226,18 @@ struct AIAlgorithm::Node *AIAlgorithm::addNode( parent->children.push_back(newNode); } #else // MILL_FIRST -#ifdef MEMORY_POOL parent->children[parent->childrenSize] = newNode; parent->childrenSize++; -#else // MEMORY_POOL - parent->children.push_back(newNode); -#endif // MEMORY_POOL #endif // MILL_FIRST } else { // 如果启用了置换表并且不是叶子结点,把哈希得到的最优着法换到首位 -#ifdef MEMORY_POOL + // TODO: memmove for (int i = parent->childrenSize; i >= 1; i--) { parent->children[i] = parent->children[i - 1]; } parent->children[0] = newNode; parent->childrenSize++; -#else // MEMORY_POOL - parent->children.insert(parent->children.begin(), newNode); -#endif // MEMORY_POOL } } @@ -351,7 +337,6 @@ void AIAlgorithm::sortMoves(Node *node) // 这个函数对效率的影响很大,排序好的话,剪枝较早,节省时间,但不能在此函数耗费太多时间 auto cmp = tempGame.position.sideToMove == PLAYER_BLACK ? nodeGreater : nodeLess; -#ifdef MEMORY_POOL assert(node->childrenSize != 0); //#define DEBUG_SORT @@ -384,10 +369,8 @@ void AIAlgorithm::sortMoves(Node *node) } loggerDebug("\n----------------------------------------\n"); #endif + assert(node->childrenSize != 0); -#else - std::stable_sort(node->children.begin(), node->children.end(), cmp); -#endif } void AIAlgorithm::deleteTree(Node *node) @@ -397,31 +380,15 @@ void AIAlgorithm::deleteTree(Node *node) return; } -#ifdef MEMORY_POOL for (int i = 0; i < node->childrenSize; i++) { deleteTree(node->children[i]); } -#else - for (auto child : node->children) { - deleteTree(child); - } -#endif -#ifdef MEMORY_POOL if (node->childrenSize) { node->childrenSize = 0; } -#else - if (!node->children.empty()) { - node->children.clear(); - } -#endif -#ifdef MEMORY_POOL memmgr.memmgr_free(node); -#else - delete(node); -#endif } void AIAlgorithm::setGame(const Game &g) @@ -446,13 +413,11 @@ void AIAlgorithm::setGame(const Game &g) position = &(tempGame.position); requiredQuit = false; deleteTree(root); -#ifdef MEMORY_POOL + root = (Node *)memmgr.memmgr_alloc(sizeof(Node)); assert(root != nullptr); + root->childrenSize = 0; // Important -#else - root = new Node; -#endif root->value = VALUE_ZERO; root->move = MOVE_NONE; root->parent = nullptr; @@ -718,18 +683,10 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no minMax = tempGame.position.sideToMove == PLAYER_BLACK ? -VALUE_INFINITE : VALUE_INFINITE; -#ifdef MEMORY_POOL assert(node->childrenSize != 0); - for (int i = 0; i < node->childrenSize; i++) { -#else - for (auto child : node->children) { -#endif // MEMORY_POOL -#ifdef MEMORY_POOL + for (int i = 0; i < node->childrenSize; i++) { doMove(node->children[i]->move); -#else - doMove(child->move); -#endif // MEMORY_POOL #ifdef DEAL_WITH_HORIZON_EFFECT // 克服“水平线效应”: 若遇到吃子,则搜索深度增加 @@ -747,12 +704,7 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no #endif /* DEEPER_IF_ONLY_ONE_LEGAL_MOVE */ // 递归 Alpha-Beta 剪枝 - -#ifdef MEMORY_POOL value = search(depth - 1 + epsilon, alpha, beta, node->children[i]); -#else - value = search(depth - 1 + epsilon, alpha, beta, child); -#endif // MEMORY_POOL undoMove(); @@ -822,22 +774,12 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no // 删除“孙子”节点,防止层数较深的时候节点树太大 #ifndef DONOT_DELETE_TREE -#ifdef MEMORY_POOL for (int i = 0; i < node->childrenSize; i++) { for (int j = 0; j < node->children[i]->childrenSize; j++) { deleteTree(node->children[i]->children[j]); } node->children[i]->childrenSize = 0; } - assert(node->childrenSize != 0); -#else // MEMORY_POOL - for (auto child : node->children) { - for (auto grandChild : child->children) { - deleteTree(grandChild); - } - child->children.clear(); - } -#endif // MEMORY_POOL #endif // DONOT_DELETE_TREE #ifdef IDS_SUPPORT @@ -878,11 +820,7 @@ const char* AIAlgorithm::bestMove() vector bestMoves; size_t bestMovesSize = 0; -#ifdef MEMORY_POOL if (!root->childrenSize) { -#else - if ((root->children).empty()) { -#endif return "error!"; } @@ -891,11 +829,7 @@ const char* AIAlgorithm::bestMove() int i = 0; string moves = "moves"; -#ifdef MEMORY_POOL for (int j = 0; j < root->childrenSize; j++) { -#else - for (auto child : root->children) { -#endif if (root->children[j]->value == root->value #ifdef SORT_CONSIDER_PRUNED && !root->children[j]->pruned @@ -916,11 +850,7 @@ const char* AIAlgorithm::bestMove() if (options.getLearnEndgameEnabled()) { bool isMostWeak = true; // 是否明显劣势 -#ifdef MEMORY_POOL for (int j = 0; j < root->childrenSize; j++) { -#else - for (auto child : root->children) { -#endif if ((side == PLAYER_BLACK && root->children[j]->value > -VALUE_STRONG) || (side == PLAYER_WHITE && root->children[j]->value < VALUE_STRONG)) { isMostWeak = false; @@ -943,11 +873,7 @@ const char* AIAlgorithm::bestMove() if (options.getGiveUpIfMostLose() == true) { bool isMostLose = true; // 是否必败 -#ifdef MEMORY_POOL for (int j = 0; j < root->childrenSize; j++) { -#else - for (auto child : root->children) { -#endif if ((side == PLAYER_BLACK && root->children[j]->value > -VALUE_WIN) || (side == PLAYER_WHITE && root->children[j]->value < VALUE_WIN)) { isMostLose = false; @@ -962,11 +888,7 @@ const char* AIAlgorithm::bestMove() } } -#ifdef MEMORY_POOL for (int j = 0; j < root->childrenSize; j++) { -#else - for (auto child : root->children) { -#endif if (root->children[j]->value == root->value) { bestMoves.push_back(root->children[j]); } @@ -976,11 +898,8 @@ const char* AIAlgorithm::bestMove() if (bestMovesSize == 0) { loggerDebug("Not any child value is equal to root value\n"); -#ifdef MEMORY_POOL + for (int j = 0; j < root->childrenSize; j++) { -#else - for (auto child : root->children) { -#endif bestMoves.push_back(root->children[j]); } } diff --git a/src/ai/search.h b/src/ai/search.h index a649950a..1b71067d 100644 --- a/src/ai/search.h +++ b/src/ai/search.h @@ -42,10 +42,7 @@ #include "hashmap.h" #include "endgame.h" #include "types.h" - -#ifdef MEMORY_POOL #include "memmgr.h" -#endif using namespace std; using namespace CTSL; @@ -70,12 +67,9 @@ public: bool pruned { false }; // 是否在此处剪枝 #endif -#ifdef MEMORY_POOL struct Node *children[NODE_CHILDREN_SIZE]; int childrenSize { 0 }; -#else - vector children; // 子节点列表 -#endif + struct Node* parent {nullptr}; // 父节点 player_t sideToMove {PLAYER_NOBODY}; // 此着是谁下的 (目前仅调试用) @@ -142,9 +136,7 @@ public: } }; -#ifdef MEMORY_POOL MemoryManager memmgr; -#endif public: AIAlgorithm(); @@ -176,11 +168,9 @@ public: #endif // 比较函数 -#ifdef MEMORY_POOL static bool nodeLess(const Node *first, const Node *second); static bool nodeGreater(const Node *first, const Node *second); static int nodeCompare(const Node *first, const Node *second); -#endif // MEMORY_POOL #ifdef ENDGAME_LEARNING bool findEndgameHash(hash_t hash, Endgame &endgame);