dilshan.dev
Systems2023

MicroCache Allocator

Lock-free jemalloc-inspired custom memory allocator in C++.

Throughput Boost4.2x vs Glibc
Cache Line Alignment64-byte True
Lock Contention0% Free-List
P99 Alloc Latency8.4ns

System Overview & Motivation

MicroCache Allocator is a specialized C++ memory management subsystem engineered to replace standard `malloc`/`free` in high-throughput database caching layers. Using thread-local size-class arenas and lock-free freelists, it completely eliminates lock contention across high-core-count server CPUs.

Key Architectural Highlights

  • Thread-local slab allocation arenas eliminating multithreaded mutex contention
  • Segregated size-classes aligned to 64-byte CPU cache lines avoiding false sharing
  • Lock-free atomic freelists for cross-thread memory deallocation
  • Virtual memory management leveraging Linux `mmap` with transparent huge pages
  • Benchmark showing 4.2x faster allocation throughput compared to standard glibc malloc

Technical Architecture & Data Pipeline

Each thread maintains private size-class bins for allocations under 4KB. Deallocations by other threads push nodes into an atomic lock-free queue, reclaimed during the owning thread's next allocation cycle.

Production Challenges & Edge Cases Solved

Preventing cross-thread memory return overhead and avoiding lock convoying. Solved using Treiber stacks and atomic pointer exchange operations.

Technology Stack & Dependencies

Low-Level Kernel

Linux mmapTransparent Huge PagesVirtual Memory

Concurrency

Atomic CASLock-Free Treiber StackMemory Fences

Language

Modern C++20GTest BenchmarksValgrind/ASan