Skip to content

Commit

Permalink
Optimize find
Browse files Browse the repository at this point in the history
  • Loading branch information
lkpworkspace committed Jul 4, 2024
1 parent ce8d9cd commit b5b967a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions myframe/actor_context_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ bool ActorContextManager::RegContext(std::shared_ptr<ActorContext> ctx) {
std::shared_ptr<ActorContext> ActorContextManager::GetContext(
const std::string& actor_name) {
std::shared_lock<std::shared_mutex> 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<std::string> ActorContextManager::GetAllActorAddr() {
Expand Down
5 changes: 3 additions & 2 deletions myframe/event_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ EventManager::~EventManager() {

ev_handle_t EventManager::ToHandle(const std::string& name) {
std::shared_lock<std::shared_mutex> 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) {
Expand Down
10 changes: 6 additions & 4 deletions myframe/event_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ class EventManager final {
template<typename T>
std::shared_ptr<T> Get(ev_handle_t h) {
std::shared_lock<std::shared_mutex> lk(rw_);
if (evs_.find(h) == evs_.end()) {
auto p = evs_.find(h);
if (p == evs_.end()) {
return nullptr;
}
return std::dynamic_pointer_cast<T>(evs_[h]);
return std::dynamic_pointer_cast<T>(p->second);
}

template<typename T>
std::shared_ptr<T> Get(const std::string& name) {
std::shared_lock<std::shared_mutex> 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<T>(name_handle_map_[name]);
return Get<T>(p->second);
}

bool Has(const std::string& name);
Expand Down

0 comments on commit b5b967a

Please sign in to comment.