Skip to content

Commit

Permalink
Optimize pointer access
Browse files Browse the repository at this point in the history
  • Loading branch information
lkpworkspace committed May 8, 2024
1 parent e47c629 commit e8cf2f6
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 34 deletions.
22 changes: 9 additions & 13 deletions myframe/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_; }
Expand All @@ -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;
Expand All @@ -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()) {
Expand All @@ -82,12 +79,12 @@ bool Actor::Subscribe(const std::string& addr, const std::string& msg_type) {
auto msg = std::make_shared<Msg>();
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<ActorContext> c) { ctx_ = c; }
void Actor::SetContext(ActorContext* c) { ctx_ = c; }

const Json::Value* Actor::GetConfig() const {
return &config_;
Expand All @@ -98,11 +95,10 @@ void Actor::SetConfig(const Json::Value& conf) {
}

std::shared_ptr<App> Actor::GetApp() {
auto ctx = ctx_.lock();
if (ctx == nullptr) {
if (ctx_ == nullptr) {
return nullptr;
}
return ctx->GetApp();
return ctx_->GetApp();
}

} // namespace myframe
4 changes: 2 additions & 2 deletions myframe/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActorContext>);
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<ActorContext> ctx_;
ActorContext* ctx_{ nullptr };

DISALLOW_COPY_AND_ASSIGN(Actor)
};
Expand Down
2 changes: 1 addition & 1 deletion myframe/actor_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand All @@ -34,7 +35,6 @@ ActorContext::~ActorContext() {
std::shared_ptr<App> ActorContext::GetApp() { return app_.lock(); }

int ActorContext::Init(const char* param) {
actor_->SetContext(shared_from_this());
return actor_->Init(param);
}

Expand Down
2 changes: 1 addition & 1 deletion myframe/actor_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class App;
class Msg;
class Actor;
class WorkerCommon;
class ActorContext final : public std::enable_shared_from_this<ActorContext> {
class ActorContext final {
friend std::ostream& operator<<(std::ostream& out, const ActorContext& ctx);
friend class ActorContextManager;
friend class WorkerCommon;
Expand Down
1 change: 0 additions & 1 deletion myframe/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ bool App::AddWorker(
const Json::Value& config) {
auto worker_ctx = std::make_shared<WorkerContext>(
shared_from_this(), worker, poller_);
worker->SetContext(worker_ctx);
worker->SetInstName(inst_name);
worker->SetConfig(config);
if (worker->GetTypeName() == "node") {
Expand Down
2 changes: 1 addition & 1 deletion myframe/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace myframe {
#error "Unsupported platform"
#endif

class MYFRAME_EXPORT Event : public std::enable_shared_from_this<Event> {
class MYFRAME_EXPORT Event {
public:
enum class Type : int {
kWorkerCommon,
Expand Down
22 changes: 9 additions & 13 deletions myframe/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -84,7 +81,7 @@ const Json::Value* Worker::GetConfig() const {
return &config_;
}

void Worker::SetContext(std::shared_ptr<WorkerContext> ctx) {
void Worker::SetContext(WorkerContext* ctx) {
ctx_ = ctx;
}

Expand All @@ -93,11 +90,10 @@ Event::Type Worker::GetType() {
}

std::shared_ptr<App> Worker::GetApp() {
auto ctx = ctx_.lock();
if (ctx == nullptr) {
if (ctx_ == nullptr) {
return nullptr;
}
return ctx->GetApp();
return ctx_->GetApp();
}

} // namespace myframe
4 changes: 2 additions & 2 deletions myframe/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ class MYFRAME_EXPORT Worker {
void SetInstName(const std::string&);
void SetConfig(const Json::Value&);

void SetContext(std::shared_ptr<WorkerContext>);
void SetContext(WorkerContext*);

std::string mod_name_;
std::string worker_name_;
std::string inst_name_;
Json::Value config_;

std::weak_ptr<WorkerContext> ctx_;
WorkerContext* ctx_{ nullptr };

DISALLOW_COPY_AND_ASSIGN(Worker)
};
Expand Down
1 change: 1 addition & 0 deletions myframe/worker_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ WorkerContext::WorkerContext(
: runing_(false)
, worker_(worker)
, app_(app) {
worker_->SetContext(this);
cmd_channel_ = CmdChannel::Create(poller);
}

Expand Down

0 comments on commit e8cf2f6

Please sign in to comment.