Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

High-Performance TCMallocator

A modern, high-concurrency custom memory pool in C++ inspired by Google's TCMalloc architecture. This project is designed to eliminate global lock contention and kernel-space transition overheads that plague standard system malloc under high-concurrency, frequent small-object allocation workloads.

By implementing a three-tier caching architecture (ThreadCache, CentralCache, and PageHeap), combined with lock-free hot paths and optimized metadata management, this memory pool achieves extreme throughput and rock-solid stability under heavy loads.


System Architecture

The memory pool adopts a classic three-tiered hierarchy to filter out the vast majority of requests that would otherwise hit the operating system kernel:

 [ Worker Thread 1 ]   [ Worker Thread 2 ]   [ Worker Thread N ]  (16 High-Concurrency Threads)
         │                     │                     │
         ▼                     ▼                     ▼
┌─────────────────┐   ┌─────────────────┐   ┌─────────────────┐
│   ThreadCache   │   │   ThreadCache   │   │   ThreadCache   │  (Lock-free Fast Path, thread_local)
└────────┬────────┘   └────────┬────────┘   └────────┬────────┘
         │ (Batch fetch / return on miss)            │
         └─────────────┬─────────────────────────────┘
                       ▼
             ┌───────────────────┐
             │   CentralCache    │  (Bucket-level mutexes, manages Spans by Size Class)
             └────────┬──────────┘
                      │ (Requests new Spans when exhausted)
                      ▼
             ┌───────────────────┐
             │     PageHeap      │  (Page-level memory management, caching & reuse)
             └────────┬──────────┘
                      │ (Requests raw memory via mmap when empty)
                      ▼
             ┌───────────────────┐
             │   Linux Kernel    │  (Underlying OS memory pages)
             └───────────────────┘

Core Optimization Highlights

  1. Lock-Free Hot Path:
    • Small-object allocations (up to 256 KB) are served directly via thread_local ThreadCaches. Multiple threads operate concurrently without interference, completely bypassing global lock contention.
  2. High-Speed Metadata Indexing (FastPageMap):
    • Replaced slow, lock-heavy std::unordered_map lookups with a two-level fixed-size chunked array (Radix Tree style), achieving O(1) lock-free mapping from page addresses to Span metadata.
  3. Memory Pooling & Reuse (Page Heap Caching):
    • Implements page-level caching. Freed memory is recycled into PageHeap's free span lists instead of being immediately returned via munmap, drastically reducing costly kernel-space context switches.
  4. Extreme Robustness:
    • Verified through 16-thread, 30-minute ultra-high-intensity stress tests, demonstrating zero memory fragmentation buildup and zero memory leaks.

Quantitative Benchmarks

The project is evaluated using Google Benchmark for micro-benchmarks and a custom 16-thread, 30-minute macro stress test. Quantitative results are summarized below:

1. Micro-Benchmark Performance (Threads: 1 vs 8/16)

Workload Scenario Threads Latency / Allocation (Time) Comparison vs System malloc
FixedSmall (32B) 1 Thread 9.67 ns Significantly faster
FixedSmall (32B) 8 Threads 3.23 ns (CPU: 14.1 ns) Zero-lock ThreadCache hit advantage
MixedWorkload 1 Thread 22.5 ns Highly efficient
MixedWorkload 16 Threads 9.07 ns (CPU: 45.5 ns) Outperforms system malloc (~2.4x)

2. 30-Minute Extreme Stress Test Report (16 Threads, 1800 Seconds Duration)

Test Environment: Linux 4-Core CPU / 16 Concurrent Worker Threads / Dynamic Mixed Allocations (16B ~ 256B)

Performance Metric Quantitative Test Result Industrial Assessment
Total Operations (Total Ops) 294,189,133,229 ops (~294 Billion) Sustained high throughput without degradation
Average Throughput (Overall QPS) 163,426,439.30 ops/sec (~163M ops/sec) Exceptional multi-core scalability
Average Allocation Latency Stable between 170 ns ~ 230 ns Ultra-low jitter and deterministic latency
Memory Leaks & Stability 0 Memory Leaks / 0 Crashes Successfully passed 30-minute endurance test

Quick Start & Build

Prerequisites

  • C++17 compatible compiler (GCC 7+)
  • CMake 3.10+
  • Google Benchmark (Optional, for micro-benchmarks)

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages