DevShiftTeam / AppShift-MemoryPool

A very fast cross-platform memory pool mechanism for C++ built using a data-oriented approach (3 to 24 times faster than regular new or delete, depending on operating system & compiler)
Apache License 2.0
214 stars 25 forks source link
allocations cpp cpp17 memory-allocations memory-pool memorypool

MemoryPool For C++

A very fast cross-platform memory pool mechanism for C++ built using a data-oriented approach. I hope this simple feature will help you increase your software's performance - and there are more projects and features to come under the AppShift library name, wait for it ;)

Table of Contents

Usage

To use the memory pool features you just need to copy the MemoryPool.cpp & MemoryPool.h files to your project. The memory pool structure is AppShift::Memory::MemoryPool. The Memory Pool Is Not Thread Safe - In case of threads it is better to create a memory pool for each thread

Memory scoping

Scoping is a fast way to deallocate many allocations at once. If for example you need to allocate more than once in a given part of the code, and then you deallocate all the allocations that happaned, then you can "scope" all these allocations together. it works the same way as a stack in a function scope.

Macros

There are some helpful macros available to indicate how you want the MemoryPool to manage your memory allocations.

Methodology

The MemoryPool is a structure pointing to the start of a chain of blocks, which size of every block is by default MEMORYPOOL_BLOCK_MAX_SIZE macro (See Macros) or the size passed into the AppShift::Memory::MemoryPool(size) constructor. The MemoryPool is an object holding the necessary functions to work with the a memory pool. What's also good is that you can also access the MemoryPool structure data directly if needed (everything is public).

MemoryPool data (MemoryPool)

The memory pool structure holds meta-data about the memory space that is allocated and stored in the pool.

Memory Block (SMemoryBlockHeader)

Each block contains a block header the size of 48 bytes containing the following information:

When a block is fully filled the MemoryPool creates a new block and relates it to the previous block, and the previous to the current, them uses the new pool as the current block.

Memory Unit (SMemoryUnitHeader)

When allocating a space, MemoryPool creates a SMemoryUnitHeader and moves the blocks offset forward by the header size plus the amount of space requested. The header is 16 bytes long and contains the following data:

Memory Scope (SMemoryScopeHeader)

A scope has it's own structure - it has an offset and a pointer to the starting block of the scope, and also a pointer to the previous scope (parent).

Benchmark

Windows & CLang


About 21-24 times faster than standard new/delete in each test.

Windows & MSVC


About 10-13 times faster than standard new/delete in each test.

MacOS & CLang


About 8-10 times faster than standard new/delete in each test.

About

Contributors - Thank You! :D

A list of people that were kind enough to help:

More to come in later versions

In the next versions I'm planning to add some interesting features:

Star History

Star History Chart