From e8cf2f6dfbf24d4c9b747f615aac02c5705ade0e Mon Sep 17 00:00:00 2001 From: likepeng Date: Wed, 8 May 2024 18:49:32 +0800 Subject: [PATCH] Optimize pointer access --- myframe/actor.cpp | 22 +++++++++------------- myframe/actor.h | 4 ++-- myframe/actor_context.cpp | 2 +- myframe/actor_context.h | 2 +- myframe/app.cpp | 1 - myframe/event.h | 2 +- myframe/worker.cpp | 22 +++++++++------------- myframe/worker.h | 4 ++-- myframe/worker_context.cpp | 1 + 9 files changed, 26 insertions(+), 34 deletions(-) diff --git a/myframe/actor.cpp b/myframe/actor.cpp index 0c78bbc..6af5a9f 100644 --- a/myframe/actor.cpp +++ b/myframe/actor.cpp @@ -33,11 +33,10 @@ const std::string& Actor::GetModName() const { bool Actor::IsFromLib() const { return is_from_lib_; } Mailbox* Actor::GetMailbox() { - auto ctx = ctx_.lock(); - if (ctx == nullptr) { + if (ctx_ == nullptr) { return nullptr; } - return ctx->GetMailbox(); + return ctx_->GetMailbox(); } const std::string& Actor::GetTypeName() const { return actor_name_; } @@ -53,12 +52,11 @@ void Actor::SetTypeName(const std::string& name) { actor_name_ = name; } void Actor::SetInstName(const std::string& name) { instance_name_ = name; } int Actor::Timeout(const std::string& timer_name, int expired) { - auto ctx = ctx_.lock(); - if (ctx == nullptr) { + if (ctx_ == nullptr) { LOG(ERROR) << "actor context is nullptr"; return -1; } - auto app = ctx->GetApp(); + auto app = ctx_->GetApp(); if (app == nullptr) { LOG(ERROR) << "app is nullptr"; return -1; @@ -72,8 +70,7 @@ int Actor::Timeout(const std::string& timer_name, int expired) { } bool Actor::Subscribe(const std::string& addr, const std::string& msg_type) { - auto ctx = ctx_.lock(); - if (ctx == nullptr) { + if (ctx_ == nullptr) { return false; } if (addr == GetActorName()) { @@ -82,12 +79,12 @@ bool Actor::Subscribe(const std::string& addr, const std::string& msg_type) { auto msg = std::make_shared(); msg->SetType("SUBSCRIBE"); msg->SetDesc(msg_type); - auto mailbox = ctx->GetMailbox(); + auto mailbox = ctx_->GetMailbox(); mailbox->Send(addr, msg); return true; } -void Actor::SetContext(std::shared_ptr c) { ctx_ = c; } +void Actor::SetContext(ActorContext* c) { ctx_ = c; } const Json::Value* Actor::GetConfig() const { return &config_; @@ -98,11 +95,10 @@ void Actor::SetConfig(const Json::Value& conf) { } std::shared_ptr Actor::GetApp() { - auto ctx = ctx_.lock(); - if (ctx == nullptr) { + if (ctx_ == nullptr) { return nullptr; } - return ctx->GetApp(); + return ctx_->GetApp(); } } // namespace myframe diff --git a/myframe/actor.h b/myframe/actor.h index 876ef28..e08059c 100644 --- a/myframe/actor.h +++ b/myframe/actor.h @@ -116,14 +116,14 @@ class MYFRAME_EXPORT Actor { void SetInstName(const std::string& name); void SetConfig(const Json::Value& conf); - void SetContext(std::shared_ptr); + void SetContext(ActorContext*); bool is_from_lib_{ false }; std::string mod_name_; std::string actor_name_; std::string instance_name_; Json::Value config_; - std::weak_ptr ctx_; + ActorContext* ctx_{ nullptr }; DISALLOW_COPY_AND_ASSIGN(Actor) }; diff --git a/myframe/actor_context.cpp b/myframe/actor_context.cpp index 2392295..15db419 100644 --- a/myframe/actor_context.cpp +++ b/myframe/actor_context.cpp @@ -23,6 +23,7 @@ ActorContext::ActorContext( , in_wait_que_(false) , actor_(actor) , app_(app) { + actor_->SetContext(this); mailbox_.SetAddr(actor_->GetActorName()); LOG(INFO) << mailbox_.Addr() << " context create"; } @@ -34,7 +35,6 @@ ActorContext::~ActorContext() { std::shared_ptr ActorContext::GetApp() { return app_.lock(); } int ActorContext::Init(const char* param) { - actor_->SetContext(shared_from_this()); return actor_->Init(param); } diff --git a/myframe/actor_context.h b/myframe/actor_context.h index b9ef15e..ac2cd12 100644 --- a/myframe/actor_context.h +++ b/myframe/actor_context.h @@ -19,7 +19,7 @@ class App; class Msg; class Actor; class WorkerCommon; -class ActorContext final : public std::enable_shared_from_this { +class ActorContext final { friend std::ostream& operator<<(std::ostream& out, const ActorContext& ctx); friend class ActorContextManager; friend class WorkerCommon; diff --git a/myframe/app.cpp b/myframe/app.cpp index 6975dd4..1a3fa2b 100644 --- a/myframe/app.cpp +++ b/myframe/app.cpp @@ -276,7 +276,6 @@ bool App::AddWorker( const Json::Value& config) { auto worker_ctx = std::make_shared( shared_from_this(), worker, poller_); - worker->SetContext(worker_ctx); worker->SetInstName(inst_name); worker->SetConfig(config); if (worker->GetTypeName() == "node") { diff --git a/myframe/event.h b/myframe/event.h index c072d0a..b357275 100644 --- a/myframe/event.h +++ b/myframe/event.h @@ -31,7 +31,7 @@ namespace myframe { #error "Unsupported platform" #endif -class MYFRAME_EXPORT Event : public std::enable_shared_from_this { +class MYFRAME_EXPORT Event { public: enum class Type : int { kWorkerCommon, diff --git a/myframe/worker.cpp b/myframe/worker.cpp index 3601187..0a0ddf2 100644 --- a/myframe/worker.cpp +++ b/myframe/worker.cpp @@ -28,11 +28,10 @@ void Worker::SetTypeName(const std::string& name) { worker_name_ = name; } void Worker::SetInstName(const std::string& name) { inst_name_ = name; } void Worker::Stop() { - auto ctx = ctx_.lock(); - if (ctx == nullptr) { + if (ctx_ == nullptr) { return; } - ctx->Stop(); + ctx_->Stop(); } int Worker::DispatchMsg() { @@ -62,18 +61,16 @@ int Worker::DispatchAndWaitMsg() { } Mailbox* Worker::GetMailbox() { - auto ctx = ctx_.lock(); - if (ctx == nullptr) { + if (ctx_ == nullptr) { return nullptr; } - return ctx->GetMailbox(); + return ctx_->GetMailbox(); } CmdChannel* Worker::GetCmdChannel() { - auto ctx = ctx_.lock(); - LOG_IF(FATAL, ctx == nullptr) + LOG_IF(FATAL, ctx_ == nullptr) << "worker ctx is nullptr"; - return ctx->GetCmdChannel(); + return ctx_->GetCmdChannel(); } void Worker::SetConfig(const Json::Value& conf) { @@ -84,7 +81,7 @@ const Json::Value* Worker::GetConfig() const { return &config_; } -void Worker::SetContext(std::shared_ptr ctx) { +void Worker::SetContext(WorkerContext* ctx) { ctx_ = ctx; } @@ -93,11 +90,10 @@ Event::Type Worker::GetType() { } std::shared_ptr Worker::GetApp() { - auto ctx = ctx_.lock(); - if (ctx == nullptr) { + if (ctx_ == nullptr) { return nullptr; } - return ctx->GetApp(); + return ctx_->GetApp(); } } // namespace myframe diff --git a/myframe/worker.h b/myframe/worker.h index d1c77c2..4cfa9a8 100644 --- a/myframe/worker.h +++ b/myframe/worker.h @@ -114,14 +114,14 @@ class MYFRAME_EXPORT Worker { void SetInstName(const std::string&); void SetConfig(const Json::Value&); - void SetContext(std::shared_ptr); + void SetContext(WorkerContext*); std::string mod_name_; std::string worker_name_; std::string inst_name_; Json::Value config_; - std::weak_ptr ctx_; + WorkerContext* ctx_{ nullptr }; DISALLOW_COPY_AND_ASSIGN(Worker) }; diff --git a/myframe/worker_context.cpp b/myframe/worker_context.cpp index eacc2aa..ee255e6 100644 --- a/myframe/worker_context.cpp +++ b/myframe/worker_context.cpp @@ -23,6 +23,7 @@ WorkerContext::WorkerContext( : runing_(false) , worker_(worker) , app_(app) { + worker_->SetContext(this); cmd_channel_ = CmdChannel::Create(poller); }