Skip to content

Commit

Permalink
[perf] optimize tree shape, timecost 22s->18s
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Jan 5, 2025
1 parent 772818c commit c6c6a95
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ CStatus GDynamicEngine::setup(const GSortedGElementPtrSet& elements) {
/**
* 1. 判断是否是 dag 结构
* 2. 标记数据,比如有多少个结束element等
* 3. 标记哪些数据,是linkable 的
* 3. 计算element的结构类型
* 4. 分析当前dag类型信息
*/
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(!GEngine::isDag(elements),
"it is not a dag struct");
mark(elements);
link(elements);
calcShape(elements);
analysisDagType(elements);
CGRAPH_FUNCTION_END
}
Expand Down Expand Up @@ -125,8 +125,11 @@ CVoid GDynamicEngine::afterElementRun(GElementPtr element) {
element->done_ = true;
if (!element->run_before_.empty() && cur_status_.isOK()) {
if (internal::GElementShape::LINKABLE == element->shape_) {
// 针对linkable 的情况,做特殊判定
process(*(element->run_before_.begin()), true);
} else if (internal::GElementShape::ROOT == element->shape_) {
for (CSize i = 0; i < element->run_before_.size(); i++) {
process(element->run_before_[i], i == element->run_before_.size() - 1);
}
} else {
GElementPtr reserved = nullptr;
for (auto* cur : element->run_before_) {
Expand Down
22 changes: 15 additions & 7 deletions src/GraphCtrl/GraphElement/_GEngine/GEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,33 @@ class GEngine : public GElementObject {
virtual CStatus setup(const GSortedGElementPtrSet& elements) = 0;

/**
* 分析所有的可以设置 linkable 的数据
* 分析当前的element的形状
* @param elements
* @return
*/
CVoid link(const GSortedGElementPtrSet& elements) {
CVoid calcShape(const GSortedGElementPtrSet& elements) {
/**
* 认定图可以连通的判定条件:
* 1,当前元素仅有一个后继
* 2,当前元素的唯一后继,仅有一个前驱
* 3,前后元素绑定机制是一样的
* 认定LINKABLE的判定条件:
* 1,当前元素仅有一个后继
* 2,当前元素的唯一后继,仅有一个前驱
* 3,前后元素绑定机制是一样的
* 认定ROOT的判断条件:
* 1,有后继,且不是LINKABLE
* 2,所有的后继仅有当前一个前驱
*/
linked_size_ = 0;
for (GElementPtr element : elements) {
element->shape_ = internal::GElementShape::NORMAL;
if (1 == element->run_before_.size()
&& 1 == (*element->run_before_.begin())->dependence_.size()
&& element->binding_index_ == (*(element->run_before_.begin()))->binding_index_) {
element->shape_ = internal::GElementShape::LINKABLE;
linked_size_++;
} else if (!element->run_before_.empty()
&& std::all_of(element->run_before_.begin(), element->run_before_.end(),
[](GElementPtr ptr) { return 1 == ptr->dependence_.size();})) {
element->shape_ = internal::GElementShape::ROOT;
} else {
element->shape_ = internal::GElementShape::NORMAL;
}
}
}
Expand Down

0 comments on commit c6c6a95

Please sign in to comment.