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 2b4a84d6aab65ac49fcc4958c5070cd238a2bb02..148680148ea4e4508578f42c6313752b7d6cd467 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_};
     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;
           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 (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;
           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 (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;
         bypassedRoute = bypassRoutes_.at(key);
         break;
+      } catch (std::out_of_range& unfound) {
+        // Do nothing
       }
     }
   }
       }
     }
   }
index ddc4b6d788e1087663df13ce31677c33492ac0fe..c2f242a75fb40d67c2b51d44da710f369781ed59 100644 (file)
@@ -83,21 +83,26 @@ void VivaldiZone::getLocalRoute(NetPoint* src, NetPoint* dst, sg_platf_route_cba
   }
 
   /* Retrieve the private links */
   }
 
   /* 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();
     }
     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();
     }
     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 */
   }
 
   /* Compute the extra latency due to the euclidean distance if needed */
index 0e983e8ffe9bf79c6f509fd0a62a8764aec00146..8df13cd410bdb9953391cb443a0b702f9a789abd 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)
 {
 /** @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 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)
 }
 /** @brief Fill the provided vector with all existing netpoints */
 void Engine::getNetpointList(std::vector<simgrid::kernel::routing::NetPoint*>* list)
index a1520f952b3ee2443f8b11616f6e5051ac0ccda9..f39b2a29696628301a86abfdeaf70b18748324b8 100644 (file)
@@ -87,9 +87,11 @@ Host* Host::by_name_or_null(const char* name)
 }
 Host* Host::by_name_or_null(std::string 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 nullptr;
-  return host_list.at(name);
+  }
 }
 
 Host *Host::current(){
 }
 
 Host *Host::current(){
index 25902cea16331a68b690668b209a10a395da9a42..d9b877d4ada5ae55afdee10b566d3ea3ce230083 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)
 {
 /** @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 nullptr;
-  return simix_global->process_list.at(PID);
+  }
 }
 
 /** @brief returns a dynar containing all currently existing processes */
 }
 
 /** @brief returns a dynar containing all currently existing processes */
index 89abd06941fcb5b40abcf05ac8f93db22a0e165e..bce418b0662286e6f650a03e2a5d58211a4a38d4 100644 (file)
@@ -73,10 +73,11 @@ int Group::rank(int index)
 {
   if (this == MPI_GROUP_EMPTY)
     return MPI_UNDEFINED;
 {
   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);
     return index_to_rank_map_.at(index);
+  } catch (std::out_of_range& unfound) {
+    return MPI_UNDEFINED;
+  }
 }
 
 void Group::ref()
 }
 
 void Group::ref()
index 53548500a5a737dd5fcdd761a9ec9acc7cb21987..e454c3a073552f12cd248b5748cf779292dedcb9 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
   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);
     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());
     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();
   /* 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);
       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 {
       XBT_WARN("File %s doesn't exist", path_.c_str());
     }
   } else {
index 81c55be208e3194525be004c7a62d06eca59bd3a..fdb99fcd11302fd47280f9c29b205bc2f2b7ee98 100644 (file)
@@ -22,9 +22,11 @@ namespace simgrid {
 
   LinkImpl* LinkImpl::byName(const char* name)
   {
 
   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 nullptr;
-    return links->at(name);
+    }
   }
   /** @brief Returns the amount of links in the platform */
   int LinkImpl::linksCount()
   }
   /** @brief Returns the amount of links in the platform */
   int LinkImpl::linksCount()
index b9b6612af19ec9318ef74b02d74d2304b270471c..74bc5a3f7125f14d3891ae2490f32aabd211a599 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;
       } 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);
           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});
         }
           otherqueue = new std::queue<ReplayAction*>();
           action_queues.insert({evtname, otherqueue});
         }