From 2ca226250e556a99c25f15c9c517cdcefe1b7d21 Mon Sep 17 00:00:00 2001 From: likepeng Date: Thu, 6 Jun 2024 19:33:13 +0800 Subject: [PATCH] Optimize pointer access2 --- myframe/actor.cpp | 2 +- myframe/actor_context_manager.cpp | 26 ++++++++++++++------------ myframe/app.cpp | 14 ++++++++------ myframe/app.h | 2 +- myframe/common.cpp | 2 +- myframe/event_conn.cpp | 4 ++-- myframe/event_conn_manager.cpp | 6 +++--- myframe/event_manager.cpp | 4 ++-- myframe/event_manager.h | 4 ++-- myframe/mailbox.cpp | 8 ++++---- myframe/worker_context.cpp | 2 +- myframe/worker_context_manager.cpp | 4 ++-- myframe/worker_timer.cpp | 2 +- 13 files changed, 42 insertions(+), 38 deletions(-) diff --git a/myframe/actor.cpp b/myframe/actor.cpp index 6af5a9f..93817e0 100644 --- a/myframe/actor.cpp +++ b/myframe/actor.cpp @@ -80,7 +80,7 @@ bool Actor::Subscribe(const std::string& addr, const std::string& msg_type) { msg->SetType("SUBSCRIBE"); msg->SetDesc(msg_type); auto mailbox = ctx_->GetMailbox(); - mailbox->Send(addr, msg); + mailbox->Send(addr, std::move(msg)); return true; } diff --git a/myframe/actor_context_manager.cpp b/myframe/actor_context_manager.cpp index c2077e3..e99d09b 100644 --- a/myframe/actor_context_manager.cpp +++ b/myframe/actor_context_manager.cpp @@ -34,8 +34,8 @@ void ActorContextManager::DispatchMsg( return; } auto mailbox = ctx->GetMailbox(); - mailbox->Recv(msg); - PushContext(ctx); + mailbox->Recv(std::move(msg)); + PushContext(std::move(ctx)); } bool ActorContextManager::RegContext(std::shared_ptr ctx) { @@ -46,7 +46,7 @@ bool ActorContextManager::RegContext(std::shared_ptr ctx) { return false; } LOG(INFO) << "reg actor " << ctx->GetActor()->GetActorName(); - ctxs_[ctx->GetActor()->GetActorName()] = ctx; + ctxs_[ctx->GetActor()->GetActorName()] = std::move(ctx); return true; } @@ -76,6 +76,9 @@ bool ActorContextManager::HasActor(const std::string& name) { } void ActorContextManager::PrintWaitQueue() { + if (!VLOG_IS_ON(1)) { + return; + } VLOG(1) << "cur wait queue actor:"; auto it = wait_queue_.begin(); while (it != wait_queue_.end()) { @@ -94,7 +97,7 @@ std::shared_ptr ActorContextManager::GetContextWithMsg() { return nullptr; } - std::vector> in_runing_context; + std::list> in_runing_context; std::shared_ptr ret = nullptr; while (!wait_queue_.empty()) { if (wait_queue_.front().expired()) { @@ -104,20 +107,19 @@ std::shared_ptr ActorContextManager::GetContextWithMsg() { auto ctx = wait_queue_.front().lock(); if (ctx->IsRuning()) { wait_queue_.pop_front(); - in_runing_context.push_back(ctx); + VLOG(1) << ctx->GetActor()->GetActorName() + << " is runing, move to wait queue back"; + in_runing_context.push_back(std::move(ctx)); } else { wait_queue_.pop_front(); - ctx->SetRuningFlag(true); ctx->SetWaitQueueFlag(false); - ret = ctx; + ret.swap(ctx); break; } } - for (std::size_t i = 0; i < in_runing_context.size(); ++i) { - VLOG(1) << in_runing_context[i]->GetActor()->GetActorName() - << " is runing, move to wait queue back"; - wait_queue_.push_back(in_runing_context[i]); + if (!in_runing_context.empty()) { + wait_queue_.splice(wait_queue_.end(), in_runing_context); } return ret; } @@ -129,7 +131,7 @@ void ActorContextManager::PushContext(std::shared_ptr ctx) { return; } ctx->SetWaitQueueFlag(true); - wait_queue_.push_back(ctx); + wait_queue_.push_back(std::move(ctx)); PrintWaitQueue(); } diff --git a/myframe/app.cpp b/myframe/app.cpp index 1a3fa2b..b8e4039 100644 --- a/myframe/app.cpp +++ b/myframe/app.cpp @@ -306,9 +306,9 @@ int App::Send(std::shared_ptr msg) { return -1; } poller_->Add(conn); - auto ret = conn->Send(msg); + auto ret = conn->Send(std::move(msg)); poller_->Del(conn); - ev_conn_mgr_->Release(conn); + ev_conn_mgr_->Release(std::move(conn)); return ret; } @@ -320,9 +320,9 @@ const std::shared_ptr App::SendRequest( return nullptr; } poller_->Add(conn); - auto resp = conn->SendRequest(msg); + auto resp = conn->SendRequest(std::move(msg)); poller_->Del(conn); - ev_conn_mgr_->Release(conn); + ev_conn_mgr_->Release(std::move(conn)); return resp; } @@ -434,7 +434,7 @@ void App::DispatchMsg(std::list>* msg_list) { msg_list->size() > warning_msg_size_.load()) << " dispatch msg too many"; for (auto& msg : (*msg_list)) { - DispatchMsg(msg); + DispatchMsg(std::move(msg)); } msg_list->clear(); } @@ -444,7 +444,7 @@ void App::DispatchMsg(std::shared_ptr msg) { VLOG(1) << *msg; /// 处理框架消息 if (msg->GetDst() == MAIN_ADDR) { - ProcessMain(msg); + ProcessMain(std::move(msg)); return; } /// 消息分发 @@ -463,12 +463,14 @@ void App::DispatchMsg(std::shared_ptr msg) { actor_ctx_mgr_->DispatchMsg(msg); } else if (name_list[0] == "event") { if (name_list[1] == "conn") { + // dispatch to event conn auto handle = ev_mgr_->ToHandle(msg->GetDst()); ev_conn_mgr_->Notify(handle, msg); } else { LOG(ERROR) << "Unknown msg " << *msg; } } else if (!node_addr_.empty()) { + // dispatch to node if (node_addr_.substr(0, 5) == "actor") { actor_ctx_mgr_->DispatchMsg(msg, node_addr_); } else if (node_addr_.substr(0, 6) == "worker") { diff --git a/myframe/app.h b/myframe/app.h index 9ad1825..0deaa7e 100644 --- a/myframe/app.h +++ b/myframe/app.h @@ -136,7 +136,7 @@ class MYFRAME_EXPORT App final : public std::enable_shared_from_this { struct CacheMsg { CacheMsg(int c, std::shared_ptr m) : search_count(c) - , msg(m) {} + , msg(std::move(m)) {} int search_count{0}; std::shared_ptr msg{nullptr}; }; diff --git a/myframe/common.cpp b/myframe/common.cpp index 7418e26..cffaabe 100644 --- a/myframe/common.cpp +++ b/myframe/common.cpp @@ -113,7 +113,7 @@ std::vector Common::SplitMsgName(const std::string& name) { std::string item; std::stringstream ss(name); while (std::getline(ss, item, '.')) { - name_list.push_back(item); + name_list.push_back(std::move(item)); } return name_list; } diff --git a/myframe/event_conn.cpp b/myframe/event_conn.cpp index 9211cdd..445ca77 100644 --- a/myframe/event_conn.cpp +++ b/myframe/event_conn.cpp @@ -44,7 +44,7 @@ int EventConn::Send(std::shared_ptr msg) { if (msg->GetDst().empty()) { return -1; } - mailbox_.Send(msg); + mailbox_.Send(std::move(msg)); cmd_channel_->SendToMain(CmdChannel::Cmd::kRun); CmdChannel::Cmd cmd; return cmd_channel_->RecvFromMain(&cmd); @@ -60,7 +60,7 @@ const std::shared_ptr EventConn::SendRequest( if (req->GetDst().empty()) { return nullptr; } - mailbox_.Send(req); + mailbox_.Send(std::move(req)); cmd_channel_->SendToMain(CmdChannel::Cmd::kRunWithMsg); CmdChannel::Cmd cmd; cmd_channel_->RecvFromMain(&cmd); diff --git a/myframe/event_conn_manager.cpp b/myframe/event_conn_manager.cpp index 226f67c..91b3155 100644 --- a/myframe/event_conn_manager.cpp +++ b/myframe/event_conn_manager.cpp @@ -37,7 +37,7 @@ void EventConnManager::AddEventConn() { auto conn = std::make_shared(poller_); std::string name = "event.conn." + std::to_string(conn_sz_); conn->GetMailbox()->SetAddr(name); - idle_conn_.emplace_back(conn); + idle_conn_.push_back(std::move(conn)); conn_sz_++; } @@ -62,7 +62,7 @@ void EventConnManager::Release(std::shared_ptr ev) { ev_mgr_->Del(ev); // add to idle_conn std::lock_guard g(mtx_); - idle_conn_.emplace_back(ev); + idle_conn_.push_back(std::move(ev)); } // call by main frame @@ -80,7 +80,7 @@ void EventConnManager::Notify( return; } // push msg to event_conn - ev->GetMailbox()->Recv(msg); + ev->GetMailbox()->Recv(std::move(msg)); // send cmd to event_conn auto cmd_channel = ev->GetCmdChannel(); cmd_channel->SendToOwner(CmdChannel::Cmd::kIdle); diff --git a/myframe/event_manager.cpp b/myframe/event_manager.cpp index 57b7fb8..6db1bb9 100644 --- a/myframe/event_manager.cpp +++ b/myframe/event_manager.cpp @@ -52,7 +52,7 @@ std::vector> EventManager::Get( return tmp_evs; } -bool EventManager::Add(std::shared_ptr ev) { +bool EventManager::Add(const std::shared_ptr& ev) { auto handle = ev->GetHandle(); std::unique_lock lk(rw_); if (evs_.find(handle) != evs_.end()) { @@ -64,7 +64,7 @@ bool EventManager::Add(std::shared_ptr ev) { return true; } -bool EventManager::Del(std::shared_ptr ev) { +bool EventManager::Del(const std::shared_ptr& ev) { auto handle = ev->GetHandle(); std::unique_lock lk(rw_); if (evs_.find(handle) == evs_.end()) { diff --git a/myframe/event_manager.h b/myframe/event_manager.h index 2db37ed..91b0b62 100644 --- a/myframe/event_manager.h +++ b/myframe/event_manager.h @@ -54,8 +54,8 @@ class EventManager final { ev_handle_t ToHandle(const std::string&); private: - bool Add(std::shared_ptr); - bool Del(std::shared_ptr); + bool Add(const std::shared_ptr&); + bool Del(const std::shared_ptr&); std::shared_mutex rw_; std::unordered_map name_handle_map_; diff --git a/myframe/mailbox.cpp b/myframe/mailbox.cpp index d4fd842..69f39e8 100644 --- a/myframe/mailbox.cpp +++ b/myframe/mailbox.cpp @@ -30,7 +30,7 @@ void Mailbox::SendClear() { } void Mailbox::Send(std::shared_ptr msg) { - send_.emplace_back(msg); + send_.push_back(std::move(msg)); } void Mailbox::Send( @@ -38,7 +38,7 @@ void Mailbox::Send( std::shared_ptr msg) { msg->SetSrc(addr_); msg->SetDst(dst); - Send(msg); + Send(std::move(msg)); } void Mailbox::Send( @@ -46,7 +46,7 @@ void Mailbox::Send( const std::any& data) { auto msg = std::make_shared(); msg->SetAnyData(data); - Send(dst, msg); + Send(dst, std::move(msg)); } void Mailbox::Send(std::list>* msg_list) { @@ -70,7 +70,7 @@ void Mailbox::RecvClear() { } void Mailbox::Recv(std::shared_ptr msg) { - recv_.emplace_back(msg); + recv_.push_back(std::move(msg)); } void Mailbox::Recv(std::list>* msg_list) { diff --git a/myframe/worker_context.cpp b/myframe/worker_context.cpp index 4ef0b40..0871097 100644 --- a/myframe/worker_context.cpp +++ b/myframe/worker_context.cpp @@ -85,7 +85,7 @@ std::list>* WorkerContext::GetCache() { } void WorkerContext::Cache(std::shared_ptr msg) { - cache_.emplace_back(msg); + cache_.push_back(std::move(msg)); } void WorkerContext::Cache(std::list>* msg_list) { diff --git a/myframe/worker_context_manager.cpp b/myframe/worker_context_manager.cpp index 86a798e..c3f67ff 100644 --- a/myframe/worker_context_manager.cpp +++ b/myframe/worker_context_manager.cpp @@ -77,7 +77,7 @@ void WorkerContextManager::PopFrontIdleWorker() { void WorkerContextManager::PushBackIdleWorker( std::shared_ptr worker) { std::unique_lock lk(rw_); - idle_workers_ctx_.emplace_back(worker); + idle_workers_ctx_.push_back(std::move(worker)); } std::vector WorkerContextManager::GetAllUserWorkerAddr() { @@ -163,7 +163,7 @@ void WorkerContextManager::DispatchWorkerMsg( LOG(WARNING) << worker_name << " unsupport recv msg, drop it"; return; } - worker_ctx->Cache(msg); + worker_ctx->Cache(std::move(msg)); LOG_IF(WARNING, worker_ctx->CacheSize() > warning_msg_size_.load()) << *worker_ctx << " has " << worker_ctx->CacheSize() diff --git a/myframe/worker_timer.cpp b/myframe/worker_timer.cpp index 54c89e6..a8984fa 100644 --- a/myframe/worker_timer.cpp +++ b/myframe/worker_timer.cpp @@ -85,7 +85,7 @@ void TimerManager::_Dispath(List* cur) { msg->SetDesc(timer->timer_name_); msg->SetType("TIMER"); delete begin; - timeout_list_.emplace_back(msg); + timeout_list_.push_back(std::move(msg)); begin = temp; } }