Skip to content

khubaibumer/MemPool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MemPool

An ultra-low-latency, header-only C++ memory pool designed specifically for low-latency systems, order execution engines, and feed handlers.

It provides sub-30 nanosecond allocation times, lock-free cross-thread reclamation, zero-allocation custom smart pointers, and pool-backed STL containers out of the box.


What Makes it Fast?

  1. Zero Heap Allocations on Hot Paths: All pool segments are pre-allocated at startup as a contiguous array. No malloc or kernel transitions on the hot path.
  2. True Zero-Copy In-Place Construction: Leverages placement new with parameter-forwarding templates. No temporary aggregate copies are ever made.
  3. Thread-Local Caching: Each thread operates on its own lock-free local pool. Same-thread allocations and releases use zero locks, zero atomic instructions, and zero fences.
  4. Batched Remote Returns (Lock-Free): When a thread releases a block belonging to another thread, it's pushed onto a thread-safe, lock-free stack. The owning thread drains this stack only when its local pool runs dry, avoiding cache-line ping-ponging on every release.
  5. No Per-Node Metadata Overhead: Traditional memory pools store control headers next to each block. We use compiler-sized range arithmetic on the pointer to identify its owning coordinator, retaining high cache density.

Benchmark Metrics (WSL / Ubuntu Release)

Running MemPoolTest under full Release optimizations (-DCMAKE_BUILD_TYPE=Release) outputs the following performance metadata:

=====================================================
       TESTING FLASH::STLALLOCATOR COMPLIANCE
=====================================================
Successfully populated mem::map with custom allocator.
Map value at key 2: 200.2
Successfully populated mem::vector with custom allocator.
Vector element at index 1: 20
Successfully populated mem::deque with custom allocator.
Deque element at index 0 (front): 30
Successfully populated mem::queue with custom allocator.
Queue element at front: 60
=====================================================

=====================================================
      FLASH NEW_MEMPOOL LATENCY & JITTER PROFILE
=====================================================
Metric                   Latency / Value
-----------------------------------------------------
Average:                 16.51 ns
Min:                     15 ns
p50 (Median):            16 ns
p90:                     17 ns
p99:                     22 ns
p99.9:                   44 ns
Max (Peak Tail):         3968 ns
Std Dev (Jitter):        12.98 ns
Micro benchmark Sput:    30.62 million ops/sec
=====================================================

=====================================================
    FLASH NEW_MEMPOOL CROSS-THREAD THROUGHPUT RUN
=====================================================
Processed:  5000000 allocations/deallocations cross-thread
Duration:   701 ms
Throughput: 7.13 million ops/sec
=====================================================

Integration Guide

The library is header-only, fully compilable down to C++11, and natively warns and triggers pre-C++20 compatibility pathways when compiled on legacy compilers.

Modern CMake Integration

You can fetch and link this library directly in your own CMakeLists.txt via FetchContent:

include(FetchContent)
FetchContent_Declare(
        MemPool
        GIT_REPOSITORY https://github.com/khubaibumer/MemPool.git
        GIT_TAG main
)
FetchContent_MakeAvailable(MemPool)

# Links headers, alignments, and sets compiler standard to C++20 (falls back seamlessly if needed)
target_link_libraries(your_execution_engine PRIVATE MemPool::MemPool)

💻 Sample Code

1. Unified Entrypoint & Custom Smart Pointers

To use our custom heap-free raw allocator, mem::unique_ptr, or mem::shared_ptr:

#include <MemPool.h>

struct MarketOrder {
    uint64_t orderId;
    uint32_t price;
    uint32_t quantity;
    char symbol[8];

    // Explicit constructor to enable zero-copy perfect forwarding
    MarketOrder(uint64_t id, uint32_t p, uint32_t qty, const char* sym) noexcept
        : orderId(id), price(p), quantity(qty) {
        for (int i = 0; i < 8; ++i) {
            symbol[i] = sym[i];
            if (sym[i] == '\0') break;
        }
    }
};

void hot_path_run() {
    // 1. Create a modern, pool-backed unique pointer (0 heap allocation!)
    auto order = mem::make_unique<MarketOrder>(1ULL, 2500, 100, "MSFT");

    // 2. Create high-performance shared pointers contiguously inside the pool
    auto sharedOrder = mem::make_shared<MarketOrder>(2ULL, 1250, 50, "AAPL");
    
    // Copying shared_ptr only increments integer counters without any heap allocations!
    auto childRef = sharedOrder;
}

2. High-Performance STL-Containers

We provide pre-aliased template containers under mem:: namespace. These automatically allocate nodes from our high-performance lock-free pool segments:

#include <MemPool.h>

void engine_setup() {
    // A red-black tree map backed entirely by NewMemPool
    mem::map<int, double, std::less<int>, 1024> customMap;
    customMap[1] = 100.1;
    customMap[2] = 200.2;

    // A contiguous vector falling back safely to aligned heap allocations on resizing
    mem::vector<int, 1024> customVector;
    customVector.push_back(10);
    customVector.push_back(20);

    // Dynamic queue using pool-allocated deques under the hood
    mem::queue<int, 1024> customQueue;
    customQueue.push(100);
}

Standalone Build & Benchmark runs

Ensure you compile under Release to configure optimization levels correctly:

# Build standalone benchmarks
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --target MemPool20Test -j4

# Execute Tail-Latency & Cross-thread lock-free benchmarks
./build/MemPoolTest

About

Lockless MemoryPool

Topics

Resources

License

Stars

2 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors