]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
osd/OSDMap: using std::vector::reserve to reduce memory reallocation
authorxie xingguo <xie.xingguo@zte.com.cn>
Mon, 18 Feb 2019 07:40:22 +0000 (15:40 +0800)
committerxie xingguo <xie.xingguo@zte.com.cn>
Mon, 18 Feb 2019 07:40:22 +0000 (15:40 +0800)
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 <xie.xingguo@zte.com.cn>
src/osd/OSDMap.cc

index e2c395fc4f0da826d6a879bfcbf09f48dae7552b..8446d6e8cb73dffafcd160b354dcf0d4960f8e92 100644 (file)
@@ -4641,6 +4641,7 @@ int OSDMap::calc_pg_upmaps(
       // look for remaps we can un-remap
       vector<pair<pg_t,
         mempool::osdmap::vector<pair<int32_t,int32_t>>>> candidates;
+      candidates.reserve(tmp.pg_upmap_items.size());
       for (auto& i : tmp.pg_upmap_items) {
         if (to_skip.count(i.first))
           continue;