diff --git a/README.md b/README.md index 9dd5c27..c7c8419 100644 --- a/README.md +++ b/README.md @@ -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`内容 diff --git a/src/GraphCtrl/GraphElement/GElement.h b/src/GraphCtrl/GraphElement/GElement.h index a9f1264..7413f94 100644 --- a/src/GraphCtrl/GraphElement/GElement.h +++ b/src/GraphCtrl/GraphElement/GElement.h @@ -156,17 +156,6 @@ class GElement : public GElementObject, */ CStatus removeDepend(GElement* element); - /** - * 获取对应的ptr类型 - * @tparam T - * @param ptr - * @param allowEmpty - * @return - */ - template::value, int> = 0> - T* getPtr(CBool allowEmpty = true); - /** * 实现连续注册的语法糖,形如: * (*a)-->b&c; diff --git a/src/GraphCtrl/GraphElement/GElement.inl b/src/GraphCtrl/GraphElement/GElement.inl index da5fb6a..f78dfb5 100644 --- a/src/GraphCtrl/GraphElement/GElement.inl +++ b/src/GraphCtrl/GraphElement/GElement.inl @@ -9,6 +9,8 @@ #ifndef CGRAPH_GELEMENT_INL #define CGRAPH_GELEMENT_INL +#include + #include "GElement.h" CGRAPH_NAMESPACE_BEGIN @@ -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::value, int> > + c_enable_if_t::value, int>> T* GElement::getEParam(const std::string& key) { auto iter = local_params_.find(key); - return dynamic_cast((iter != local_params_.end()) ? iter->second : nullptr); -} - + if (iter == local_params_.end()) { + return nullptr; + } -template::value, int>> -T* GElement::getPtr(CBool allowEmpty) { - T* ptr = dynamic_cast(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(param) : nullptr; } CGRAPH_NAMESPACE_END diff --git a/src/GraphCtrl/GraphParam/GParamManager.cpp b/src/GraphCtrl/GraphParam/GParamManager.cpp index a559635..3bf9ec5 100644 --- a/src/GraphCtrl/GraphParam/GParamManager.cpp +++ b/src/GraphCtrl/GraphParam/GParamManager.cpp @@ -91,6 +91,7 @@ CStatus GParamManager::removeByKey(const std::string& key) { std::vector GParamManager::getKeys() { std::vector keys; CGRAPH_LOCK_GUARD lock(this->mutex_); + keys.reserve(params_map_.size()); for (const auto& iter : params_map_) { keys.emplace_back(iter.first); } diff --git a/src/GraphCtrl/GraphParam/GParamManager.inl b/src/GraphCtrl/GraphParam/GParamManager.inl index 92d25d8..09b2026 100644 --- a/src/GraphCtrl/GraphParam/GParamManager.inl +++ b/src/GraphCtrl/GraphParam/GParamManager.inl @@ -9,6 +9,8 @@ #ifndef CGRAPH_GPARAMMANAGER_INL #define CGRAPH_GPARAMMANAGER_INL +#include + #include "GParamManager.h" CGRAPH_NAMESPACE_BEGIN @@ -18,10 +20,10 @@ templatemutex_); - 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"); } @@ -37,12 +39,17 @@ CStatus GParamManager::create(const std::string& key, CBool backtrace) { template::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(result->second); + /** + * 实测比 return dynamic_cast(iter->second); 快很多 + * dynamic_cast : 当前方案,耗时比约为 10:3 + */ + auto param = iter->second; + return likely(typeid(T) == typeid(*param)) ? static_cast(param) : nullptr; } CGRAPH_NAMESPACE_END