Skip to content

Commit

Permalink
[perf] petty code with GElement::checkSuitable
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Sep 5, 2023
1 parent 5cae809 commit 9c92b3d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 27 deletions.
11 changes: 11 additions & 0 deletions src/GraphCtrl/GraphElement/GElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,15 @@ CStatus GElement::getAsyncResult() {
CGRAPH_FUNCTION_END
}


CStatus GElement::checkSuitable() {
CGRAPH_FUNCTION_BEGIN

// 包含异步执行的逻辑,不可以loop超过1次
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION((loop_ > CGRAPH_DEFAULT_LOOP_TIMES && this->isAsync()), \
"[" + name_ + "] can set loop = 1 only for the reason of async run")

CGRAPH_FUNCTION_END
}

CGRAPH_NAMESPACE_END
6 changes: 6 additions & 0 deletions src/GraphCtrl/GraphElement/GElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ class GElement : public GElementObject,
*/
CStatus getAsyncResult();

/**
* 判断当前节点,是否符合执行逻辑。主要用于init的早期阶段
* @return
*/
virtual CStatus checkSuitable();

private:
/** 状态相关信息 */
CBool done_ { false }; // 判定被执行结束
Expand Down
1 change: 1 addition & 0 deletions src/GraphCtrl/GraphElement/GElementManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ CStatus GElementManager::calcMaxParaSize(CSize& size) {
size = op.getMaxParaSize(manager_elements_);
} else {
// 遇到不可以解析的情况,将size赋值为0,告知上游
size = 0;
status = CErrStatus("cannot calculate max parallel size within groups");
}
CGRAPH_FUNCTION_END
Expand Down
9 changes: 6 additions & 3 deletions src/GraphCtrl/GraphElement/GElementRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ CStatus GElementRepository::init() {
async_elements_.clear(); // 每次记得清空这里。因为每次init之后,都可能不一样
for (auto& element : elements_) {
/**
* 以下几种情况,需要到最后,再确认是否执行ok的
* 1. 异步节点信息
* 2. 包含超时逻辑的信息
* 1. 查验element是否为空
* 2. 查验配置信息是否正确
* 3. 记录异步节点的信息
*/
CGRAPH_ASSERT_NOT_NULL(element)
status = element->checkSuitable();
CGRAPH_FUNCTION_CHECK_STATUS

if (element->isAsync()) {
async_elements_.emplace(element);
}
Expand Down
4 changes: 2 additions & 2 deletions src/GraphCtrl/GraphElement/GGroup/GSome/GSome.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class GSome : public GGroup {

CStatus addElement(GElementPtr element) override;

CStatus init() final;

CStatus run() final;

CBool isSerializable() final;
Expand All @@ -34,6 +32,8 @@ class GSome : public GGroup {

CBool isHold() final;

CStatus checkSuitable() final;

CGRAPH_NO_ALLOWED_COPY(GSome)

private:
Expand Down
36 changes: 18 additions & 18 deletions src/GraphCtrl/GraphElement/GGroup/GSome/GSome.inl
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,6 @@ CStatus GSome<TriggerNum>::addElement(GElementPtr element) {
}


template<CInt TriggerNum>
CStatus GSome<TriggerNum>::init() {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_NOT_NULL(thread_pool_)
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION((CGRAPH_DEFAULT_LOOP_TIMES != loop_), "GSome cannot set loop > 1.")
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION((0 >= TriggerNum), "trigger num must bigger than 0.")
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION((group_elements_arr_.size() < TriggerNum), \
"this GSome need at least [" + std::to_string(TriggerNum) + "] element.")
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(std::any_of(group_elements_arr_.begin(), group_elements_arr_.end(), [](GElementPtr ptr) {
return !ptr->isAsync();
}), "GSome contains async node only.")

status = GGroup::init();

CGRAPH_FUNCTION_END
}


template<CInt TriggerNum>
CStatus GSome<TriggerNum>::run() {
CGRAPH_FUNCTION_BEGIN
Expand Down Expand Up @@ -128,6 +110,24 @@ CBool GSome<TriggerNum>::isHold() {
return false;
}


template<CInt TriggerNum>
CStatus GSome<TriggerNum>::checkSuitable() {
CGRAPH_FUNCTION_BEGIN
status = GElement::checkSuitable();
CGRAPH_FUNCTION_CHECK_STATUS

CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION((CGRAPH_DEFAULT_LOOP_TIMES != loop_), "GSome cannot set loop > 1.")
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION((0 >= TriggerNum), "trigger num must bigger than 0.")
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION((group_elements_arr_.size() < TriggerNum), \
"this GSome need at least [" + std::to_string(TriggerNum) + "] element.")
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(std::any_of(group_elements_arr_.begin(), group_elements_arr_.end(), [](GElementPtr ptr) {
return !ptr->isAsync();
}), "GSome contains async node only.")

CGRAPH_FUNCTION_END
}

CGRAPH_NAMESPACE_END

#endif //CGRAPH_GSOME_INL
5 changes: 3 additions & 2 deletions src/GraphCtrl/GraphParam/GParam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@

CGRAPH_NAMESPACE_BEGIN

CStatus GParam::getBacktrace(std::vector<std::string>& trace) {
CStatus GParam::getBacktrace(std::vector<std::string>& traces) {
CGRAPH_FUNCTION_BEGIN
if (likely(!backtrace_enable_)) {
// 非问题排查或信息展示场景,更倾向不开启此功能
CGRAPH_RETURN_ERROR_STATUS("backtrace no enable.")
}

backtrace_lock_.lock();
backtrace_.getUniqueArray(trace);
traces.clear();
backtrace_.getUniqueArray(traces);
backtrace_lock_.unlock();

CGRAPH_FUNCTION_END
Expand Down
4 changes: 2 additions & 2 deletions src/GraphCtrl/GraphParam/GParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class GParam : public GParamObject {

/**
* 获取参数的调用栈信息
* @param trace
* @param traces
* @return
*/
CStatus getBacktrace(std::vector<std::string>& trace);
CStatus getBacktrace(std::vector<std::string>& traces);

/**
* 添加trace信息
Expand Down

0 comments on commit 9c92b3d

Please sign in to comment.