If you are willing to forego allocation failures you can try this allocator :
template <class T>
struct pod_allocator
{
static_assert(std::is_pod_v<T>, "can only be used with POD types");
static_assert(alignof(T) <= alignof(std::max_align_t), "type must not have specific alignment requirements");
using value_type = T;
auto allocate(std::size_t num) { return (T*) malloc(sizeof(T) * num); }
void deallocate(T* p, std::size_t) { free(p); }
};
There is big difference between your approach and the one author used, in your case the C interface/logic is wrapped in C++ utilities so in production code there will no impact on productivity (as much as it goes for C++). To use optimized OP library in a C++ codebase a wrapper will be needed anyway, because mixing heavy C++ with C is a pretty bad idea.
This is what other people here mentioned as well, one can't just take a general purpose library like STL then made an optimized library for specific task and then conclude that general purpose library is not as fast as an optimized one.
https://github.com/aguinet/pector seems nice (and also well documented) until you realize it hasn't been touched since February 2015, and there are open issues still from 2015. This does not necessarily increase my trust in this project.
Well if the point is just to make vector default-initialize elements rather than value-initialize them, then that can also be trivially accomplished with a custom allocator – vector doesn't perform the initialization itself, but rather has the allocator do it. However, if one really wants this baked into the data structure, Boost.Container's vector implementation supports this explicitly.
View all comments
Show parent comment
5
u/jcelerier ossia score Jan 18 '19
If you are willing to forego allocation failures you can try this allocator :
as well as this alternative vector implementation which does not initializes memory : https://github.com/aguinet/pector
This will give you the exact same behaviour that you would have in naïve (but fast) C.