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: v14.1.0~52^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=4a0eabb3a65107cbee5e692ade564102e2b2f8aa;p=ceph-ci.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 --- diff --git a/src/osd/OSDMap.cc b/src/osd/OSDMap.cc index e2c395fc4f0..8446d6e8cb7 100644 --- a/src/osd/OSDMap.cc +++ b/src/osd/OSDMap.cc @@ -4641,6 +4641,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;