From c897fb0d193bbe081581d7ea7f579f04df1b5b5d Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Mon, 24 Jul 2017 10:51:56 +0200 Subject: [PATCH] overall optimization of map usage replace all find != end + at by a try/catch on at. This should save a useless traversal of the [unordered_]map each time we want to access an element. --- src/kernel/routing/NetZoneImpl.cpp | 12 +++++++++--- src/kernel/routing/VivaldiZone.cpp | 9 +++++++-- src/s4u/s4u_engine.cpp | 6 ++++-- src/s4u/s4u_host.cpp | 6 ++++-- src/simix/ActorImpl.cpp | 6 ++++-- src/smpi/mpi/smpi_group.cpp | 7 ++++--- src/surf/FileImpl.cpp | 8 ++++---- src/surf/network_interface.cpp | 6 ++++-- src/xbt/xbt_replay.cpp | 4 ++-- 9 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/kernel/routing/NetZoneImpl.cpp b/src/kernel/routing/NetZoneImpl.cpp index 2b4a84d6aa..148680148e 100644 --- a/src/kernel/routing/NetZoneImpl.cpp +++ b/src/kernel/routing/NetZoneImpl.cpp @@ -260,16 +260,20 @@ bool NetZoneImpl::getBypassRoute(routing::NetPoint* src, routing::NetPoint* dst, for (int i = 0; i < max; i++) { if (i <= max_index_src && max <= max_index_dst) { key = {path_src.at(i)->netpoint_, path_dst.at(max)->netpoint_}; - if (bypassRoutes_.find(key) != bypassRoutes_.end()) { + try { bypassedRoute = bypassRoutes_.at(key); break; + } catch (std::out_of_range& unfound) { + // Do nothing } } if (max <= max_index_src && i <= max_index_dst) { key = {path_src.at(max)->netpoint_, path_dst.at(i)->netpoint_}; - if (bypassRoutes_.find(key) != bypassRoutes_.end()) { + try { bypassedRoute = bypassRoutes_.at(key); break; + } catch (std::out_of_range& unfound) { + // Do nothing } } } @@ -279,9 +283,11 @@ bool NetZoneImpl::getBypassRoute(routing::NetPoint* src, routing::NetPoint* dst, if (max <= max_index_src && max <= max_index_dst) { key = {path_src.at(max)->netpoint_, path_dst.at(max)->netpoint_}; - if (bypassRoutes_.find(key) != bypassRoutes_.end()) { + try { bypassedRoute = bypassRoutes_.at(key); break; + } catch (std::out_of_range& unfound) { + // Do nothing } } } diff --git a/src/kernel/routing/VivaldiZone.cpp b/src/kernel/routing/VivaldiZone.cpp index ddc4b6d788..c2f242a75f 100644 --- a/src/kernel/routing/VivaldiZone.cpp +++ b/src/kernel/routing/VivaldiZone.cpp @@ -83,21 +83,26 @@ void VivaldiZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cba } /* Retrieve the private links */ - if (privateLinks_.find(src->id()) != privateLinks_.end()) { + try { std::pair info = privateLinks_.at(src->id()); if (info.first) { route->link_list->push_back(info.first); if (lat) *lat += info.first->latency(); } + } catch (std::out_of_range& unfound) { + XBT_DEBUG("Source of private link (%u) doesn't exist", src->id()); } - if (privateLinks_.find(dst->id()) != privateLinks_.end()) { + + try { std::pair info = privateLinks_.at(dst->id()); if (info.second) { route->link_list->push_back(info.second); if (lat) *lat += info.second->latency(); } + } catch (std::out_of_range& unfound) { + XBT_DEBUG("Destination of private link (%u) doesn't exist", dst->id()); } /* Compute the extra latency due to the euclidean distance if needed */ diff --git a/src/s4u/s4u_engine.cpp b/src/s4u/s4u_engine.cpp index 0e983e8ffe..8df13cd410 100644 --- a/src/s4u/s4u_engine.cpp +++ b/src/s4u/s4u_engine.cpp @@ -128,9 +128,11 @@ NetZone* Engine::getNetzoneByNameOrNull(const char* name) /** @brief Retrieve the netpoint of the given name (or nullptr if not found) */ simgrid::kernel::routing::NetPoint* Engine::getNetpointByNameOrNull(const char* name) { - if (pimpl->netpoints_.find(name) == pimpl->netpoints_.end()) + try { + return pimpl->netpoints_.at(name); + } catch (std::out_of_range& unfound) { return nullptr; - return pimpl->netpoints_.at(name); + } } /** @brief Fill the provided vector with all existing netpoints */ void Engine::getNetpointList(std::vector* list) diff --git a/src/s4u/s4u_host.cpp b/src/s4u/s4u_host.cpp index a1520f952b..f39b2a2969 100644 --- a/src/s4u/s4u_host.cpp +++ b/src/s4u/s4u_host.cpp @@ -87,9 +87,11 @@ Host* Host::by_name_or_null(const char* name) } Host* Host::by_name_or_null(std::string name) { - if (host_list.find(name) == host_list.end()) + try { + return host_list.at(name); + } catch (std::out_of_range& unfound) { return nullptr; - return host_list.at(name); + } } Host *Host::current(){ diff --git a/src/simix/ActorImpl.cpp b/src/simix/ActorImpl.cpp index 25902cea16..d9b877d4ad 100644 --- a/src/simix/ActorImpl.cpp +++ b/src/simix/ActorImpl.cpp @@ -823,9 +823,11 @@ xbt_dynar_t SIMIX_process_get_runnable() /** @brief Returns the process from PID. */ smx_actor_t SIMIX_process_from_PID(aid_t PID) { - if (simix_global->process_list.find(PID) == simix_global->process_list.end()) + try { + return simix_global->process_list.at(PID); + } catch (std::out_of_range& unfound) { return nullptr; - return simix_global->process_list.at(PID); + } } /** @brief returns a dynar containing all currently existing processes */ diff --git a/src/smpi/mpi/smpi_group.cpp b/src/smpi/mpi/smpi_group.cpp index 89abd06941..bce418b066 100644 --- a/src/smpi/mpi/smpi_group.cpp +++ b/src/smpi/mpi/smpi_group.cpp @@ -73,10 +73,11 @@ int Group::rank(int index) { if (this == MPI_GROUP_EMPTY) return MPI_UNDEFINED; - if (index_to_rank_map_.find(index) == index_to_rank_map_.end()) - return MPI_UNDEFINED; - else + try { return index_to_rank_map_.at(index); + } catch (std::out_of_range& unfound) { + return MPI_UNDEFINED; + } } void Group::ref() diff --git a/src/surf/FileImpl.cpp b/src/surf/FileImpl.cpp index 53548500a5..e454c3a073 100644 --- a/src/surf/FileImpl.cpp +++ b/src/surf/FileImpl.cpp @@ -17,9 +17,9 @@ FileImpl::FileImpl(sg_storage_t st, std::string path, std::string mount) : path_ location_ = st->getImpl(); std::map* content = location_->getContent(); // if file does not exist create an empty file - if (content->find(path) != content->end()) + try { size_ = content->at(path); - else { + } catch (std::out_of_range& unfound) { size_ = 0; content->insert({path, size_}); XBT_DEBUG("File '%s' was not found, file created.", path.c_str()); @@ -96,13 +96,13 @@ void FileImpl::move(const char* fullpath) /* Check if the new full path is on the same mount point */ if (not strncmp(mount_point_.c_str(), fullpath, mount_point_.size())) { std::map* content = location_->getContent(); - if (content->find(path_) != content->end()) { // src file exists + try { // src file exists sg_size_t new_size = content->at(path_); content->erase(path_); std::string path = std::string(fullpath).substr(mount_point_.size(), strlen(fullpath)); content->insert({path.c_str(), new_size}); XBT_DEBUG("Move file from %s to %s, size '%llu'", path_.c_str(), fullpath, new_size); - } else { + } catch (std::out_of_range& unfound) { XBT_WARN("File %s doesn't exist", path_.c_str()); } } else { diff --git a/src/surf/network_interface.cpp b/src/surf/network_interface.cpp index 81c55be208..fdb99fcd11 100644 --- a/src/surf/network_interface.cpp +++ b/src/surf/network_interface.cpp @@ -22,9 +22,11 @@ namespace simgrid { LinkImpl* LinkImpl::byName(const char* name) { - if (links->find(name) == links->end()) + try { + return links->at(name); + } catch (std::out_of_range& unfound) { return nullptr; - return links->at(name); + } } /** @brief Returns the amount of links in the platform */ int LinkImpl::linksCount() diff --git a/src/xbt/xbt_replay.cpp b/src/xbt/xbt_replay.cpp index b9b6612af1..74bc5a3f71 100644 --- a/src/xbt/xbt_replay.cpp +++ b/src/xbt/xbt_replay.cpp @@ -78,9 +78,9 @@ static ReplayAction* get_action(char* name) } else { // Else, I have to store it for the relevant colleague std::queue* otherqueue = nullptr; - if (action_queues.find(evtname) != action_queues.end()) + try { otherqueue = action_queues.at(evtname); - else { // Damn. Create the queue of that guy + } catch (std::out_of_range& unfound) { // Damn. Create the queue of that guy otherqueue = new std::queue(); action_queues.insert({evtname, otherqueue}); } -- 2.20.1