diff options
author | Hannes Rantzsch <[email protected]> | 2022-04-01 09:40:32 +0200 |
---|---|---|
committer | Hannes Rantzsch <[email protected]> | 2022-04-01 09:40:32 +0200 |
commit | 4a0d5116675728be1df91c7ff69510cf8358a66b (patch) | |
tree | 42be957a7ca6757e3a82fbebf4a671fcecefeee5 /src | |
parent | eb766403edf977837999d6ed31523493c62d6259 (diff) |
performance: don't reserve before buffer inserting
Fixes #2920
Calling `reserve` before inserting into a vector with `operator+=`
caused a significant performance degradation when called very often (see
referenced GH issue).
In this case, letting `insert` reserve exponentially more memory when
needed is a better strategy than manually calling `reserve`, which would
need to allocate new memory on every call.
cppreference.com notes that "a function that receives an arbitrary
vector by reference and appends elements to it should usually not call
reserve() on the vector, since it does not know of the vector's usage
characteristics."
Co-authored-by: René Meusel <[email protected]>
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/base/secmem.h | 3 |
1 files changed, 0 insertions, 3 deletions
diff --git a/src/lib/base/secmem.h b/src/lib/base/secmem.h index a5ba5857a..2762d33e6 100644 --- a/src/lib/base/secmem.h +++ b/src/lib/base/secmem.h @@ -79,7 +79,6 @@ std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::vector<T, Alloc2>& in) { - out.reserve(out.size() + in.size()); out.insert(out.end(), in.begin(), in.end()); return out; } @@ -95,7 +94,6 @@ template<typename T, typename Alloc, typename L> std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) { - out.reserve(out.size() + in.second); out.insert(out.end(), in.first, in.first + in.second); return out; } @@ -104,7 +102,6 @@ template<typename T, typename Alloc, typename L> std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<T*, L>& in) { - out.reserve(out.size() + in.second); out.insert(out.end(), in.first, in.first + in.second); return out; } |