-
Notifications
You must be signed in to change notification settings - Fork 578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ConfigItem::CommitNewItems(): pre-sort types by their load dependencies once #10003
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ | ||
|
||
#include "base/type.hpp" | ||
#include "base/atomic.hpp" | ||
#include "base/configobject.hpp" | ||
#include "base/debug.hpp" | ||
#include "base/scriptglobal.hpp" | ||
#include "base/namespace.hpp" | ||
#include "base/objectlock.hpp" | ||
#include <algorithm> | ||
#include <functional> | ||
|
||
using namespace icinga; | ||
|
||
|
@@ -32,6 +37,61 @@ INITIALIZE_ONCE_WITH_PRIORITY([]() { | |
Type::Register(type); | ||
}, InitializePriority::RegisterTypeType); | ||
|
||
static std::vector<Type::Ptr> l_SortedByLoadDependencies; | ||
static Atomic l_SortingByLoadDependenciesDone (false); | ||
|
||
INITIALIZE_ONCE_WITH_PRIORITY([] { | ||
auto types (Type::GetAllTypes()); | ||
|
||
types.erase(std::remove_if(types.begin(), types.end(), [](auto& type) { | ||
return !ConfigObject::TypeInstance->IsAssignableFrom(type); | ||
}), types.end()); | ||
|
||
// Depth-first search | ||
std::unordered_set<Type*> unsorted; | ||
std::unordered_set<Type*> visited; | ||
std::vector<Type::Ptr> sorted; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need to temporarily cache the sorted ones elsewhere than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The sorted var and the "atomic" std::swap() at the end (which I consider good practice) has no disadvantages IMAO. |
||
|
||
for (auto type : types) { | ||
unsorted.emplace(type.get()); | ||
} | ||
|
||
std::function<void(Type*)> visit; | ||
visit = ([&visit, &unsorted, &visited, &sorted](Type* type) { | ||
if (unsorted.find(type) == unsorted.end()) { | ||
return; | ||
} | ||
Comment on lines
+61
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If, for whatever reason, you define in the |
||
|
||
VERIFY(visited.emplace(type).second); // Detect cycles in type dependencies | ||
|
||
for (auto dep : type->GetLoadDependencies()) { | ||
visit(dep); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... |
||
} | ||
|
||
unsorted.erase(type); | ||
sorted.emplace_back(type); | ||
}); | ||
|
||
while (!unsorted.empty()) { | ||
visit(*unsorted.begin()); | ||
} | ||
|
||
VERIFY(sorted.size() == types.size()); | ||
|
||
visited.clear(); | ||
|
||
for (auto& t : sorted) { | ||
for (auto dep : t->GetLoadDependencies()) { | ||
VERIFY(visited.find(dep) != visited.end()); | ||
} | ||
|
||
VERIFY(visited.emplace(t.get()).second); | ||
} | ||
|
||
std::swap(sorted, l_SortedByLoadDependencies); | ||
l_SortingByLoadDependenciesDone.store(true); | ||
}, InitializePriority::SortTypes); | ||
|
||
String Type::ToString() const | ||
{ | ||
return "type '" + GetName() + "'"; | ||
|
@@ -72,6 +132,12 @@ std::vector<Type::Ptr> Type::GetAllTypes() | |
return types; | ||
} | ||
|
||
const std::vector<Type::Ptr>& Type::GetAllConfigTypesSortedByLoadDependencies() | ||
{ | ||
VERIFY(l_SortingByLoadDependenciesDone.load()); | ||
return l_SortedByLoadDependencies; | ||
} | ||
|
||
String Type::GetPluralName() const | ||
{ | ||
String name = GetName(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also inline this check into the
visit
callback, since you don't have to remove those unwanted types beforehand.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I need to
VERIFY(sorted.size() == types.size());