From 4a0eabb3a65107cbee5e692ade564102e2b2f8aa Mon Sep 17 00:00:00 2001 From: xie xingguo Date: Mon, 18 Feb 2019 15:40:22 +0800 Subject: [PATCH] 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 --- src/osd/OSDMap.cc | 1 + 1 file changed, 1 insertion(+) 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; -- 2.39.5