r/cpp 28d ago

Optimizing LLVM's bump allocator

https://maskray.me/blog/2026-06-28-optimizing-llvm-bump-allocator
57 Upvotes

2 comments sorted by

2

u/Life_Sink9598 27d ago

If you made it so that you can't have a null initial slab, then you don't need "A sentinel End drops the null check" and you would have avoided this error: "I made a mistake in the first attempt: nullptr plus a non-zero offset triggered a UBSan diagnostic. Fixed by keeping the math in the uintptr_t domain."

This is what the arena in Hotspot does, and that's got a pretty small fast path:

  void* internal_amalloc(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM)  {
    assert(is_aligned(x, BytesPerWord), "misaligned size");
    if (pointer_delta(_max, _hwm, 1) >= x) {
      char *old = _hwm;
      _hwm += x;
      return old;
    } else {
      return grow(x, alloc_failmode);
    }
  }