Skip to content

Commit

Permalink
[perf] optimize GParam and EParam get timecost
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Nov 16, 2024
1 parent 8fe83a1 commit f85b9f0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ int main() {
* 优化`event`(事件)机制,异步事件可以等待结束
* 发布 [CGraph-lite](https://github.com/ChunelFeng/CGraph-lite) 项目,提供简单DAG构图和参数传递功能。接口完全兼容,可无缝切换至本项目

[2024.10.27 - v2.6.2 - Chunel]
* 优化参数互斥机制
[2024.11.16 - v2.6.2 - Chunel]
* 优化参数互斥机制和获取性能
* 修复辅助线程异常等待问题
* 更新`tutorial`内容

Expand Down
11 changes: 0 additions & 11 deletions src/GraphCtrl/GraphElement/GElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,6 @@ class GElement : public GElementObject,
*/
CStatus removeDepend(GElement* element);

/**
* 获取对应的ptr类型
* @tparam T
* @param ptr
* @param allowEmpty
* @return
*/
template<typename T,
c_enable_if_t<std::is_base_of<GElement, T>::value, int> = 0>
T* getPtr(CBool allowEmpty = true);

/**
* 实现连续注册的语法糖,形如:
* (*a)-->b&c;
Expand Down
20 changes: 9 additions & 11 deletions src/GraphCtrl/GraphElement/GElement.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef CGRAPH_GELEMENT_INL
#define CGRAPH_GELEMENT_INL

#include <typeinfo>

#include "GElement.h"

CGRAPH_NAMESPACE_BEGIN
Expand Down Expand Up @@ -56,25 +58,21 @@ GElementPtr GElement::addEParam(const std::string& key, T* param) {
T* cur = CGRAPH_SAFE_MALLOC_COBJECT(T);
cur->clone(param);

local_params_[key] = cur; // 写入其中
local_params_[key] = cur;
return this;
}


template<typename T,
c_enable_if_t<std::is_base_of<GElementParam, T>::value, int> >
c_enable_if_t<std::is_base_of<GElementParam, T>::value, int>>
T* GElement::getEParam(const std::string& key) {
auto iter = local_params_.find(key);
return dynamic_cast<T *>((iter != local_params_.end()) ? iter->second : nullptr);
}

if (iter == local_params_.end()) {
return nullptr;
}

template<typename T,
c_enable_if_t<std::is_base_of<GElement, T>::value, int>>
T* GElement::getPtr(CBool allowEmpty) {
T* ptr = dynamic_cast<T *>(this);
CGRAPH_THROW_EXCEPTION_BY_CONDITION(!allowEmpty && !ptr, "change ptr type failed")
return ptr;
auto param = iter->second;
return likely(typeid(T) == typeid(*param)) ? static_cast<T *>(param) : nullptr;
}

CGRAPH_NAMESPACE_END
Expand Down
1 change: 1 addition & 0 deletions src/GraphCtrl/GraphParam/GParamManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ CStatus GParamManager::removeByKey(const std::string& key) {
std::vector<std::string> GParamManager::getKeys() {
std::vector<std::string> keys;
CGRAPH_LOCK_GUARD lock(this->mutex_);
keys.reserve(params_map_.size());
for (const auto& iter : params_map_) {
keys.emplace_back(iter.first);
}
Expand Down
19 changes: 13 additions & 6 deletions src/GraphCtrl/GraphParam/GParamManager.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef CGRAPH_GPARAMMANAGER_INL
#define CGRAPH_GPARAMMANAGER_INL

#include <typeinfo>

#include "GParamManager.h"

CGRAPH_NAMESPACE_BEGIN
Expand All @@ -18,10 +20,10 @@ template<typename T,
CStatus GParamManager::create(const std::string& key, CBool backtrace) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_LOCK_GUARD lock(this->mutex_);
auto result = params_map_.find(key);
if (result != params_map_.end()) {
auto iter = params_map_.find(key);
if (iter != params_map_.end()) {
/* 如果是重复创建,则返回ok;非重复创建(类型不同)则返回err */
auto param = result->second;
auto param = iter->second;
return (typeid(*param).name() == typeid(T).name()) ?
CStatus() : CStatus("create [" + key + "] param duplicate");
}
Expand All @@ -37,12 +39,17 @@ CStatus GParamManager::create(const std::string& key, CBool backtrace) {
template<typename T,
c_enable_if_t<std::is_base_of<GParam, T>::value, int>>
T* GParamManager::get(const std::string& key) {
auto result = params_map_.find(key);
if (result == params_map_.end()) {
const auto& iter = params_map_.find(key);
if (iter == params_map_.end()) {
return nullptr;
}

return dynamic_cast<T *>(result->second);
/**
* 实测比 return dynamic_cast<T *>(iter->second); 快很多
* dynamic_cast<T *> : 当前方案,耗时比约为 10:3
*/
auto param = iter->second;
return likely(typeid(T) == typeid(*param)) ? static_cast<T *>(param) : nullptr;
}

CGRAPH_NAMESPACE_END
Expand Down

0 comments on commit f85b9f0

Please sign in to comment.