Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
overall optimization of map usage
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Mon, 24 Jul 2017 08:51:56 +0000 (10:51 +0200)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Mon, 24 Jul 2017 08:51:56 +0000 (10:51 +0200)
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
src/kernel/routing/VivaldiZone.cpp
src/s4u/s4u_engine.cpp
src/s4u/s4u_host.cpp
src/simix/ActorImpl.cpp
src/smpi/mpi/smpi_group.cpp
src/surf/FileImpl.cpp
src/surf/network_interface.cpp
src/xbt/xbt_replay.cpp

index 2b4a84d..1486801 100644 (file)
@@ -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
       }
     }
   }
index ddc4b6d..c2f242a 100644 (file)
@@ -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<surf::LinkImpl*, surf::LinkImpl*> 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<surf::LinkImpl*, surf::LinkImpl*> 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 */
index 0e983e8..8df13cd 100644 (file)
@@ -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<simgrid::kernel::routing::NetPoint*>* list)
index a1520f9..f39b2a2 100644 (file)
@@ -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(){
index 25902ce..d9b877d 100644 (file)
@@ -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 */
index 89abd06..bce418b 100644 (file)
@@ -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()
index 5354850..e454c3a 100644 (file)
@@ -17,9 +17,9 @@ FileImpl::FileImpl(sg_storage_t st, std::string path, std::string mount) : path_
   location_ = st->getImpl();
   std::map<std::string, sg_size_t>* 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<std::string, sg_size_t>* 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 {
index 81c55be..fdb99fc 100644 (file)
@@ -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()
index b9b6612..74bc5a3 100644 (file)
@@ -78,9 +78,9 @@ static ReplayAction* get_action(char* name)
       } else {
         // Else, I have to store it for the relevant colleague
         std::queue<ReplayAction*>* 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<ReplayAction*>();
           action_queues.insert({evtname, otherqueue});
         }