I am expecting to be handling a huge number of data records, whereby around 20 uint8_t
keys will have millions of <int, struct>
pairs associated with each of them (ordered by int
). These pairs are rather lightweight at ~10 bytes, and need to be allocated dynamically.
Initially, I was using a std::map<uint8_t, std::vector<int, struct>>
but after studying the overhead associated with vectors, namely the capacity()
in
3 machine words in total +
sizeof(element)
*capacity()
as seen here; capacity()
"typically has room for up to twice the actual number of elements" which is seemingly detrimental.
Instead of a vector, I could use a std::map, however the overhead of ~32 bytes per node also becomes very expensive for such light weight pairs.
I am unfamiliar with Boost and other C++ libraries, so was wondering whether anyone could advise on a solution where I could avoid manual dynamic memory allocation?
Edit : To clarify following a few questions in comments, the struct stored will contain 3 shorts (to start with), and no further data structures. I anticipate the length of the vector
to be no greater than 1.5*10^8, and understand this comes to ~1.4 GiB (thanks @dyp).
I suppose the question is rather, how to manage vector capacity()
such that reallocation through reserve()
is kept to a minimum. I am also unsure of the efficiency of shrink_to_fit()
(C++11)