From: xie xingguo Date: Mon, 18 Feb 2019 07:40:22 +0000 (+0800) Subject: osd/OSDMap: using std::vector::reserve to reduce memory reallocation X-Git-Tag: v12.2.12~55^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=0729a78877eef994d595ba0f411919025ec0b3a4;p=ceph.git osd/OSDMap: using std::vector::reserve to reduce memory reallocation In C++ vectors are dynamic arrays. Vectors are assigned memory in blocks of contiguous locations. When the memory allocated for the vector falls short of storing new elements, a new memory block is allocated to vector and all elements are copied from the old location to the new location. This reallocation of elements helps vectors to grow when required. However, it is a costly operation and time complexity is involved in this step is linear. Try to use std::vector::reserve whenever possible if performance matters. Signed-off-by: xie xingguo (cherry picked from commit 4a0eabb3a65107cbee5e692ade564102e2b2f8aa) --- diff --git a/src/osd/OSDMap.cc b/src/osd/OSDMap.cc index dde5b4aca387..f95eae4b5c77 100644 --- a/src/osd/OSDMap.cc +++ b/src/osd/OSDMap.cc @@ -4345,6 +4345,7 @@ int OSDMap::calc_pg_upmaps( // look for remaps we can un-remap vector>>> candidates; + candidates.reserve(tmp.pg_upmap_items.size()); for (auto& i : tmp.pg_upmap_items) { if (to_skip.count(i.first)) continue;