5 ms·
Then you might need std::vector::reserve() instead.
by andreyv 10y ago
Then you might need std::vector::reserve() instead.
- brucedawson 10y agoBut reserve() doesn't work if you are going to fread() into the buffer or otherwise fully initialize it. std::vector<char> basically requires you to double-initialize the buffer if you pass it to a function that just needs a raw pointer to fill, which can be an issue in some high-performance contexts. I hit this in a game server where a few percent of CPU time (that was enough to count as low-hanging-fruit) was being spent on memset inside of resize().
- honkhonkpants 10y agoWhat was the solution? Using a custom allocator?
- deadmutex 10y agoI am not OP, but you could get around it by having a default constructor that is empty. This might've not been possible if you need a different default constructor that initializes values.
- gpderetta 10y agoUsing a custom allocator is the standard-blessed solution for this problem, although it can be inconvenient.
- someguydave 10y agoYes you could use a custom allocator except that changes the type of the std::vector - making a large ripple in our code base. I have not solved it yet. The best solution - don't use std::vector for anything critical.
- brucedawson 10y agoIn my case the fix was to not allocate so much space. The vector was always being resized to 64 KiB. After considering lots of other options I decided that the most pragmatic fix was just a smaller constant to the vector constructor. I don't remember why the other options weren't practical.
- andreyv 10y agoA simple new int[] will work. It doesn't initialize the array (unlike the new int[]() version). You can use it with std::unique_ptr<> to simplify memory management.
- SeanBoocock 10y agoThis is true but at least with the current standard you will want to define a custom deleter with your unique_ptr to implement "delete [] data" vs the default behavior of "delete data".
- deleted 10y ago[deleted]
- someguydave 10y agoYes I am in proecisely this situation. It made me realize how political the c++ std library authorship is.