From: Arnaud Giersch Date: Wed, 2 Aug 2017 13:49:50 +0000 (+0200) Subject: Avoid costly exceptions when looking into a map. X-Git-Tag: v3_17~283 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/093b834960aa7457d220feacad9542c4606ed8a0 Avoid costly exceptions when looking into a map. --- diff --git a/src/kernel/routing/NetZoneImpl.cpp b/src/kernel/routing/NetZoneImpl.cpp index 5f846b1017..b26cf092b6 100644 --- a/src/kernel/routing/NetZoneImpl.cpp +++ b/src/kernel/routing/NetZoneImpl.cpp @@ -260,20 +260,18 @@ 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_}; - try { - bypassedRoute = bypassRoutes_.at(key); + auto bpr = bypassRoutes_.find(key); + if (bpr != bypassRoutes_.end()) { + bypassedRoute = bpr->second; 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_}; - try { - bypassedRoute = bypassRoutes_.at(key); + auto bpr = bypassRoutes_.find(key); + if (bpr != bypassRoutes_.end()) { + bypassedRoute = bpr->second; break; - } catch (std::out_of_range& unfound) { - // Do nothing } } } @@ -283,11 +281,10 @@ 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_}; - try { - bypassedRoute = bypassRoutes_.at(key); + auto bpr = bypassRoutes_.find(key); + if (bpr != bypassRoutes_.end()) { + bypassedRoute = bpr->second; break; - } catch (std::out_of_range& unfound) { - // Do nothing } } } diff --git a/src/kernel/routing/VivaldiZone.cpp b/src/kernel/routing/VivaldiZone.cpp index 8ebb7fa996..80c785fdf0 100644 --- a/src/kernel/routing/VivaldiZone.cpp +++ b/src/kernel/routing/VivaldiZone.cpp @@ -86,25 +86,27 @@ void VivaldiZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cba } /* Retrieve the private links */ - try { - std::pair info = privateLinks_.at(src->id()); + auto src_link = privateLinks_.find(src->id()); + if (src_link != privateLinks_.end()) { + std::pair info = src_link->second; if (info.first) { route->link_list->push_back(info.first); if (lat) *lat += info.first->latency(); } - } catch (std::out_of_range& unfound) { + } else { XBT_DEBUG("Source of private link (%u) doesn't exist", src->id()); } - try { - std::pair info = privateLinks_.at(dst->id()); + auto dst_link = privateLinks_.find(dst->id()); + if (dst_link != privateLinks_.end()) { + std::pair info = dst_link->second; if (info.second) { route->link_list->push_back(info.second); if (lat) *lat += info.second->latency(); } - } catch (std::out_of_range& unfound) { + } else { XBT_DEBUG("Destination of private link (%u) doesn't exist", dst->id()); } diff --git a/src/s4u/s4u_engine.cpp b/src/s4u/s4u_engine.cpp index e061713a9c..4c13de0741 100644 --- a/src/s4u/s4u_engine.cpp +++ b/src/s4u/s4u_engine.cpp @@ -128,11 +128,8 @@ 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) { - try { - return pimpl->netpoints_.at(name); - } catch (std::out_of_range& unfound) { - return nullptr; - } + auto netp = pimpl->netpoints_.find(name); + return netp == pimpl->netpoints_.end() ? nullptr : netp->second; } /** @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 cd083646d2..5439ce8887 100644 --- a/src/s4u/s4u_host.cpp +++ b/src/s4u/s4u_host.cpp @@ -87,11 +87,8 @@ Host* Host::by_name_or_null(const char* name) } Host* Host::by_name_or_null(std::string name) { - try { - return host_list.at(name); - } catch (std::out_of_range& unfound) { - return nullptr; - } + auto host = host_list.find(name); + return host == host_list.end() ? nullptr : host->second; } Host *Host::current(){ diff --git a/src/simdag/sd_daxloader.cpp b/src/simdag/sd_daxloader.cpp index 843e43e312..f18920c2ad 100644 --- a/src/simdag/sd_daxloader.cpp +++ b/src/simdag/sd_daxloader.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2009-2016. The SimGrid Team. +/* Copyright (c) 2009-2017. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -318,9 +318,10 @@ void STag_dax__uses() static SD_task_t current_child; void STag_dax__child() { - try { - current_child = jobs.at(A_dax__child_ref); - } catch (std::out_of_range& unfound) { + auto job = jobs.find(A_dax__child_ref); + if (job != jobs.end()) { + current_child = job->second; + } else { throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) + ": Asked to add dependencies to the non-existent " + A_dax__child_ref + "task"); } @@ -333,11 +334,12 @@ void ETag_dax__child() void STag_dax__parent() { - try { - SD_task_t parent = jobs.at(A_dax__parent_ref); + auto job = jobs.find(A_dax__parent_ref); + if (job != jobs.end()) { + SD_task_t parent = job->second; SD_task_dependency_add(nullptr, nullptr, parent, current_child); XBT_DEBUG("Control-flow dependency from %s to %s", current_child->name, parent->name); - } catch (std::out_of_range& unfound) { + } else { throw std::out_of_range(std::string("Parse error on line ") + std::to_string(dax_lineno) + ": Asked to add a dependency from " + current_child->name + " to " + A_dax__parent_ref + ", but " + A_dax__parent_ref + " does not exist"); diff --git a/src/simdag/sd_dotloader.cpp b/src/simdag/sd_dotloader.cpp index b2008872e7..0960689062 100644 --- a/src/simdag/sd_dotloader.cpp +++ b/src/simdag/sd_dotloader.cpp @@ -103,9 +103,10 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched if ((performer != -1 && order != -1) && performer < static_cast(sg_host_count())) { /* required parameters are given and less performers than hosts are required */ XBT_DEBUG ("Task '%s' is scheduled on workstation '%d' in position '%d'", task->name, performer, order); - try { - computer = computers.at(char_performer); - } catch (std::out_of_range& unfound) { + auto comp = computers.find(char_performer); + if (comp != computers.end()) { + computer = comp->second; + } else { computer = new std::vector; computers.insert({char_performer, computer}); } diff --git a/src/simix/ActorImpl.cpp b/src/simix/ActorImpl.cpp index 6bd02caaa7..bd2ab82149 100644 --- a/src/simix/ActorImpl.cpp +++ b/src/simix/ActorImpl.cpp @@ -829,11 +829,8 @@ xbt_dynar_t SIMIX_process_get_runnable() /** @brief Returns the process from PID. */ smx_actor_t SIMIX_process_from_PID(aid_t PID) { - try { - return simix_global->process_list.at(PID); - } catch (std::out_of_range& unfound) { - return nullptr; - } + auto process = simix_global->process_list.find(PID); + return process == simix_global->process_list.end() ? nullptr : process->second; } /** @brief returns a dynar containing all currently existing processes */ diff --git a/src/smpi/include/smpi_keyvals.hpp b/src/smpi/include/smpi_keyvals.hpp index bc3e2af943..986540f4e9 100644 --- a/src/smpi/include/smpi_keyvals.hpp +++ b/src/smpi/include/smpi_keyvals.hpp @@ -109,11 +109,12 @@ template int Keyval::attr_get(int keyval, void* attr_value, int* fl *flag=0; return MPI_SUCCESS; } - try { - *static_cast(attr_value) = attributes()->at(keyval); + const auto& attribs = attributes(); + auto attr = attribs->find(keyval); + if (attr != attribs->end()) { + *static_cast(attr_value) = attr->second; *flag=1; - } - catch (const std::out_of_range& oor) { + } else { *flag=0; } return MPI_SUCCESS; @@ -140,12 +141,13 @@ template void Keyval::cleanup_attr(){ if (not attributes()->empty()) { int flag=0; for(auto it : attributes_){ - try{ - smpi_key_elem elem = T::keyvals_.at(it.first); + auto elm = T::keyvals_.find(it.first); + if (elm != T::keyvals_.end()) { + smpi_key_elem elem = elm->second; if(elem != nullptr){ call_deleter((T*)this, elem, it.first,it.second,&flag); } - }catch(const std::out_of_range& oor) { + } else { //already deleted, not a problem; flag=0; } diff --git a/src/smpi/mpi/smpi_comm.cpp b/src/smpi/mpi/smpi_comm.cpp index 23a4bd6ad7..051be8cba8 100644 --- a/src/smpi/mpi/smpi_comm.cpp +++ b/src/smpi/mpi/smpi_comm.cpp @@ -452,11 +452,9 @@ MPI_Comm Comm::f2c(int id) { return MPI_COMM_WORLD; } else if(F2C::f2c_lookup() != nullptr && id >= 0) { char key[KEY_SIZE]; - try { - return static_cast(F2C::f2c_lookup()->at(get_key_id(key, id))); - } catch (std::out_of_range& unfound) { - return MPI_COMM_NULL; - } + const auto& lookup = F2C::f2c_lookup(); + auto comm = lookup->find(get_key_id(key, id)); + return comm == lookup->end() ? MPI_COMM_NULL : static_cast(comm->second); } else { return MPI_COMM_NULL; } diff --git a/src/smpi/mpi/smpi_f2c.cpp b/src/smpi/mpi/smpi_f2c.cpp index ffd795fdbb..532f3e497a 100644 --- a/src/smpi/mpi/smpi_f2c.cpp +++ b/src/smpi/mpi/smpi_f2c.cpp @@ -90,11 +90,8 @@ F2C* F2C::f2c(int id) if(id >= 0){ char key[KEY_SIZE]; - try { - return f2c_lookup_->at(get_key(key, id)); - } catch (std::out_of_range& unfound) { - return nullptr; - } + auto comm = f2c_lookup_->find(get_key(key, id)); + return comm == f2c_lookup_->end() ? nullptr : comm->second; }else return nullptr; } diff --git a/src/smpi/mpi/smpi_group.cpp b/src/smpi/mpi/smpi_group.cpp index 8cc21c33e7..232076857d 100644 --- a/src/smpi/mpi/smpi_group.cpp +++ b/src/smpi/mpi/smpi_group.cpp @@ -73,11 +73,8 @@ int Group::rank(int index) { if (this == MPI_GROUP_EMPTY) return MPI_UNDEFINED; - try { - return index_to_rank_map_.at(index); - } catch (std::out_of_range& unfound) { - return MPI_UNDEFINED; - } + auto rank = index_to_rank_map_.find(index); + return rank == index_to_rank_map_.end() ? MPI_UNDEFINED : rank->second; } void Group::ref() diff --git a/src/smpi/mpi/smpi_info.cpp b/src/smpi/mpi/smpi_info.cpp index ff3c8535d8..3698e30d40 100644 --- a/src/smpi/mpi/smpi_info.cpp +++ b/src/smpi/mpi/smpi_info.cpp @@ -32,15 +32,16 @@ void Info::set(char *key, char *value){ int Info::get(char *key, int valuelen, char *value, int *flag){ *flag=false; - try { - std::string tmpvalue = map_.at(key); + auto val = map_.find(key); + if (val != map_.end()) { + std::string tmpvalue = val->second; memset(value, 0, valuelen); memcpy(value, tmpvalue.c_str(), (tmpvalue.length() + 1 < static_cast(valuelen)) ? tmpvalue.length() + 1 : valuelen); *flag=true; return MPI_SUCCESS; - } catch (std::out_of_range& unfound) { + } else { return MPI_ERR_INFO_KEY; } } @@ -71,11 +72,12 @@ int Info::get_nthkey(int n, char *key){ int Info::get_valuelen(char *key, int *valuelen, int *flag){ *flag=false; - try { - *valuelen = map_.at(key).length(); + auto val = map_.find(key); + if (val != map_.end()) { + *valuelen = val->second.length(); *flag=true; return MPI_SUCCESS; - } catch (std::out_of_range& unfound) { + } else { return MPI_ERR_INFO_KEY; } } diff --git a/src/surf/FileImpl.cpp b/src/surf/FileImpl.cpp index e454c3a073..fbb70a7e8f 100644 --- a/src/surf/FileImpl.cpp +++ b/src/surf/FileImpl.cpp @@ -17,9 +17,10 @@ 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 - try { - size_ = content->at(path); - } catch (std::out_of_range& unfound) { + auto sz = content->find(path); + if (sz != content->end()) { + size_ = sz->second; + } else { size_ = 0; content->insert({path, size_}); XBT_DEBUG("File '%s' was not found, file created.", path.c_str()); @@ -96,13 +97,14 @@ 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(); - try { // src file exists - sg_size_t new_size = content->at(path_); + auto sz = content->find(path_); + if (sz != content->end()) { // src file exists + sg_size_t new_size = sz->second; 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); - } catch (std::out_of_range& unfound) { + } else { XBT_WARN("File %s doesn't exist", path_.c_str()); } } else { diff --git a/src/surf/PropertyHolder.cpp b/src/surf/PropertyHolder.cpp index a6696aee1e..e7657a277d 100644 --- a/src/surf/PropertyHolder.cpp +++ b/src/surf/PropertyHolder.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2015. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -16,11 +16,8 @@ PropertyHolder::~PropertyHolder() { const char *PropertyHolder::getProperty(const char*key) { if (properties_ == nullptr) return nullptr; - try { - return properties_->at(key).c_str(); - } catch (std::out_of_range& unfound) { - return nullptr; - } + auto prop = properties_->find(key); + return prop == properties_->end() ? nullptr : prop->second.c_str(); } /** @brief Change the value of a given key in the property set */ diff --git a/src/surf/network_ib.cpp b/src/surf/network_ib.cpp index 6018e1d2dc..3293c9da28 100644 --- a/src/surf/network_ib.cpp +++ b/src/surf/network_ib.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2014-2015. The SimGrid Team. +/* Copyright (c) 2014-2017. The SimGrid Team. *All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -52,15 +52,17 @@ static void IB_action_init_callback(simgrid::surf::NetworkAction* action, simgri simgrid::surf::IBNode* act_src; simgrid::surf::IBNode* act_dst; - try { - act_src = ibModel->active_nodes.at(src->getName()); - } catch (std::out_of_range& unfound) { + auto asrc = ibModel->active_nodes.find(src->getName()); + if (asrc != ibModel->active_nodes.end()) { + act_src = asrc->second; + } else { throw std::out_of_range(std::string("Could not find '") + src->getCname() + "' active comms !"); } - try { - act_dst = ibModel->active_nodes.at(dst->getName()); - } catch (std::out_of_range& unfound) { + auto adst = ibModel->active_nodes.find(dst->getName()); + if (adst != ibModel->active_nodes.end()) { + act_dst = adst->second; + } else { throw std::out_of_range(std::string("Could not find '") + dst->getCname() + "' active comms !"); } diff --git a/src/surf/network_interface.cpp b/src/surf/network_interface.cpp index fdb99fcd11..2944bc3e04 100644 --- a/src/surf/network_interface.cpp +++ b/src/surf/network_interface.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2013-2015. The SimGrid Team. +/* Copyright (c) 2013-2017. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -22,11 +22,8 @@ namespace simgrid { LinkImpl* LinkImpl::byName(const char* name) { - try { - return links->at(name); - } catch (std::out_of_range& unfound) { - return nullptr; - } + auto link = links->find(name); + return link == links->end() ? nullptr : link->second; } /** @brief Returns the amount of links in the platform */ int LinkImpl::linksCount() diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index d0ef69e9f9..6813d3636c 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -354,9 +354,10 @@ void sg_platf_new_storage(StorageCreationArgs* storage) "Refusing to add a second storage named \"%s\"", storage->id.c_str()); simgrid::surf::StorageType* stype; - try { - stype = storage_types.at(storage->type_id); - } catch (std::out_of_range& unfound) { + auto st = storage_types.find(storage->type_id); + if (st != storage_types.end()) { + stype = st->second; + } else { xbt_die("No storage type '%s'", storage->type_id.c_str()); } diff --git a/src/xbt/config.cpp b/src/xbt/config.cpp index f451949b43..2b1788bf63 100644 --- a/src/xbt/config.cpp +++ b/src/xbt/config.cpp @@ -305,15 +305,17 @@ Config::~Config() inline ConfigurationElement* Config::getDictElement(const char* name) { - try { - return options.at(name); - } catch (std::out_of_range& unfound) { - try { - ConfigurationElement* res = aliases.at(name); + auto opt = options.find(name); + if (opt != options.end()) { + return opt->second; + } else { + auto als = aliases.find(name); + if (als != aliases.end()) { + ConfigurationElement* res = als->second; if (warn_for_aliases) XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, res->getKey().c_str()); return res; - } catch (std::out_of_range& missing_key) { + } else { throw simgrid::config::missing_key_error(std::string("Bad config key: ") + name); } } diff --git a/src/xbt/xbt_replay.cpp b/src/xbt/xbt_replay.cpp index 9571091105..7a35e2fdf8 100644 --- a/src/xbt/xbt_replay.cpp +++ b/src/xbt/xbt_replay.cpp @@ -78,9 +78,10 @@ static ReplayAction* get_action(char* name) } else { // Else, I have to store it for the relevant colleague std::queue* otherqueue = nullptr; - try { - otherqueue = action_queues.at(evtname); - } catch (std::out_of_range& unfound) { // Damn. Create the queue of that guy + auto act = action_queues.find(evtname); + if (act != action_queues.end()) { + otherqueue = act->second; + } else { // Damn. Create the queue of that guy otherqueue = new std::queue(); action_queues.insert({evtname, otherqueue}); }