From b5b967ab03fd1a0c8718fb01504ec6a5cb0404d7 Mon Sep 17 00:00:00 2001 From: likepeng Date: Thu, 4 Jul 2024 16:32:14 +0800 Subject: [PATCH] Optimize find --- myframe/actor_context_manager.cpp | 6 +++--- myframe/event_manager.cpp | 5 +++-- myframe/event_manager.h | 10 ++++++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/myframe/actor_context_manager.cpp b/myframe/actor_context_manager.cpp index d9dca96..e48a3cd 100644 --- a/myframe/actor_context_manager.cpp +++ b/myframe/actor_context_manager.cpp @@ -54,12 +54,12 @@ bool ActorContextManager::RegContext(std::shared_ptr ctx) { std::shared_ptr ActorContextManager::GetContext( const std::string& actor_name) { std::shared_lock lk(rw_); - if (ctxs_.find(actor_name) == ctxs_.end()) { + auto p = ctxs_.find(actor_name); + if (p == ctxs_.end()) { LOG(WARNING) << "not found " << actor_name; return nullptr; } - auto ctx = ctxs_[actor_name]; - return ctx; + return p->second; } std::vector ActorContextManager::GetAllActorAddr() { diff --git a/myframe/event_manager.cpp b/myframe/event_manager.cpp index 6db1bb9..97c72aa 100644 --- a/myframe/event_manager.cpp +++ b/myframe/event_manager.cpp @@ -26,10 +26,11 @@ EventManager::~EventManager() { ev_handle_t EventManager::ToHandle(const std::string& name) { std::shared_lock lk(rw_); - if (name_handle_map_.find(name) == name_handle_map_.end()) { + auto p = name_handle_map_.find(name); + if (p == name_handle_map_.end()) { return Event::DEFAULT_EV_HANDLE; } - return name_handle_map_[name]; + return p->second; } bool EventManager::Has(const std::string& name) { diff --git a/myframe/event_manager.h b/myframe/event_manager.h index 91b0b62..da48922 100644 --- a/myframe/event_manager.h +++ b/myframe/event_manager.h @@ -30,20 +30,22 @@ class EventManager final { template std::shared_ptr Get(ev_handle_t h) { std::shared_lock lk(rw_); - if (evs_.find(h) == evs_.end()) { + auto p = evs_.find(h); + if (p == evs_.end()) { return nullptr; } - return std::dynamic_pointer_cast(evs_[h]); + return std::dynamic_pointer_cast(p->second); } template std::shared_ptr Get(const std::string& name) { std::shared_lock lk(rw_); - if (name_handle_map_.find(name) == name_handle_map_.end()) { + auto p = name_handle_map_.find(name); + if (p == name_handle_map_.end()) { return nullptr; } lk.unlock(); - return Get(name_handle_map_[name]); + return Get(p->second); } bool Has(const std::string& name);