From c6c6a95fa196117f38fb589c20852fc907f6d0a7 Mon Sep 17 00:00:00 2001 From: ChunelFeng Date: Sun, 5 Jan 2025 17:16:38 +0800 Subject: [PATCH] [perf] optimize tree shape, timecost 22s->18s --- .../GDynamicEngine/GDynamicEngine.cpp | 9 +++++--- src/GraphCtrl/GraphElement/_GEngine/GEngine.h | 22 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/GraphCtrl/GraphElement/_GEngine/GDynamicEngine/GDynamicEngine.cpp b/src/GraphCtrl/GraphElement/_GEngine/GDynamicEngine/GDynamicEngine.cpp index 19f77a41..7ebd9690 100644 --- a/src/GraphCtrl/GraphElement/_GEngine/GDynamicEngine/GDynamicEngine.cpp +++ b/src/GraphCtrl/GraphElement/_GEngine/GDynamicEngine/GDynamicEngine.cpp @@ -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 } @@ -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_) { diff --git a/src/GraphCtrl/GraphElement/_GEngine/GEngine.h b/src/GraphCtrl/GraphElement/_GEngine/GEngine.h index f00aab70..dbea0e13 100644 --- a/src/GraphCtrl/GraphElement/_GEngine/GEngine.h +++ b/src/GraphCtrl/GraphElement/_GEngine/GEngine.h @@ -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; } } }