stack: 增加 Stack 类取代 std::stack 并且在拷贝时使用 memcpy 提高效率
自对弈时长由66秒缩短到63秒, 提速 4.5%.
This commit is contained in:
parent
306373371c
commit
2918b9a23d
|
@ -72,6 +72,8 @@
|
||||||
|
|
||||||
#define MEMORY_POOL
|
#define MEMORY_POOL
|
||||||
|
|
||||||
|
//#define USE_STD_STACK
|
||||||
|
|
||||||
//#define RAPID_GAME
|
//#define RAPID_GAME
|
||||||
|
|
||||||
#define ENDGAME_LEARNING
|
#define ENDGAME_LEARNING
|
||||||
|
|
|
@ -60,6 +60,7 @@ HEADERS += \
|
||||||
src/base/MemoryPool.h \
|
src/base/MemoryPool.h \
|
||||||
src/base/MemoryPool.tcc \
|
src/base/MemoryPool.tcc \
|
||||||
src/base/misc.h \
|
src/base/misc.h \
|
||||||
|
src/base/stack.h \
|
||||||
src/base/stackalloc.h \
|
src/base/stackalloc.h \
|
||||||
src/base/thread.h \
|
src/base/thread.h \
|
||||||
src/ai/search.h \
|
src/ai/search.h \
|
||||||
|
|
|
@ -451,6 +451,7 @@
|
||||||
<ClInclude Include="src\base\HashNode.h" />
|
<ClInclude Include="src\base\HashNode.h" />
|
||||||
<ClInclude Include="src\base\MemoryPool.h" />
|
<ClInclude Include="src\base\MemoryPool.h" />
|
||||||
<ClInclude Include="src\base\misc.h" />
|
<ClInclude Include="src\base\misc.h" />
|
||||||
|
<ClInclude Include="src\base\stack.h" />
|
||||||
<ClInclude Include="src\base\stackalloc.h" />
|
<ClInclude Include="src\base\stackalloc.h" />
|
||||||
<QtMoc Include="src\base\thread.h" />
|
<QtMoc Include="src\base\thread.h" />
|
||||||
<ClInclude Include="src\base\zobrist.h" />
|
<ClInclude Include="src\base\zobrist.h" />
|
||||||
|
|
|
@ -126,6 +126,9 @@
|
||||||
<ClInclude Include="src\game\location.h">
|
<ClInclude Include="src\game\location.h">
|
||||||
<Filter>game</Filter>
|
<Filter>game</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\base\stack.h">
|
||||||
|
<Filter>base</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<CustomBuild Include="debug\moc_predefs.h.cbt">
|
<CustomBuild Include="debug\moc_predefs.h.cbt">
|
||||||
|
|
|
@ -28,8 +28,12 @@
|
||||||
//#ifdef MEMORY_POOL
|
//#ifdef MEMORY_POOL
|
||||||
//#include "StackAlloc.h"
|
//#include "StackAlloc.h"
|
||||||
//#else
|
//#else
|
||||||
|
#ifdef USE_STD_STACK
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#else
|
||||||
|
#include "stack.h"
|
||||||
|
#endif // USE_STD_STACK
|
||||||
//#endif
|
//#endif
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -223,11 +227,11 @@ private:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// 局面数据栈
|
// 局面数据栈
|
||||||
//#ifdef MEMORY_POOL
|
#ifdef USE_STD_STACK
|
||||||
// StackAlloc<MillGame::Position, MemoryPool<MillGame::Position> > positionStack;
|
|
||||||
//#else
|
|
||||||
stack<Position, vector<Position> > positionStack;
|
stack<Position, vector<Position> > positionStack;
|
||||||
//#endif
|
#else
|
||||||
|
Stack<Position> positionStack;
|
||||||
|
#endif /* USE_STD_STACK */
|
||||||
|
|
||||||
// 标识,用于跳出剪枝算法,立即返回
|
// 标识,用于跳出剪枝算法,立即返回
|
||||||
bool requiredQuit {false};
|
bool requiredQuit {false};
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue