Best allocator for fixed size heap allocation

What’s the best allocator if I allocate in the heap once and the size is fixed?

std.heap.FixedBufferAllocator ?

The FixedBufferAllocator is indeed fixed, but in most cases, you are going to pass a stack-allocated buffer to it.

To allocate in the heap just once, I’m not sure which is better, IMO the page_allocator could be a good fit, but you have to benchmark it against other allocators to measure.

1 Like

As a rule of thumb, if you don’t know for sure what’s best, you should default to GeneralPurposeAllocator.

2 Likes

Oh, yeah. That’s right. Actually thank you for the clarification. I was wondering one day why I couldn’t allocate a “big” buffer and the answer was just there :sweat_smile: (stack based).

So yeah, if it needs to be purely heap based, the page allocator to create the buffer. In the “memory allocation” video they said it’s probably the slowest (they don’t talk about the General Purpose yet) but I but that it’s as performant as the General Purpose (if not more). The only “problem” is that it returns a full page on each call which may be too big for some purposes.

1 Like

The page_allocator is a building block for other allocators, it requests a entire memory page from the operating system. My guess is that for a single allocation, it could avoid some of the logic performed by other allocators. But, really? I would rather use GeneralPurposeAllocator.

2 Likes