very simple memory pool
c++ / Windows xp~ / Visual Studio 2015~
Layer : VMEM <-> MemoryUnitsGroup_List <-> UnitsGroup (per cpu core) <-> Thread Local Storage
uses internally winapi virtulalloc functions to handle the memory
flexible object pool
unique object pool
https://github.com/lastpenguin/MemoryPool/blob/master/Project_C/SourceCode/Core/UTIL/ObjectPool.h
performance test(1 thread)
My Memory Pool vs LFH vs TBB malloc

performance test(8 thread)
My Memory Pool vs LFH vs TBB malloc

Better performance when used repeatedly
test program path :
https://github.com/lastpenguin/MemoryPool/tree/master/Project_C/SourceCode/x64/Release
test code :
https://github.com/lastpenguin/MemoryPool/blob/master/Project_C/SourceCode/TEST/ConsoleTestProject/test_memorypool.h
atom x5-z8350, 4core, 4GB ram
test loop : 20
object size : 32 ~ 512
random seed : 0
mem size per test : 512MB
write / read test : no
thread 1:
pool 47.5925 sec
lfh 69.9947 sec
tbb 50.1776 sec
thread 4:
pool 21.3557 sec
lfh 31.0175 sec
tbb 21.7671 sec
- ์ค๋ ๋ ์ธ์ดํ
- ๋ฉ๋ชจ๋ฆฌ ๋ ผ๋ฆฌ์ ์ฃผ์, ๋ฌผ๋ฆฌ์ ๊ณต๊ฐ์ ๋จํธํ ๋ฐฉ์ง
- ๋ถ๊ฐ์ ์ธ ๋ฉ๋ชจ๋ฆฌ ์๋ชจ๋น์ฉ์ ์ต์ํ
- ์๋
- ๋ฒ์ฉ์ฑ์ ์ํด ์ฌ์ฉํ์ง ์๋ ๋ฉ๋ชจ๋ฆฌ๋ ๊ฐ๋ฅํ ํด์ ๋ฅผ ์๋
- Visual Studio 2015 C++
- ์ฌ์ฉ์์ C++ ๊ธฐ์ด์ง์
- Visual Studio 2015 C++ ๋๋,
- Visual Studio 2015์ฉ Visual C++ ์ฌ๋ฐฐํฌ ๊ฐ๋ฅ ํจํค์ง
- https://www.microsoft.com/ko-kr/download/details.aspx?id=48145
- Core_x86.dll Windows XP ์ด์
- Core_x64.dll Windows Vista ์ด์
- โป ๋ฌธ์ ์งํฉ : ์ ๋์ฝ๋
Project_C/Library Public Version/Core Library BETA 2.0 ์ ์ฌ์ฉ
์ด๊ธฐํ๋ฅผ ์ฒ๋ฆฌํ cpp ํ์ผ์์ ๋ค์ ํ์ผ์ ํฌํจ(include)
./User/Core_import.h์ด๊ธฐํ
::CORE::Connect_CORE(...)์ข ๋ฃ(๋ช ์์ ์ข ๋ฃ ์์ ์ด ํ์ํ ๋๋ง ์ฌ์ฉ)
::CORE::Disconnect_CORE์ฌ์ฉ์ ์ํด์๋ ๋ค์ ํ์ผ์ ํฌํจ(include)
./User/Core_include.hProject_C/SourceCode/Engine ๋ผ์ด๋ธ๋ฌ๋ฆฌ import ๋๋ ์ฝ๋๋ฅผ ์ฐธ๊ณ
์ธํฐํ์ด์ค๋ ๋ค์ ํ์ผ๋ค์ ์ฐธ๊ณ
Core_Interface.hMemoryPool_Interface.hSystem_Information_Interface.h
๋๋ฒ๊น ์ฉ ๋ก๊ทธ ํ์์ ๋ค์ ํ์ผ์ ์ฐธ๊ณ ํ์ฌ ๊ฐ์ฒด๋ฅผ ์ ์ํ์ฌ, ์ด๊ธฐํ์ ์ธ์คํด์ค๋ฅผ ์ ๋ฌํด์ผ ํจ
LogWriter_Interface.hpLogSystem: ์ค์ํ ์ค๋ฅ๋ฑ์ ๊ธฐ๋กpLogDebug: ์ผ๋ฐ ๋๋ฒ๊น ์ ๋ณด
(๋ค์์ ๋ฐฉ๋ฒ๋ค์ ๋ชจ๋ ํผ์ฉ์ฌ์ฉ์ด ๊ฐ๋ฅํฉ๋๋ค)
#1) ์ด๋ฏธ ์์ฑ๋ ์ฝ๋์ ์ฝ๊ฒ ์ ์ฉํ๋ ๋ฐฉ๋ฒ
๋ฉ๋ชจ๋ฆฌํ์ ์ ์ฉํ๋ ค๋ ๊ฐ์ฒด๋ CMemoryPoolResource๋ฅผ ์์๋ฐ์ต๋๋ค.
โป ์ฃผ์ : ์์ ํด๋์ค๊น์ง ๋ชจ๋ ๋ฉ๋ชจ๋ฆฌํ์ ์ํฅ์ ๋ฐ์ต๋๋ค.
์:)
class CTest1 : public CMemoryPoolResource {
...
};
CTest1* p = new CTest1(...);
delete p;์:)
struct TTest1 : CMemoryPoolResource {
...
};
TTest1* p = new TTest1;
delete p;class CParent : public CMemoryPoolResource {
public:
virtual ~CParent();
...
};
class CChild : public CParent {
...
};
//CParent::__Get_MemoryPool<sizeof(CParent)>()->mFN_Hint_MaxMemorySize_Add(sizeof(CParent)*1000);
//CChild::__Get_MemoryPool<CChild>()->mFN_Hint_MaxMemorySize_Set(sizeof(CChild)*500);
CParent* p = new CChild(...);
delete p;new delete ๋ฅผ ์ด์ฉํ์ฌ ์ฌ์ฉํฉ๋๋ค.
#2) ๊ฐ์ฒด๋จ์์ ๋ํ์ฌ ๋ง์ฝ ์์์ ์ฌ์ฉํ์ง ์๊ณ ํน์ ์ํฉ์๋ง ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด ๋ค์์ ๋งคํฌ๋ก๋ฅผ ์ฌ์ฉํฉ๋๋ค.
CMemoryPoolResource๋ฅผ ์์ ๋ฐ์ ํ์
๋ํ ๊ฐ๋ฅํฉ๋๋ค.
// โ ๋งคํฌ๋ก ๋ฒ์ : ํ ๋น์ / ์๋ฉธ์ ๋น ํธ์ถ
_MACRO_ALLOC__FROM_MEMORYPOOL(Address)
_MACRO_FREE__FROM_MEMORYPOOL(Address)
// โ ๋งคํฌ๋ก ๋ฒ์ : ํ ๋น์ / ์๋ฉธ์ ํธ์ถ
_MACRO_NEW__FROM_MEMORYPOOL(Address, Constructor)
_MACRO_DELETE__FROM_MEMORYPOOL(Address)
์:)
TTest1* p1 = nullptr;
_MACRO_ALLOC__FROM_MEMORYPOOL(p1);
_MACRO_FREE__FROM_MEMORYPOOL(p2);
TTest1* p2 = nullptr;
_MACRO_NEW__FROM_MEMORYPOOL(p1, TTest1(...));
_MACRO_DELETE__FROM_MEMORYPOOL(p1);#3) ๊ฐ๋ณํฌ๊ธฐ ์ฌ์ฉ
๋ง์ฝ ํ ๋นํ๋ ค๋ ํฌ๊ธฐ๊ฐ ๊ฐ๋ณ์ (์๋ฅผ ๋ค์ด ๋ฌธ์์ด ๋ฒํผ) ์ด๋ผ๋ฉด ๋ฉ๋ชจ๋ฆฌํ๊ด๋ฆฌ์์ ์ง์ ์ ๊ทผํ์ฌ ๋ค์์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
IMemoryPool_Manager::mFN_Get_Memory
IMemoryPool_Manager::mFN_Return_Memory
IMemoryPool_Manager::mFN_Get_Memory__AlignedCacheSize // ์บ์๋ผ์ธ ์ ๋ ฌ ๋ฒ์
IMemoryPool_Manager::mFN_Return_Memory__AlignedCacheSize // ์บ์๋ผ์ธ ์ ๋ ฌ ๋ฒ์
// โ ๋งคํฌ๋ก ๋ฒ์ : ํ ๋น์ / ์๋ฉธ์ ๋น ํธ์ถ
// โป malloc / free ๋ฅผ ๋์ ํ๊ธฐ์ ์ ํฉํ ๋ฐฉ๋ฒ์
๋๋ค.
_MACRO_ALLOC__FROM_MEMORYPOOL_GENERIC
_MACRO_FREE__FROM_MEMORYPOOL_GENERIC
์:)
TTest1* p1 = _MACRO_ALLOC__FROM_MEMORYPOOL_GENERIC(sizeof(TTest1));
...
_MACRO_FREE__FROM_MEMORYPOOL_GENERIC(p1);#๊ธฐํ1 : STL Container Allocator
STL Container ์ Allocator ๊ต์ฒด ํ ๊ฒฝ์ฐ ๋ค์์ ์ฌ์ฉ
TAllocator ์ด๊ฒ์ vector ๊ฐ์ ๋งค์ฐ ๊ฐ๋ณ์ ํฌ๊ธฐ ํ ๋น์ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ด ์ข์ต๋๋ค
#๊ธฐํ2 :
๋ง์ฝ ์บ์๋ผ์ธ ํฌ๊ธฐ์ธ 64B๋จ์๋ก ์ ๋ ฌ๋ ์ฃผ์๋ฅผ ์ป๊ณ ์ถ๋ค๋ฉด
๋จ์ํ๊ฒ๋ 64B์ ๋ฐฐ์๋จ์๋ก ์๊ตฌ๋ฅผ ํด๋ ๋ฉ๋๋ค
ํ์ผ MemoryPool_Interface.h์ ์ฌ์ฉ ์ค๋ช
์ ์ฐธ๊ณ ํ์ญ์์ค
Memory Pool Interface
::UTIL::MEM::IMemoryPool
Memory Pool Manager Interface
::UTIL::MEM::IMemoryPool_Manager
Instance
::UTIL::g_pMem
๋ค์์ ์ต์ ํ๋ ๋ฉ๋ชจ๋ฆฌํ์ ๋ฐ์์๊ฐ์ ๋ํญ ์ค์ผ ์ ์์ต๋๋ค
(ํ ๋น/ํด์ ๊ฐ ์ฌ๋ฌ๋ฒ ๋ฐ๋ณต๋๋ ๊ฒฝ์ฐ virtualalloc virtualfree ํธ์ถ ์ต์ )
#1) ํ๋ก๊ทธ๋จ ์ด๊ธฐ๊ณผ์
์ด์ ์คํ์์์ ๊ฐ์ฒด๋ณ ์ต๋ ์ฌ์ฉํฌ๊ธฐ ํ์ธ
#2) ๊ฐ์ฒด๋ณ ์ต๋์ฌ์ฉ ํฌ๊ธฐ๋ฅผ ๋ฉ์๋์ ์ ๋ฌ
IMemoryPool::mFN_Hint_MaxMemorySize_Set๋ฅผ ํตํด ์ ๋ฌ
(โป ํ์ฌ ๋ถ์ ํํ๊ฒ ๋์ํจ)
#3) ํ๋ก๊ทธ๋จ ์ข
๋ฃ์
ํต๊ณ์์ ๋ฉ๋ชจ๋ฆฌํ ์ต๋ํฌ๊ธฐ ์กฐ์ฌ, ์ ์ฅ
IMemoryPool::mFN_Query_Stats
TMemoryPool_Stats::Base::nMaxSize_Pool
๋ฉ๋ชจ๋ฆฌํ ์ฝ๋๋ ํ๋ก์ ํธCore์ ํด๋ UTIL ๋ด์ ์์ต๋๋ค. ๊ทธ์ค์, V2 ํด๋์ ์๋ ๊ฒ์ด ์ต๊ทผ ๋ฒ์ ์ ๋๋ค(2015๋ ๊น์ง ์์ ) ํ์ผ๋น ๋ผ์ธ์๋ ๋ถํ ์ ํ์ง ์์ ์กฐ๊ธ ๋ถํธํ๊ฒ ๋๋์๋ ์์ง๋ง ๊ฐ๊ธ์ ๊ฐ์ฒด๋จ์๋ก namespace๋ก ๊ฐ์ธ๋์ VS์ ์จ๊ธฐ๊ธฐ ๊ธฐ๋ฅ์ ์ด์ฉํ๋ฉด ๋ณด๊ธฐ ๋ถํธํ์ง ์์๊ฒ๋๋ค ์ด๋ฐ์์ผ๋ก์
namespace UTIL{
namespace MEM{
class OBJ1{
}
}
}
namespace UTIL{
namespace MEM{
class OBJ2{
}
}
}์ด ๋ฉ๋ชจ๋ฆฌํ ๋ด๋ถ์ ์ฌ์ฉ๋ ๋ฝ์ ๋ชฝ๋ ์คํ๋ฝ์ ๋๋ค (๋ฌผ๋ก ์คํ ํ์์ ์ ํ์ ๋์ด ์ค๋ ๋์ค์์นญ์ ์๋ํฉ๋๋ค) (์์ ํ๊ณ ์ถ๋ค๋ฉด ์ ๊ธ์ด ์ฌ์ฉ๋ ๋ถ๋ถ์ ์ฌ์ค ๋ง์ง ์์ผ๋ ์ฝ๊ฒ ํ์ค ์ ์์ต๋๋ค)
์ด ๋ฉ๋ชจ๋ฆฌํ์ ๊ฒ์๊ฐ์ ๊ณณ์ ์ฌ์ฉ์ LFH, TBB์ ๋นํด ์ข๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ์ฝํฉ๋๋ค (์๋ง๋์, ๊ณ์ ํด์ LFH, TBB ์ ๋น๊ตํด์๊ฑฐ๋ ์) LFH์ ๋นํด ์ข๋ ๋ค์ํ ํ๋ก ๋ถํ ํ์ต๋๋ค ๊ธฐ๋ณธ ์ฒญํฌ ๋จ์๋ 256KB์ด๋ฉฐ, ์ด๊ฒ์ ๋ค์ ๋ช ๊ฐ์ ๊ทธ๋ฃน์ผ๋ก ๋ถํ ํ์ฌ ์ฌ๋ฌ ์ค๋ ๋ ๊ฒฝ์์ ์ค์ ๋๋ค ์ค์ virtualalloc ์์๋ 256KB * x ๋จ์๋ก ํ๋ฒ์ ๋ ๋ง์ด ํ ๋นํฉ๋๋ค(์ฌ๋ฌ๊ฐ์ ์ฒญํฌ๋ฅผ ํ๋ฒ์ ์์ฑํฉ๋๋ค) ๊ทธ๋ฆฌ๊ณ ์ด ์ฒญํฌ๋ค์ ๊ฐ ๋จ์์ฌ์ด์ฆ๋ฅผ ์ฌ์ฉํ๋ ํ์์ ํ์์ ๋ถํ ํด์ ๊ฐ์ ธ๊ฐ ์ฌ์ฉํฉ๋๋ค ์ฌ๊ธฐ์ ์ ์๋์ฐ ๋ฉ๋ชจ๋ฆฌ ์์ฝ๋จ์์ธ 64KB๊ฐ ์๋ 256KB ๋๊ณ ๋ฌป๋๋ค๋ฉด ์์ ํ ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ์ ์ํด์ ์ ๋๋ค ์ฌ๊ธฐ์ 256KB ์ ๋ ฌ๋ฌธ์ ๋ก ์ข ๊ท์ฐฎ์ ์ฒ๋ฆฌ๋ฅผ ํ๊ตฌ์
๋ง์ฝ ์ฌ์ฉ์๊ฐ ์์ ์ ํ๋ก์ ํธ์ ๋ง๊ฒ๋ ์์ ํ๊ณ ์ถ๋ค๋ฉด ๋ค์ ์คํ๋ ๋์ํธ ํ์ผ์ ์ฐธ๊ณ ํ์ธ์
- ๋ฉ๋ชจ๋ฆฌํ ํ ๋นํฌ๊ธฐ ๊ณ์ฐ.ods
- ๋ฉ๋ชจ๋ฆฌํ ์์ฝ๋จ์ ๊ณ์ฐ.ods
(์ฌ์ค ํผ์ ์ฐ๋ ค ํ๋๋ฐ ๋ผ์ด์ ๋๋ฌธ์ ๋ฉ์ธ์คํธ๋ฆผ์ด 12, 16 ์ค๋ ๋๋ก ๋ฐ๋์ด์ ์คํ์์คํ ํฉ๋๋ค)
MemoryPool_v2/MemoryPoolCore_v2.h
// Release๋ฒ์ ์์ ํ ์คํธ ON //#define _DEF_USING_MEMORYPOOL_DEBUG 1 ์ฃผ์ ํด์ ์ ๋๋ฒ๊ทธ๋ชจ๋๊ฐ ์๋์ฌ๋ ์ถ๋ ฅ์ฐฝ์ ์ ๋ณดํ์
// ํ๋กํ์ผ๋ง ON //#define _DEF_USING_MEMORYPOOL__PERFORMANCE_PROFILING 1 visual studio ์์ ํ๋กํ์ผ๋ง ๋๊ตฌ ์ฌ์ฉ์, ๋ฉ๋ชจ๋ฆฌํ ํจ์/๋ฉ์๋ ์ผ๋ถ๋ ์ธ๋ผ์ธํ๋์ด ์ฌ์ฉ์๋ ์ด๋ฐ ๋ถ๋ถ๋ค์ ํ๋กํ์ผ๋ง ๊ฒฐ๊ณผ๋ฅผ ์ธ๋ฐํ๊ฒ ๋ถ์ํ๊ธฐ ํ๋ค์ด์ง๊ธฐ๋๋ฌธ์ ํ์ํ ๋ถ๋ถ์ ์ธ๋ผ์ธํ๋ฅผ ๋ง๋ ์ต์
์ง๋ฌธ์ lastpenguin83@gmail.com ์ผ๋ก ํ์๊ฑฐ๋ ์ด๊ณณ์ ํ์ญ์์ค
2015๋ ์ดํ๋ก ์์ ํ์ง ์์ ๊ธฐ์ต๋์ง ์๋ ๋ถ๋ถ์ด ๋ง์ต๋๋ค. ์ด ๋ถ๋ถ์ด ์ ์ด๋ ๊ฒ ํ๋ ๊ถ๊ธํ ๋ถ๋ถ์ ๋ค์์ ํ์ผ์ ์ฐธ๊ณ ํด์ฃผ์ธ์
- ๋ฉ๋ชจ๋ฆฌํ ๊ฐ์ .txt (V1)
- ๋ฉ๋ชจ๋ฆฌํ ๊ฐ์ V2.txt

