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
is holding is terrible, and you cannot use adeque
for whatever reason, then maybe you should change yourvector
over to holdshared_ptr
instances that point to the objects. That way the copy operations will become a lot cheaper. I'm not sure ifunique_ptr
objects can be used in standard containers but that would reduce copying overhead even further. – Headstock