From: Arnaud Giersch Date: Fri, 15 Feb 2019 14:04:15 +0000 (+0100) Subject: Dynamic cast without checking result is slow and useless. Use static cast. X-Git-Tag: v3_22~321 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/58bb531bcf303874a5e1356c750423a8490617e9 Dynamic cast without checking result is slow and useless. Use static cast. It brings for example > 1.2 speedup on s4u-dht-kademlia. --- diff --git a/include/simgrid/s4u/Engine.hpp b/include/simgrid/s4u/Engine.hpp index ae82253529..cc6a2a8e59 100644 --- a/include/simgrid/s4u/Engine.hpp +++ b/include/simgrid/s4u/Engine.hpp @@ -306,8 +306,9 @@ template XBT_PRIVATE void get_filtered_netzones_recursive(s4u::NetZone "Filtering netzones is only possible for subclasses of kernel::routing::NetZoneImpl"); for (auto const& elem : current->get_children()) { get_filtered_netzones_recursive(elem, whereto); - if (elem->get_impl() == dynamic_cast(elem->get_impl())) - whereto->push_back(dynamic_cast(elem->get_impl())); + T* elem_impl = dynamic_cast(elem->get_impl()); + if (elem_impl != nullptr) + whereto->push_back(elem_impl); } } #endif diff --git a/src/instr/instr_paje_containers.cpp b/src/instr/instr_paje_containers.cpp index e648aeb3dc..4b23e354a8 100644 --- a/src/instr/instr_paje_containers.cpp +++ b/src/instr/instr_paje_containers.cpp @@ -215,21 +215,21 @@ void Container::log_destruction() StateType* Container::get_state(const std::string& name) { - StateType* ret = dynamic_cast(type_->by_name(name)); + StateType* ret = static_cast(type_->by_name(name)); ret->set_calling_container(this); return ret; } LinkType* Container::get_link(const std::string& name) { - LinkType* ret = dynamic_cast(type_->by_name(name)); + LinkType* ret = static_cast(type_->by_name(name)); ret->set_calling_container(this); return ret; } VariableType* Container::get_variable(const std::string& name) { - VariableType* ret = dynamic_cast(type_->by_name(name)); + VariableType* ret = static_cast(type_->by_name(name)); ret->set_calling_container(this); return ret; } diff --git a/src/kernel/activity/MailboxImpl.cpp b/src/kernel/activity/MailboxImpl.cpp index 8ad9e02ce9..506040480e 100644 --- a/src/kernel/activity/MailboxImpl.cpp +++ b/src/kernel/activity/MailboxImpl.cpp @@ -132,7 +132,7 @@ CommImplPtr MailboxImpl::find_matching_comm(CommImpl::Type type, int (*match_fun deque = &comm_queue_; for (auto it = deque->begin(); it != deque->end(); it++) { - CommImplPtr comm = boost::dynamic_pointer_cast(std::move(*it)); + CommImplPtr comm = boost::static_pointer_cast(std::move(*it)); if (comm->type == CommImpl::Type::SEND) { other_user_data = comm->src_data_; diff --git a/src/plugins/host_energy.cpp b/src/plugins/host_energy.cpp index 82417f7f46..3a81ebee01 100644 --- a/src/plugins/host_energy.cpp +++ b/src/plugins/host_energy.cpp @@ -497,8 +497,9 @@ void sg_host_energy_plugin_init() simgrid::kernel::activity::ExecImpl::on_creation.connect([](simgrid::kernel::activity::ExecImplPtr activity){ if (activity->host_ != nullptr) { // We only run on one host simgrid::s4u::Host* host = activity->host_; - if (dynamic_cast(activity->host_)) - host = dynamic_cast(activity->host_)->get_pm(); + simgrid::s4u::VirtualMachine* vm = dynamic_cast(host); + if (vm != nullptr) + host = vm->get_pm(); host->extension()->update(); } diff --git a/src/plugins/host_load.cpp b/src/plugins/host_load.cpp index 0769bd2dec..c529ac8067 100644 --- a/src/plugins/host_load.cpp +++ b/src/plugins/host_load.cpp @@ -207,8 +207,9 @@ void sg_host_load_plugin_init() simgrid::kernel::activity::ExecImpl::on_creation.connect([](simgrid::kernel::activity::ExecImplPtr activity){ if (activity->host_ != nullptr) { // We only run on one host simgrid::s4u::Host* host = activity->host_; - if (dynamic_cast(activity->host_)) - host = dynamic_cast(activity->host_)->get_pm(); + simgrid::s4u::VirtualMachine* vm = dynamic_cast(host); + if (vm != nullptr) + host = vm->get_pm(); host->extension()->add_activity(activity); host->extension()->update(); // If the system was idle until now, we need to update *before* @@ -222,8 +223,9 @@ void sg_host_load_plugin_init() simgrid::kernel::activity::ExecImpl::on_completion.connect([](simgrid::kernel::activity::ExecImplPtr activity){ if (activity->host_ != nullptr) { // We only run on one host simgrid::s4u::Host* host = activity->host_; - if (dynamic_cast(activity->host_)) - host = dynamic_cast(activity->host_)->get_pm(); + simgrid::s4u::VirtualMachine* vm = dynamic_cast(host); + if (vm != nullptr) + host = vm->get_pm(); host->extension()->update(); } diff --git a/src/s4u/s4u_Comm.cpp b/src/s4u/s4u_Comm.cpp index a5e19da341..3f59ca2e34 100644 --- a/src/s4u/s4u_Comm.cpp +++ b/src/s4u/s4u_Comm.cpp @@ -201,7 +201,7 @@ Comm* Comm::detach() Comm* Comm::cancel() { - simgrid::simix::simcall([this] { dynamic_cast(pimpl_.get())->cancel(); }); + simgrid::simix::simcall([this] { static_cast(pimpl_.get())->cancel(); }); state_ = State::CANCELED; return this; } diff --git a/src/s4u/s4u_Exec.cpp b/src/s4u/s4u_Exec.cpp index 0ecdc7292e..1860395c3f 100644 --- a/src/s4u/s4u_Exec.cpp +++ b/src/s4u/s4u_Exec.cpp @@ -26,9 +26,8 @@ Exec::Exec(sg_host_t host, double flops_amount) : Activity(), host_(host), flops Exec* Exec::start() { - simix::simcall([this] { - dynamic_cast(pimpl_.get())->start(flops_amount_, 1. / priority_, bound_); - }); + simix::simcall( + [this] { static_cast(pimpl_.get())->start(flops_amount_, 1. / priority_, bound_); }); state_ = State::STARTED; on_start(Actor::self()); return this; @@ -36,7 +35,7 @@ Exec* Exec::start() Exec* Exec::cancel() { - simgrid::simix::simcall([this] { dynamic_cast(pimpl_.get())->cancel(); }); + simgrid::simix::simcall([this] { static_cast(pimpl_.get())->cancel(); }); state_ = State::CANCELED; return this; } diff --git a/src/s4u/s4u_Io.cpp b/src/s4u/s4u_Io.cpp index 27ff69f216..86d6303c57 100644 --- a/src/s4u/s4u_Io.cpp +++ b/src/s4u/s4u_Io.cpp @@ -23,14 +23,14 @@ Io::Io(sg_storage_t storage, sg_size_t size, OpType type) : Activity(), storage_ Io* Io::start() { - simix::simcall([this] { dynamic_cast(pimpl_.get())->start(size_, type_); }); + simix::simcall([this] { static_cast(pimpl_.get())->start(size_, type_); }); state_ = State::STARTED; return this; } Io* Io::cancel() { - simgrid::simix::simcall([this] { dynamic_cast(pimpl_.get())->cancel(); }); + simgrid::simix::simcall([this] { static_cast(pimpl_.get())->cancel(); }); state_ = State::CANCELED; return this; } diff --git a/src/simix/ActorImpl.cpp b/src/simix/ActorImpl.cpp index 57dc7a134e..4fc435d496 100644 --- a/src/simix/ActorImpl.cpp +++ b/src/simix/ActorImpl.cpp @@ -631,7 +631,7 @@ void SIMIX_process_sleep_destroy(smx_activity_t synchro) { XBT_DEBUG("Destroy sleep synchro %p", synchro.get()); simgrid::kernel::activity::SleepImplPtr sleep = - boost::dynamic_pointer_cast(synchro); + boost::static_pointer_cast(synchro); if (sleep->surf_action_) { sleep->surf_action_->unref(); diff --git a/src/smpi/internals/smpi_global.cpp b/src/smpi/internals/smpi_global.cpp index c9604a5ecb..c8253eecab 100644 --- a/src/smpi/internals/smpi_global.cpp +++ b/src/smpi/internals/smpi_global.cpp @@ -168,7 +168,7 @@ static void check_blocks(std::vector> &private_blocks, void smpi_comm_copy_buffer_callback(smx_activity_t synchro, void *buff, size_t buff_size) { simgrid::kernel::activity::CommImplPtr comm = - boost::dynamic_pointer_cast(synchro); + boost::static_pointer_cast(synchro); int src_shared = 0; int dst_shared = 0; size_t src_offset = 0; diff --git a/src/xbt/config.cpp b/src/xbt/config.cpp index 6d7db52f0d..c69b636fc1 100644 --- a/src/xbt/config.cpp +++ b/src/xbt/config.cpp @@ -157,15 +157,15 @@ public: template T const& get_value() const { - return dynamic_cast&>(*this).get_value(); + return static_cast&>(*this).get_value(); } template void set_value(T value) { - dynamic_cast&>(*this).set_value(std::move(value)); + static_cast&>(*this).set_value(std::move(value)); } template void set_default_value(T value) { - dynamic_cast&>(*this).set_default_value(std::move(value)); + static_cast&>(*this).set_default_value(std::move(value)); } void unset_default() { isdefault = false; } bool is_default() const { return isdefault; }