Skip to content

Commit

Permalink
[perf] optimize template function return value.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Aug 30, 2024
1 parent 268d9c3 commit fd0b0a2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
30 changes: 15 additions & 15 deletions src/GraphCtrl/GraphPipeline/GPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class GPipeline : public GPipelineObject,
*/
template<typename TNode, typename ...Args,
c_enable_if_t<std::is_base_of<GNode, TNode>::value, int> = 0>
GNodePtr createGNode(const GNodeInfo &info, Args&&... args);
TNode* createGNode(const GNodeInfo &info, Args&&... args);

/**
* 根据传入的信息,创建node节点
Expand All @@ -111,12 +111,12 @@ class GPipeline : public GPipelineObject,
* @param args
* @return
*/
template<typename T, typename ...Args,
c_enable_if_t<std::is_base_of<GNode, T>::value, int> = 0>
GNodePtr createGNode(const GElementPtrSet& dependence = std::initializer_list<GElementPtr>(),
const std::string& name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES,
Args&&... args);
template<typename TNode, typename ...Args,
c_enable_if_t<std::is_base_of<GNode, TNode>::value, int> = 0>
TNode* createGNode(const GElementPtrSet& dependence = std::initializer_list<GElementPtr>(),
const std::string& name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES,
Args&&... args);

/**
* 根据传入的信息,创建Group信息
Expand All @@ -127,12 +127,12 @@ class GPipeline : public GPipelineObject,
* @param loop
* @return
*/
template<typename T,
c_enable_if_t<std::is_base_of<GGroup, T>::value, int> = 0>
GGroupPtr createGGroup(const GElementPtrArr &elements,
const GElementPtrSet &dependElements = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);
template<typename TGroup,
c_enable_if_t<std::is_base_of<GGroup, TGroup>::value, int> = 0>
TGroup* createGGroup(const GElementPtrArr &elements,
const GElementPtrSet &dependElements = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);

/**
* 在图中注册一个 GElement信息
Expand Down Expand Up @@ -177,8 +177,8 @@ class GPipeline : public GPipelineObject,
template<typename TNode,
c_enable_if_t<std::is_base_of<GNode, TNode>::value, int> = 0>
TNode* registerGNode(const GElementPtrSet &dependElements = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);

/**
* 注册一个 node
Expand Down
26 changes: 13 additions & 13 deletions src/GraphCtrl/GraphPipeline/GPipeline.inl
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ CStatus GPipeline::registerGElement(GCoordinatorPPtr<SIZE> coordinatorRef,

template<typename TNode, typename ...Args,
c_enable_if_t<std::is_base_of<GNode, TNode>::value, int>>
GNodePtr GPipeline::createGNode(const GNodeInfo &info, Args&&... args) {
TNode* GPipeline::createGNode(const GNodeInfo &info, Args&&... args) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_INIT_THROW_ERROR(false)

GNodePtr node = new(std::nothrow) TNode(std::forward<Args &&>(args)...);
auto* node = new(std::nothrow) TNode(std::forward<Args &&>(args)...);
CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(node)
status = node->addElementInfo(info.dependence_, info.name_, info.loop_);
CGRAPH_THROW_EXCEPTION_BY_STATUS(status)
Expand All @@ -130,21 +130,21 @@ GNodePtr GPipeline::createGNode(const GNodeInfo &info, Args&&... args) {
}


template<typename T, typename ...Args,
c_enable_if_t<std::is_base_of<GNode, T>::value, int>>
GNodePtr GPipeline::createGNode(const GElementPtrSet& dependence, const std::string& name,
template<typename TNode, typename ...Args,
c_enable_if_t<std::is_base_of<GNode, TNode>::value, int>>
TNode* GPipeline::createGNode(const GElementPtrSet& dependence, const std::string& name,
CSize loop, Args&&... args) {
const GNodeInfo& info = GNodeInfo(dependence, name, loop);
return createGNode<T>(info, std::forward<Args &&>(args)...);
return createGNode<TNode>(info, std::forward<Args &&>(args)...);
}


template<typename T,
c_enable_if_t<std::is_base_of<GGroup, T>::value, int>>
GGroupPtr GPipeline::createGGroup(const GElementPtrArr &elements,
const GElementPtrSet &dependElements,
const std::string &name,
CSize loop) {
template<typename TGroup,
c_enable_if_t<std::is_base_of<GGroup, TGroup>::value, int>>
TGroup* GPipeline::createGGroup(const GElementPtrArr &elements,
const GElementPtrSet &dependElements,
const std::string &name,
CSize loop) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_INIT_THROW_ERROR(false)

Expand All @@ -156,7 +156,7 @@ GGroupPtr GPipeline::createGGroup(const GElementPtrArr &elements,
[](GElementPtr element) { return (nullptr == element); }),
"createGGroup dependElements have nullptr.")

GGroupPtr group = CGRAPH_SAFE_MALLOC_COBJECT(T)
auto* group = CGRAPH_SAFE_MALLOC_COBJECT(TGroup)
for (GElementPtr element : elements) {
status += group->addElement(element);
element->belong_ = group; // 从属于这个group的信息
Expand Down
3 changes: 2 additions & 1 deletion test/Performance/test-performance-04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void test_performance_04() {
GElementPtrSet beforeLayer {};
for (int i = 0; i < layer; i++) {
for (int j = 0; j < nodePerLayer; j++) {
auto* ptr = pipeline->registerGNode<TestAdd1GNode>(beforeLayer);
GElementPtr ptr = nullptr;
pipeline->registerGElement<TestAdd1GNode>(&ptr, beforeLayer);
curLayer.insert(ptr);
}

Expand Down

0 comments on commit fd0b0a2

Please sign in to comment.