summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Larkin-York <dan.larkin-york@mongodb.com>2023-02-10 15:47:37 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-02-10 22:47:41 +0000
commit96399bf635e3916f9d5bc87ad2b3876eb89350a9 (patch)
tree6c5bc0fdb143382ba9d003cfd5df1f260394b075
parentced7b068621e891bbcdefc60ad88cd0996f00606 (diff)
downloadmongo-96399bf635e3916f9d5bc87ad2b3876eb89350a9.tar.gz
SERVER-73636 Fix poor allocation pattern in validation reporting
(cherry picked from commit 2d90115a227572cc0f46b309ceb47dda6b740da3)
-rw-r--r--src/mongo/db/catalog/index_consistency.cpp14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/mongo/db/catalog/index_consistency.cpp b/src/mongo/db/catalog/index_consistency.cpp
index 48cba4589de..19258cbe3f2 100644
--- a/src/mongo/db/catalog/index_consistency.cpp
+++ b/src/mongo/db/catalog/index_consistency.cpp
@@ -280,9 +280,21 @@ void KeyStringIndexConsistency::addIndexEntryErrors(OperationContext* opCtx,
// many as possible within memory limits.
using ExtraIt = SimpleBSONObjSet::const_iterator;
std::vector<ExtraIt> extraIndexEntriesBySize;
+ // Since the extra entries are stored in a map of sets, we have to iterate the entries in the
+ // map and sum the size of the sets in order to get the total number. Given that we can have at
+ // most 64 indexes per collection, and the total number of entries could potentially be in the
+ // millions, we expect that iterating the map will be much less costly than the additional
+ // allocations and copies that could result from not calling 'reserve' on the vector.
+ size_t totalExtraIndexEntriesCount =
+ std::accumulate(_extraIndexEntries.begin(),
+ _extraIndexEntries.end(),
+ 0,
+ [](size_t total, const std::pair<IndexKey, SimpleBSONObjSet>& set) {
+ return total + set.second.size();
+ });
+ extraIndexEntriesBySize.reserve(totalExtraIndexEntriesCount);
for (const auto& extraIndexEntry : _extraIndexEntries) {
const SimpleBSONObjSet& entries = extraIndexEntry.second;
- extraIndexEntriesBySize.reserve(extraIndexEntriesBySize.size() + entries.size());
for (auto it = entries.begin(); it != entries.end(); ++it) {
extraIndexEntriesBySize.push_back(it);
}