stack: 增加 Stack 类取代 std::stack 并且在拷贝时使用 memcpy 提高效率

自对弈时长由66秒缩短到63秒, 提速 4.5%.
This commit is contained in:
Calcitem 2019-10-02 11:25:52 +08:00
parent 306373371c
commit 2918b9a23d
6 changed files with 67 additions and 4 deletions

View File

@ -72,6 +72,8 @@
#define MEMORY_POOL
//#define USE_STD_STACK
//#define RAPID_GAME
#define ENDGAME_LEARNING

View File

@ -60,6 +60,7 @@ HEADERS += \
src/base/MemoryPool.h \
src/base/MemoryPool.tcc \
src/base/misc.h \
src/base/stack.h \
src/base/stackalloc.h \
src/base/thread.h \
src/ai/search.h \

View File

@ -451,6 +451,7 @@
<ClInclude Include="src\base\HashNode.h" />
<ClInclude Include="src\base\MemoryPool.h" />
<ClInclude Include="src\base\misc.h" />
<ClInclude Include="src\base\stack.h" />
<ClInclude Include="src\base\stackalloc.h" />
<QtMoc Include="src\base\thread.h" />
<ClInclude Include="src\base\zobrist.h" />

View File

@ -126,6 +126,9 @@
<ClInclude Include="src\game\location.h">
<Filter>game</Filter>
</ClInclude>
<ClInclude Include="src\base\stack.h">
<Filter>base</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="debug\moc_predefs.h.cbt">

View File

@ -28,8 +28,12 @@
//#ifdef MEMORY_POOL
//#include "StackAlloc.h"
//#else
#ifdef USE_STD_STACK
#include <stack>
#include <vector>
#else
#include "stack.h"
#endif // USE_STD_STACK
//#endif
#include <mutex>
#include <string>
@ -223,11 +227,11 @@ private:
#endif
// 局面数据栈
//#ifdef MEMORY_POOL
// StackAlloc<MillGame::Position, MemoryPool<MillGame::Position> > positionStack;
//#else
#ifdef USE_STD_STACK
stack<Position, vector<Position> > positionStack;
//#endif
#else
Stack<Position> positionStack;
#endif /* USE_STD_STACK */
// 标识,用于跳出剪枝算法,立即返回
bool requiredQuit {false};

52
src/base/stack.h Normal file
View File

@ -0,0 +1,52 @@
/*****************************************************************************
* Copyright (C) 2018-2019 MillGame authors
*
* Authors: liuweilhy <liuweilhy@163.com>
* Calcitem <calcitem@outlook.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef STACK_H
#define STACK_H
template <typename T, size_t size = 128>
class Stack
{
public:
inline void push(const T &obj)
{
p++;
memcpy(arr + p, &obj, sizeof(T));
assert(p < size);
}
inline void pop()
{
p--;
}
inline T &top()
{
return arr[p];
}
private:
int p { -1 };
T arr[size];
};
#endif // STACK_H