StackAllocator is not performant and wastes memory on padding
inovachrono opened this issue · comments
Monu Yadav commented
I assume that the project has been abandoned since a some pull requests are pending.
Anyways, I will address a few things here for those coming to this repository in search of knowledge from this code base.
Problems with using "AllocationHeader"
- Wasted Memory:
AllocationHeader
as is used in this repository simply adds overhead in terms of wasted padding memory. Regardless of the fact that the memory(m_start_ptr + m_offset)
is already aligned,AllocationHeader
header wastes as low asalignment
bytes orpadding + alignment
bytes of memory. - Probable Cache Misses: Since the
AllocationHeader
is stored before the memory pointer thatallocate
gives away, there is a probability of cache miss when usingfree
if the memory pulled in does not align with cache boundary. It might be a small cost or a big one depending upon how many timesfree
is called. It's bad since the entire point of a custom allocator is performance with solid metrics of memory usage. - Current Implementation of
AllocationHeader
stores garbage: As has been addressed in issue #8,AllocationHeader
currently stores garbage since it stores the address of a stack allocatedAllocationHeader
that would go out of scope once it goes out of scope i.e. as soon as the allocate function ends
AllocationHeader allocationHeader{padding};
AllocationHeader * headerPtr = (AllocationHeader*) headerAddress;
headerPtr = &allocationHeader; // <---------- allocationHeader would go out of scope
A better way of implementing is to use push/begin constructs that would store the offset of stack and pop/end which would reinstate the stack allocator back to the pushed offset.