Skip to content

Commit

Permalink
[perf] optimize trim function, can trim group.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Sep 2, 2024
1 parent 3a6d690 commit 91593fe
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/GraphCtrl/GraphElement/GElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ CBool GElement::isMacro() const {


CStatus GElement::crashed(const CException& ex) {
(void)(this);
return CStatus(internal::STATUS_CRASH, ex.what());
}

Expand Down Expand Up @@ -375,6 +376,7 @@ CVoid GElement::dump(std::ostream& oss) {


CVoid GElement::dumpEdge(std::ostream& oss, GElementPtr src, GElementPtr dst, const std::string& label) {
(void)(this);
if (src->isGGroup() && dst->isGGroup()) {
// 在group的逻辑中,添加 cluster_ 的信息
oss << 'p' << src << " -> p" << dst << label << "[ltail=cluster_p" << src << " lhead=cluster_p" << dst << "]";
Expand Down
6 changes: 6 additions & 0 deletions src/GraphCtrl/GraphElement/GGroup/GGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ CVoid GGroup::dumpGroupLabelBegin(std::ostream& oss) {


CVoid GGroup::dumpGroupLabelEnd(std::ostream& oss) {
(void)(this);
oss << "}\n";
}

Expand Down Expand Up @@ -109,4 +110,9 @@ CBool GGroup::isSeparate(GElementCPtr a, GElementCPtr b) const {
return false;
}


CSize GGroup::trim() const {
return 0;
}

CGRAPH_NAMESPACE_END
7 changes: 7 additions & 0 deletions src/GraphCtrl/GraphElement/GGroup/GGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ class GGroup : public GElement {
*/
virtual CBool isSeparate(GElementCPtr a, GElementCPtr b) const;

/**
* 剪裁逻辑
* @return
*/
virtual CSize trim() const;

private:
GElementPtrArr group_elements_arr_; // 存放 element的数组

Expand All @@ -71,6 +77,7 @@ class GGroup : public GElement {
friend class GMutable;
template<GMultiConditionType> friend class GMultiCondition;
template<CInt> friend class GSome;
friend class GTrimOptimizer;
};

using GGroupPtr = GGroup *;
Expand Down
9 changes: 9 additions & 0 deletions src/GraphCtrl/GraphElement/GGroup/GRegion/GRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,13 @@ CBool GRegion::isSeparate(GElementCPtr a, GElementCPtr b) const {
return GSeparateOptimizer::checkSeparate(manager_->manager_elements_, a, b);
}


CSize GRegion::trim() const {
CSize result = 0;
if (manager_) {
result = GTrimOptimizer::trim(manager_->manager_elements_);
}
return result;
}

CGRAPH_NAMESPACE_END
3 changes: 3 additions & 0 deletions src/GraphCtrl/GraphElement/GGroup/GRegion/GRegion.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ class GRegion : public GGroup {

CBool isSeparate(GElementCPtr a, GElementCPtr b) const final;

CSize trim() const override;

private:
GElementManagerPtr manager_ = nullptr; // region 内部通过 manager来管理其中的 element 信息

CGRAPH_NO_ALLOWED_COPY(GRegion)

friend class GPipeline;
friend class UAllocator;
friend class GTrimOptimizer;
};

using GRegionPtr = GRegion *;
Expand Down
14 changes: 9 additions & 5 deletions src/GraphCtrl/GraphElement/_GOptimizer/GOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ class GOptimizer : public GElementObject {
int father, int son, int unlink) {
const CSize size = elements.size();
std::vector<std::vector<int>> graph(size, std::vector<int>(size, unlink));
for (auto& path : paths) {
for (int i = 0; i < path.size() - 1; i++) {
if (0 == size || 0 == paths.size()) {
return graph;
}

for (const auto& path : paths) {
for (CSize i = 0; i < path.size(); i++) {
// 这里的 find是一定能找到的。因为path的数据,是从elements中记录的
int height = (int)std::distance(elements.begin(), elements.find(path[i]));
for (int j = i + 1; j < path.size(); j++) {
int column = (int)std::distance(elements.begin(), elements.find(path[j]));
CSize height = std::distance(elements.begin(), elements.find(path[i]));
for (CSize j = i + 1; j < path.size(); j++) {
CSize column = std::distance(elements.begin(), elements.find(path[j]));
graph[height][column] = father;
graph[column][height] = son;
}
Expand Down
17 changes: 12 additions & 5 deletions src/GraphCtrl/GraphElement/_GOptimizer/GTrimOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <iterator>

#include "GOptimizer.h"
#include "../GGroup/GGroupInclude.h"

CGRAPH_NAMESPACE_BEGIN

Expand All @@ -28,12 +29,13 @@ class GTrimOptimizer : public GOptimizer {
auto graph = buildGraph(elements, paths, 1, 0, 0);

for (auto* cur : elements) {
int idx = (int)std::distance(elements.begin(), elements.find(cur));
CSize idx = std::distance(elements.begin(), elements.find(cur));
GElementPtrArr candidates;
for (int i = 0; i < (int)cur->dependence_.size(); i++) {
int x = (int)std::distance(elements.begin(), elements.find(cur->dependence_[i]));
for (int j = i; j < (int)cur->dependence_.size(); j++) {
int y = (int)std::distance(elements.begin(), elements.find(cur->dependence_[j]));
for (CSize i = 0; i < cur->dependence_.size(); i++) {
CSize x = std::distance(elements.begin(), elements.find(cur->dependence_[i]));
for (CSize j = 0; j < cur->dependence_.size(); j++) {
// 这里必须是 n^2 的循环
CSize y = std::distance(elements.begin(), elements.find(cur->dependence_[j]));
if (1 == graph[x][y]) {
graph[x][idx] = 0;
candidates.push_back(cur->dependence_[i]);
Expand All @@ -46,12 +48,17 @@ class GTrimOptimizer : public GOptimizer {
trimNum++;
}
}

if (cur->isGGroup()) {
trimNum += ((GGroupPtr)cur)->trim();
}
}

return trimNum;
}

friend class GElementManager;
friend class GRegion;
};

CGRAPH_NAMESPACE_END
Expand Down

0 comments on commit 91593fe

Please sign in to comment.