ConfigItem: Don't add items to the new items vector before committing

This also improves the performance a bit, as we longer have to iterate over the items
and copy them into the new items vector.
This commit is contained in:
Yonas Habteab 2022-06-20 14:55:18 +02:00
parent f7298e85d2
commit 400117e2f6
1 changed files with 10 additions and 5 deletions

View File

@ -433,11 +433,9 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue
<< "Committing " << items.size() << " new items."; << "Committing " << items.size() << " new items.";
#endif /* I2_DEBUG */ #endif /* I2_DEBUG */
for (const auto& ip : items)
newItems.push_back(ip.first);
std::set<Type::Ptr> types; std::set<Type::Ptr> types;
std::set<Type::Ptr> completed_types; std::set<Type::Ptr> completed_types;
int itemsCount {0};
for (const Type::Ptr& type : Type::GetAllTypes()) { for (const Type::Ptr& type : Type::GetAllTypes()) {
if (ConfigObject::TypeInstance->IsAssignableFrom(type)) if (ConfigObject::TypeInstance->IsAssignableFrom(type))
@ -464,7 +462,9 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue
continue; continue;
std::atomic<int> committed_items(0); std::atomic<int> committed_items(0);
upq.ParallelFor(items, [&type, &committed_items](const ItemPair& ip) { std::mutex newItemsMutex;
upq.ParallelFor(items, [&type, &committed_items, &newItems, &newItemsMutex](const ItemPair& ip) {
const ConfigItem::Ptr& item = ip.first; const ConfigItem::Ptr& item = ip.first;
if (item->m_Type != type) if (item->m_Type != type)
@ -479,10 +479,15 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue
} }
committed_items++; committed_items++;
std::unique_lock<std::mutex> lock(newItemsMutex);
newItems.emplace_back(item);
}); });
upq.Join(); upq.Join();
itemsCount += committed_items;
completed_types.insert(type); completed_types.insert(type);
#ifdef I2_DEBUG #ifdef I2_DEBUG
@ -498,7 +503,7 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue
#ifdef I2_DEBUG #ifdef I2_DEBUG
Log(LogDebug, "configitem") Log(LogDebug, "configitem")
<< "Committed " << items.size() << " items."; << "Committed " << itemsCount << " items.";
#endif /* I2_DEBUG */ #endif /* I2_DEBUG */
completed_types.clear(); completed_types.clear();