]> git.apps.os.sepia.ceph.com Git - ceph.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>
Sun, 5 May 2019 09:34:56 +0000 (17:34 +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>
(cherry picked from commit 4a0eabb3a65107cbee5e692ade564102e2b2f8aa)

src/osd/OSDMap.cc

index dd1106db1185e849cfbc0891928acb6d7aae9f95..ad4ef100a4afd3e89a76ab8f51deff0d2c2b5ccd 100644 (file)
@@ -4497,6 +4497,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;