From 6a5b0506e51fdbdb91d70d2c579da37058fda930 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 3 Nov 2021 15:10:43 +0300 Subject: [PATCH] Split Actor.is_lite to need_context and need_start_up. --- benchmark/bench_actor.cpp | 41 +++++++++++++++----------- tdactor/td/actor/PromiseFuture.h | 3 +- tdactor/td/actor/impl/Actor-decl.h | 3 +- tdactor/td/actor/impl/ActorInfo-decl.h | 8 +++-- tdactor/td/actor/impl/ActorInfo.h | 20 +++++++++---- tdactor/td/actor/impl/Scheduler.cpp | 10 +++---- tdactor/td/actor/impl/Scheduler.h | 4 +-- 7 files changed, 55 insertions(+), 34 deletions(-) diff --git a/benchmark/bench_actor.cpp b/benchmark/bench_actor.cpp index bfcaa65a7..338442c62 100644 --- a/benchmark/bench_actor.cpp +++ b/benchmark/bench_actor.cpp @@ -18,25 +18,34 @@ #pragma comment(linker, "/STACK:16777216") #endif +struct TestActor final : public td::Actor { + static td::int32 actor_count_; + + void start_up() final { + actor_count_++; + stop(); + } + + void tear_down() final { + if (--actor_count_ == 0) { + td::Scheduler::instance()->finish(); + } + } +}; + +td::int32 TestActor::actor_count_; + +template <> +class td::ActorTraits { + public: + static constexpr bool need_context = false; + static constexpr bool need_start_up = true; +}; + class CreateActorBench final : public td::Benchmark { private: td::ConcurrentScheduler scheduler_; - struct TestActor final : public td::Actor { - static td::int32 actor_count_; - - void start_up() final { - actor_count_++; - stop(); - } - - void tear_down() final { - if (--actor_count_ == 0) { - td::Scheduler::instance()->finish(); - } - } - }; - void start_up() final { scheduler_.init(0); scheduler_.start(); @@ -61,8 +70,6 @@ class CreateActorBench final : public td::Benchmark { } }; -td::int32 CreateActorBench::TestActor::actor_count_; - template class RingBench final : public td::Benchmark { public: diff --git a/tdactor/td/actor/PromiseFuture.h b/tdactor/td/actor/PromiseFuture.h index a4439f2d1..2b47b1d48 100644 --- a/tdactor/td/actor/PromiseFuture.h +++ b/tdactor/td/actor/PromiseFuture.h @@ -469,7 +469,8 @@ class PromiseActor; template class ActorTraits> { public: - static constexpr bool is_lite = true; + static constexpr bool need_context = false; + static constexpr bool need_start_up = false; }; template diff --git a/tdactor/td/actor/impl/Actor-decl.h b/tdactor/td/actor/impl/Actor-decl.h index bd3bb4e96..99eccef42 100644 --- a/tdactor/td/actor/impl/Actor-decl.h +++ b/tdactor/td/actor/impl/Actor-decl.h @@ -115,7 +115,8 @@ class Actor : public ObserverBase { template class ActorTraits { public: - static constexpr bool is_lite = false; + static constexpr bool need_context = true; + static constexpr bool need_start_up = true; }; } // namespace td diff --git a/tdactor/td/actor/impl/ActorInfo-decl.h b/tdactor/td/actor/impl/ActorInfo-decl.h index cb9a8e7bd..1b79a4016 100644 --- a/tdactor/td/actor/impl/ActorInfo-decl.h +++ b/tdactor/td/actor/impl/ActorInfo-decl.h @@ -63,7 +63,7 @@ class ActorInfo final ActorInfo &operator=(const ActorInfo &) = delete; void init(int32 sched_id, Slice name, ObjectPool::OwnerPtr &&this_ptr, Actor *actor_ptr, Deleter deleter, - bool is_lite); + bool need_context, bool need_start_up); void on_actor_moved(Actor *actor_new_ptr); template @@ -105,7 +105,8 @@ class ActorInfo final vector mailbox_; - bool is_lite() const; + bool need_context() const; + bool need_start_up() const; void set_wait_generation(uint32 wait_generation); bool must_wait(uint32 wait_generation) const; @@ -113,7 +114,8 @@ class ActorInfo final private: Deleter deleter_ = Deleter::None; - bool is_lite_ = false; + bool need_context_ = true; + bool need_start_up_ = true; bool is_running_ = false; bool always_wait_for_mailbox_{false}; uint32 wait_generation_{0}; diff --git a/tdactor/td/actor/impl/ActorInfo.h b/tdactor/td/actor/impl/ActorInfo.h index c20bbeb2e..f8e467fdd 100644 --- a/tdactor/td/actor/impl/ActorInfo.h +++ b/tdactor/td/actor/impl/ActorInfo.h @@ -32,13 +32,13 @@ inline StringBuilder &operator<<(StringBuilder &sb, const ActorInfo &info) { } inline void ActorInfo::init(int32 sched_id, Slice name, ObjectPool::OwnerPtr &&this_ptr, Actor *actor_ptr, - Deleter deleter, bool is_lite) { + Deleter deleter, bool need_context, bool need_start_up) { CHECK(!is_running()); CHECK(!is_migrating()); sched_id_.store(sched_id, std::memory_order_relaxed); actor_ = actor_ptr; - if (!is_lite) { + if (need_context) { context_ = Scheduler::context()->this_ptr_.lock(); VLOG(actor) << "Set context " << context_.get() << " for " << name; } @@ -48,22 +48,32 @@ inline void ActorInfo::init(int32 sched_id, Slice name, ObjectPool::O actor_->init(std::move(this_ptr)); deleter_ = deleter; - is_lite_ = is_lite; + need_context_ = need_context; + need_start_up_ = need_start_up; is_running_ = false; wait_generation_ = 0; } -inline bool ActorInfo::is_lite() const { - return is_lite_; + +inline bool ActorInfo::need_context() const { + return need_context_; } + +inline bool ActorInfo::need_start_up() const { + return need_start_up_; +} + inline void ActorInfo::set_wait_generation(uint32 wait_generation) { wait_generation_ = wait_generation; } + inline bool ActorInfo::must_wait(uint32 wait_generation) const { return wait_generation_ == wait_generation || (always_wait_for_mailbox_ && !mailbox_.empty()); } + inline void ActorInfo::always_wait_for_mailbox() { always_wait_for_mailbox_ = true; } + inline void ActorInfo::on_actor_moved(Actor *actor_new_ptr) { actor_ = actor_new_ptr; } diff --git a/tdactor/td/actor/impl/Scheduler.cpp b/tdactor/td/actor/impl/Scheduler.cpp index 45d54c21e..2407b8b14 100644 --- a/tdactor/td/actor/impl/Scheduler.cpp +++ b/tdactor/td/actor/impl/Scheduler.cpp @@ -168,10 +168,10 @@ EventGuard::~EventGuard() { } info->finish_run(); swap_context(info); - CHECK(info->is_lite() || save_context_ == info->get_context()); + CHECK(!info->need_context() || save_context_ == info->get_context()); #ifdef TD_DEBUG - LOG_CHECK(info->is_lite() || save_log_tag2_ == info->get_name().c_str()) - << info->is_lite() << " " << info->empty() << " " << info->is_migrating() << " " << save_log_tag2_ << " " + LOG_CHECK(!info->need_context() || save_log_tag2_ == info->get_name().c_str()) + << info->need_context() << " " << info->empty() << " " << info->is_migrating() << " " << save_log_tag2_ << " " << info->get_name() << " " << scheduler_->close_flag_; #endif if (event_context_.flags & Scheduler::EventContext::Stop) { @@ -186,7 +186,7 @@ EventGuard::~EventGuard() { void EventGuard::swap_context(ActorInfo *info) { std::swap(scheduler_->event_context_ptr_, event_context_ptr_); - if (info->is_lite()) { + if (!info->need_context()) { return; } @@ -353,7 +353,7 @@ void Scheduler::do_stop_actor(ActorInfo *actor_info) { CHECK(!actor_info->is_migrating()); LOG_CHECK(actor_info->migrate_dest() == sched_id_) << actor_info->migrate_dest() << " " << sched_id_; ObjectPool::OwnerPtr owner_ptr; - if (!actor_info->is_lite()) { + if (actor_info->need_start_up()) { EventGuard guard(this, actor_info); do_event(actor_info, Event::stop()); owner_ptr = actor_info->get_actor_unsafe()->clear(); diff --git a/tdactor/td/actor/impl/Scheduler.h b/tdactor/td/actor/impl/Scheduler.h index 60e6d66b9..712b77be0 100644 --- a/tdactor/td/actor/impl/Scheduler.h +++ b/tdactor/td/actor/impl/Scheduler.h @@ -109,7 +109,7 @@ ActorOwn Scheduler::register_actor_impl(Slice name, ActorT *actor_ptr, A auto weak_info = info.get_weak(); auto actor_info = info.get(); actor_info->init(sched_id_, name, std::move(info), static_cast(actor_ptr), deleter, - ActorTraits::is_lite); + ActorTraits::need_context, ActorTraits::need_start_up); ActorId actor_id = weak_info->actor_id(actor_ptr); if (sched_id != sched_id_) { @@ -117,7 +117,7 @@ ActorOwn Scheduler::register_actor_impl(Slice name, ActorT *actor_ptr, A do_migrate_actor(actor_info, sched_id); } else { pending_actors_list_.put(weak_info->get_list_node()); - if (!ActorTraits::is_lite) { + if (ActorTraits::need_start_up) { send(actor_id, Event::start()); } }