Skip to content

Commit 864e2aa

Browse files
committed
Drop superfluous mutex lock & don't manually unpack std::tuple
1 parent 693d094 commit 864e2aa

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

lib/icinga/dependency-group.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ std::vector<Dependency::Ptr> DependencyGroup::GetDependenciesForChild(const Chec
138138
*/
139139
void DependencyGroup::LoadParents(std::set<Checkable::Ptr>& parents) const
140140
{
141-
std::lock_guard lock(m_Mutex);
142141
for (auto& [compositeKey, children] : m_Members) {
143142
parents.insert(std::get<0>(compositeKey));
144143
}
@@ -203,7 +202,7 @@ void DependencyGroup::RemoveDependency(const Dependency::Ptr& dependency)
203202
/**
204203
* Copy the dependency objects of the current dependency group to the provided dependency group (destination).
205204
*
206-
* @param dest The dependency group to move the dependencies to.
205+
* @param dest The dependency group to copy the dependencies to.
207206
*/
208207
void DependencyGroup::CopyDependenciesTo(const DependencyGroup::Ptr& dest)
209208
{
@@ -274,24 +273,20 @@ String DependencyGroup::GetCompositeKey()
274273
// not achievable using pointers.
275274
using StringTuple = std::tuple<String, String, int, bool>;
276275
std::vector<StringTuple> compositeKeys;
277-
{
278-
std::lock_guard lock(m_Mutex);
279-
for (auto& [compositeKey, _] : m_Members) {
280-
auto [parent, tp, stateFilter, ignoreSoftStates] = compositeKey;
281-
compositeKeys.emplace_back(parent->GetName(), tp ? tp->GetName() : "", stateFilter, ignoreSoftStates);
282-
}
276+
for (auto& [compositeKey, _] : m_Members) {
277+
auto [parent, tp, stateFilter, ignoreSoftStates] = compositeKey;
278+
compositeKeys.emplace_back(parent->GetName(), tp ? tp->GetName() : "", stateFilter, ignoreSoftStates);
283279
}
284280

285281
// IMPORTANT: The order of the composite keys must be sorted to ensure the deterministic hash value.
286282
std::sort(compositeKeys.begin(), compositeKeys.end());
287283

288284
Array::Ptr data(new Array{GetRedundancyGroupName()});
289285
for (auto& compositeKey : compositeKeys) {
290-
auto [parent, tp, stateFilter, ignoreSoftStates] = compositeKey;
291-
data->Add(std::move(parent));
292-
data->Add(std::move(tp));
293-
data->Add(stateFilter);
294-
data->Add(ignoreSoftStates);
286+
// std::apply is used to unpack the composite key tuple and add its elements to the data array.
287+
// It's like manually expanding the tuple into x variables and then adding them one by one to the array.
288+
// See https://en.cppreference.com/w/cpp/language/fold for more information.
289+
std::apply([&data](auto&&... args) { (data->Add(std::move(args)), ...); }, std::move(compositeKey));
295290
}
296291

297292
return PackObject(data);

0 commit comments

Comments
 (0)