Fast Bitmap Fit: A CPU Cache Line friendly memory allocator for single object allocations

Dhruv Mátáni, Gaurav MenghaniarXiv 2021

Introduction: Bridging the Memory Wall

Modern CPU performance is frequently bottlenecked by the "memory wall"—the significant latency disparity between high-speed CPU caches and main RAM. While L1 and L2 caches offer sub-10ns access times, fetching data from main memory can take 100ns or more, as shown in the table below.

Latency comparison between CPU caches and main memory.

To mitigate this latency, CPUs leverage spatial locality by pre-fetching entire "cache lines" on the assumption that adjacent data will soon be needed. However, applications using small, fixed-size nodes (e.g., linked lists or trees) often suffer from "cache thrashing." Over time, as nodes are allocated and freed, their physical locations in RAM become fragmented. Standard allocators using free-lists often return memory in the order it was freed, further degrading physical proximity and forcing the CPU to jump across memory pages for every node access.

Methodology: The Fast Bitmap Fit Allocator

The Fast Bitmap Fit allocator addresses these issues by using a complete binary tree represented as a flat bitmap to manage memory blocks. This structure prioritizes physical proximity and efficient metadata management.

1. The Tree Structure

The allocator divides a memory block into $N$ slots. A bitmap is constructed where leaf nodes represent these slots (0 for free, 1 for used). Internal nodes represent the logical AND of their children: a 1-bit indicates a full subtree, while a 0-bit indicates at least one available slot. This allows the allocator to quickly skip over full sections of memory.

2. Allocation and Deallocation

Allocation is achieved in $\Theta(\log n)$ time. Starting at the root, the allocator traverses 0-bits down the tree, preferring left children to keep allocations packed, until a leaf is reached.

Binary tree-structured bitmap showing an allocation path to a free leaf node.

Deallocation similarly takes $O(\log n)$ time. When a slot is freed, the leaf bit is reset to 0, and the change propagates upward. Propagation stops as soon as an internal node that was already 0 is encountered, optimizing the process.

3. Allocation with Hint

A key innovation is the "hinted" allocation. Applications can request memory physically "near" an existing node. The allocator attempts to navigate toward the hint address from the root. If it encounters a full subtree (a 1-bit), it backtracks to the nearest available branch, ensuring the returned slot is as physically close to the hint as possible.

Key Insights

  • Metadata Separation: Unlike intrusive list allocators, this design keeps metadata in a separate bitmap. This prevents allocator bookkeeping from polluting the CPU cache during user data access.
  • Logarithmic Search: By structuring the bitmap as a tree, search complexity is reduced from $O(n)$ (linear scan) to $\Theta(\log n)$.
  • Natural Packing: The preference for leftmost slots naturally combats fragmentation, keeping the "active" memory set compact for optimal L1/L2 utilization.
  • Zero Alignment Overhead: Separate metadata allows for returning blocks as small as 1 byte without the need for pointer-sized alignment required by internal free-lists.

Conclusion and Future Work

The Fast Bitmap Fit allocator provides a theoretically sound approach to maintaining cache locality for fixed-size object allocations. It guarantees logarithmic time complexity for core operations while maximizing the reuse of cached memory pages.

While promising, the current design is limited to uniform object sizes and lacks empirical benchmarking against industry-standard allocators like jemalloc. Furthermore, future iterations must address concurrency, potentially through "sharding" the bitmap across multiple CPU cores to prevent lock contention in multi-threaded environments.

Summary updated

BibTeX
@misc{matani2021fast,
  author = {Dhruv Mátáni and Gaurav Menghani},
  title = {{Fast Bitmap Fit: A CPU Cache Line friendly memory allocator for single object allocations}},
  year = {2021},
  eprint = {2110.10357},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2110.10357},
}
Figure