std::realloc
is dangerous in c++ if the malloc'd memory contains non-pod types. It seems the only problem is that std::realloc
wont call the type destructors if it cannot grow the memory in situ.
A trivial work around would be a try_realloc
function. Instead of malloc'ing new memory if it cannot be grown in situ, it would simply return false. In which case new memory could be allocated, the objects copied (or moved) to the new memory, and finally the old memory freed.
This seems supremely useful. std::vector
could make great use of this, possibly avoiding all copies/reallocations.
preemptive flame retardant: Technically, that is same Big-O performance, but if vector growth is a bottle neck in your application a x2 speed up is nice even if the Big-O remains unchanged.
BUT, I cannot find any c api that works like a try_realloc
.
Am I missing something? Is try_realloc
not as useful as I imagine? Is there some hidden bug that makes try_realloc
unusable?
Better yet, Is there some less documented API that performs like try_realloc
?
NOTE: I'm obviously, in library/platform specific code here. I'm not worried as try_realloc
is inherently an optimization.
Update:
Following Steve Jessops comment's on whether vector
would be more efficient using realloc I wrote up a proof of concept to test. The realloc-vector
simulates a vector's growth pattern but has the option to realloc instead. I ran the program up to a million elements in the vector.
For comparison a vector
must allocate 19 times while growing to a million elements.
The results, if the realloc-vector
is the only thing using the heap the results are awesome, 3-4 allocation while growing to the size of million bytes.
If the realloc-vector
is used alongside a vector
that grows at 66% the speed of the realloc-vector
The results are less promising, allocating 8-10 times during growth.
Finally, if the realloc-vector
is used alongside a vector
that grows at the same rate, the realloc-vector
allocates 17-18 times. Barely saving one allocation over the standard vector behavior.
I don't doubt that a hacker could game allocation sizes to improve the savings, but I agree with Steve that the tremendous effort to write and maintain such an allocator isn't work the gain.
vector
generally grows in large increments. You can't do that repeatedly without relocating, unless you carefully arrange things so that there's a large extent of free addresses just above the internal buffer of the vector (which in effect requires assigning whole pages, because obviously you can't have other allocations later on the same page).
So I think that in order to get a really good optimization here, you need more than a "trivial workaround" that does a cheap reallocation if possible - you have to somehow do some preparation to make it possible, and that preparation costs you address space. If you only do it for certain vectors, ones that indicate they're going to become big, then it's fairly pointless, because they can indicate with reserve()
that they're going to become big. You can only do it automatically for all vectors if you have a vast address space, so that you can "waste" a big chunk of it on every vector.
As I understand it, the reason that the Allocator
concept has no reallocation function is to keep it simple. If std::allocator
had a try_realloc
function, then either every Allocator would have to have one (which in most cases couldn't be implemented, and would just have to return false always), or else every standard container would have to be specialized for std::allocator
to take advantage of it. Neither option is a great Allocator interface, although I suppose it wouldn't be a huge effort for implementers of almost all Allocator classes just to add a do-nothing try_realloc
function.
If vector
is slow due to re-allocation, deque
might be a good replacement.