Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Prefer using "try_emplace" (sonar, c++17).
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 25 Apr 2022 09:43:57 +0000 (11:43 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 25 Apr 2022 11:00:38 +0000 (13:00 +0200)
28 files changed:
examples/cpp/app-bittorrent/s4u-peer.cpp
examples/cpp/replay-io/s4u-replay-io.cpp
include/simgrid/plugins/file_system.h
src/dag/loaders.cpp
src/instr/instr_paje_containers.cpp
src/instr/instr_paje_types.cpp
src/kernel/resource/WifiLinkImpl.cpp
src/kernel/resource/profile/Profile.cpp
src/kernel/routing/ClusterZone.cpp
src/kernel/routing/DijkstraZone.cpp
src/kernel/routing/FatTreeZone.cpp
src/kernel/routing/NetZoneImpl.cpp
src/kernel/routing/StarZone.cpp
src/plugins/file_system/s4u_FileSystem.cpp
src/s4u/s4u_Engine.cpp
src/smpi/internals/smpi_bench.cpp
src/smpi/internals/smpi_config.cpp
src/smpi/internals/smpi_deployment.cpp
src/smpi/internals/smpi_global.cpp
src/smpi/internals/smpi_shared.cpp
src/smpi/internals/smpi_utils.cpp
src/smpi/mpi/smpi_datatype.cpp
src/surf/network_ib.cpp
src/surf/network_ns3.cpp
src/surf/sg_platf.cpp
src/surf/xml/surfxml_sax_cb.cpp
src/xbt/config.cpp
src/xbt/xbt_replay.cpp

index a2169a1..409418f 100644 (file)
@@ -121,7 +121,7 @@ bool Peer::getPeersFromTracker()
     // Add the peers the tracker gave us to our peer list.
     for (auto const& peer_id : answer->getPeers())
       if (id != peer_id)
-        connected_peers.emplace(peer_id, Connection(peer_id));
+        connected_peers.try_emplace(peer_id, Connection(peer_id));
   } catch (const simgrid::TimeoutException&) {
     XBT_DEBUG("Timeout expired when requesting peers to tracker");
     return false;
@@ -317,7 +317,7 @@ void Peer::handleMessage()
       // Check if the peer is in our connection list.
       if (remote_peer == nullptr) {
         XBT_DEBUG("This peer %d was unknown, answer to its handshake", message->peer_id);
-        connected_peers.emplace(message->peer_id, Connection(message->peer_id));
+        connected_peers.try_emplace(message->peer_id, Connection(message->peer_id));
         sendMessage(message->return_mailbox, MessageType::HANDSHAKE, message_size(MessageType::HANDSHAKE));
       }
       // Send our bitfield to the peer
index 96ee3f7..5b586fd 100644 (file)
@@ -63,7 +63,7 @@ public:
 
     ACT_DEBUG("Entering Open: %s (filename: %s)", NAME.c_str(), file_name.c_str());
     auto* file = sg4::File::open(file_name, nullptr);
-    opened_files.emplace(full_name, file);
+    opened_files.try_emplace(full_name, file);
 
     log_action(action, sg4::Engine::get_clock() - clock);
   }
index 8498c1e..28c31ea 100644 (file)
@@ -152,10 +152,7 @@ public:
   std::map<std::string, sg_size_t, std::less<>>* get_content() const { return content_.get(); }
   const char* get_mount_point() const { return mount_point_.c_str(); }
   const char* get_mount_point(s4u::Host* remote_host) { return remote_mount_points_[remote_host].c_str(); }
-  void add_remote_mount(Host* host, const std::string& mount_point)
-  {
-    remote_mount_points_.insert({host, mount_point});
-  }
+  void add_remote_mount(Host* host, const std::string& mount_point);
   sg_size_t get_size() const { return size_; }
   sg_size_t get_used_size() const { return used_size_; }
   void decr_used_size(sg_size_t size);
index 071e446..5a31331 100644 (file)
@@ -202,7 +202,7 @@ std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
     if (activities.find(name) == activities.end()) {
       XBT_DEBUG("See <Exec id = %s amount = %.0f>", name.c_str(), amount);
       act = Exec::init()->set_name(name)->set_flops_amount(amount)->vetoable_start();
-      activities.insert({name, act});
+      activities.try_emplace(name, act);
       if (name != "root" && name != "end")
         dag.push_back(act);
     } else {
@@ -244,7 +244,7 @@ std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename)
           act = Comm::sendto_init()->set_name(name)->set_payload_size(size)->vetoable_start();
           src->add_successor(act);
           act->add_successor(dst);
-          activities.insert({name, act});
+          activities.try_emplace(name, act);
           dag.push_back(act);
         } else {
           XBT_WARN("Comm '%s' is defined more than once", name.c_str());
@@ -315,7 +315,7 @@ void STag_dax__job()
     runtime *= 4200000000.; /* Assume that timings were done on a 4.2GFlops machine. I mean, why not? */
     XBT_DEBUG("See <job id=%s runtime=%s %.0f>", A_dax__job_id, A_dax__job_runtime, runtime);
     simgrid::s4u::current_job = simgrid::s4u::Exec::init()->set_name(name)->set_flops_amount(runtime)->vetoable_start();
-    simgrid::s4u::jobs.insert({A_dax__job_id, simgrid::s4u::current_job});
+    simgrid::s4u::jobs.try_emplace(A_dax__job_id, simgrid::s4u::current_job);
     simgrid::s4u::result.push_back(simgrid::s4u::current_job);
   } catch (const std::invalid_argument&) {
     throw std::invalid_argument(std::string("Parse error: ") + A_dax__job_runtime + " is not a double");
index 3802ba9..3d680b2 100644 (file)
@@ -23,7 +23,7 @@ NetZoneContainer::NetZoneContainer(const std::string& name, unsigned int level,
   if (parent_) {
     std::string type_name = std::string("L") + std::to_string(level);
     type_                 = parent_->type_->by_name_or_create<ContainerType>(type_name);
-    parent_->children_.insert({get_name(), this});
+    parent_->children_.try_emplace(get_name(), this);
     on_creation(*this);
   } else {
     type_         = new ContainerType("0");
@@ -57,13 +57,13 @@ Container::Container(const std::string& name, const std::string& type_name, Cont
 
     if (not type_name.empty()) {
       type_ = parent_->type_->by_name_or_create<ContainerType>(type_name);
-      parent_->children_.insert({name_, this});
+      parent_->children_.try_emplace(name_, this);
       on_creation(*this);
     }
   }
 
   //register all kinds by name
-  if (not all_containers_.emplace(name_, this).second)
+  if (not all_containers_.try_emplace(name_, this).second)
     throw TracingError(XBT_THROW_POINT,
                        xbt::string_printf("container %s already present in all_containers_", get_cname()));
 
index 600e6e3..0e31281 100644 (file)
@@ -137,7 +137,7 @@ void ValueType::add_entity_value(const std::string& name, const std::string& col
   auto it = values_.find(name);
   if (it == values_.end()) {
     XBT_DEBUG("new value %s, child of %s", name.c_str(), get_cname());
-    values_.emplace(name, EntityValue(name, color, this));
+    values_.try_emplace(name, EntityValue(name, color, this));
   }
 }
 
index f61fd12..640bc2b 100644 (file)
@@ -28,7 +28,7 @@ WifiLinkImpl::WifiLinkImpl(const std::string& name, const std::vector<double>& b
 
 void WifiLinkImpl::set_host_rate(const s4u::Host* host, int rate_level)
 {
-  auto insert_done = host_rates_.insert(std::make_pair(host->get_name(), rate_level));
+  auto insert_done = host_rates_.try_emplace(host->get_name(), rate_level);
   if (not insert_done.second)
     insert_done.first->second = rate_level;
 
index a00d6bc..dfb68d1 100644 (file)
@@ -71,7 +71,7 @@ Profile::Profile(const std::string& name, const std::function<ProfileBuilder::Up
     : name(name), cb(cb), repeat_delay(repeat_delay)
 {
   xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str());
-  trace_list.insert({name,this});
+  trace_list.try_emplace(name, this);
   get_enough_events(0);
 }
 
index 4a51f60..bd5ac1f 100644 (file)
@@ -43,7 +43,7 @@ void ClusterBase::set_link_characteristics(double bw, double lat, s4u::Link::Sha
 void ClusterBase::add_private_link_at(unsigned long position,
                                       std::pair<resource::StandardLinkImpl*, resource::StandardLinkImpl*> link)
 {
-  private_links_.insert({position, link});
+  private_links_.try_emplace(position, link);
 }
 
 void ClusterBase::set_gateway(unsigned long position, NetPoint* gateway)
index e8d23ae..150bf22 100644 (file)
@@ -113,7 +113,7 @@ void DijkstraZone::get_local_route(const NetPoint* src, const NetPoint* dst, Rou
     insert_link_latency(route->link_list_, e_route->link_list_, lat);
   }
 
-  auto elm                             = route_cache_.emplace(src_id, std::vector<unsigned long>());
+  auto elm                             = route_cache_.try_emplace(src_id, std::vector<unsigned long>());
   std::vector<unsigned long>& pred_arr = elm.first->second;
 
   if (elm.second) { /* new element was inserted (not cached mode, or cache miss) */
index 3c24baa..7f7337d 100644 (file)
@@ -341,7 +341,7 @@ void FatTreeZone::add_processing_node(int id, resource::StandardLinkImpl* limite
   position++;
   newNode->parents.resize(static_cast<size_t>(this->num_parents_per_node_[0]) * this->num_port_lower_level_[0]);
   newNode->label.resize(this->levels_);
-  this->compute_nodes_.insert(make_pair(id, newNode));
+  this->compute_nodes_.try_emplace(id, newNode);
   this->nodes_.emplace_back(newNode);
 }
 
index cc37733..703a97d 100644 (file)
@@ -396,7 +396,7 @@ void NetZoneImpl::add_bypass_route(NetPoint* src, NetPoint* dst, NetPoint* gw_sr
   newRoute->links.insert(newRoute->links.end(), begin(converted_list), end(converted_list));
 
   /* Store it */
-  bypass_routes_.insert({{src, dst}, newRoute});
+  bypass_routes_.try_emplace({src, dst}, newRoute);
 }
 
 /** @brief Get the common ancestor and its first children in each line leading to src and dst
index 489ecd9..6d4b0f7 100644 (file)
@@ -170,7 +170,7 @@ void StarZone::do_seal()
 {
   /* add default empty links if nothing was configured by user */
   for (auto const& node : get_vertices()) {
-    auto route = routes_.emplace(node->id(), StarRoute());
+    auto route = routes_.try_emplace(node->id(), StarRoute());
     if (route.second) {
       route.first->second.links_down_set = true;
       route.first->second.links_up_set   = true;
index be2ab2b..98acd59 100644 (file)
@@ -386,6 +386,11 @@ std::map<std::string, sg_size_t, std::less<>>* FileSystemDiskExt::parse_content(
   return parse_content;
 }
 
+void FileSystemDiskExt::add_remote_mount(Host* host, const std::string& mount_point)
+{
+  remote_mount_points_.try_emplace(host, mount_point);
+}
+
 void FileSystemDiskExt::decr_used_size(sg_size_t size)
 {
   simgrid::kernel::actor::simcall_answered([this, size] { used_size_ -= size; });
index 858ee1f..76166e0 100644 (file)
@@ -268,7 +268,7 @@ Mailbox* Engine::mailbox_by_name_or_create(const std::string& name) const
 {
   /* two actors may have pushed the same mbox_create simcall at the same time */
   kernel::activity::MailboxImpl* mbox = kernel::actor::simcall_answered([&name, this] {
-    auto m = pimpl->mailboxes_.emplace(name, nullptr);
+    auto m = pimpl->mailboxes_.try_emplace(name, nullptr);
     if (m.second) {
       m.first->second = new kernel::activity::MailboxImpl(name);
       XBT_DEBUG("Creating a mailbox at %p with name %s", m.first->second, name.c_str());
index d8fd0ec..6da2855 100644 (file)
@@ -312,16 +312,16 @@ void smpi_sample_1(int global, const char *file, const char *tag, int iters, dou
     smpi_process()->set_sampling(1);
   }
 
-  auto insert = samples.emplace(loc, LocalData{
-                                         threshold, // threshold
-                                         0.0,       // relstderr
-                                         0.0,       // mean
-                                         0.0,       // sum
-                                         0.0,       // sum_pow2
-                                         iters,     // iters
-                                         0,         // count
-                                         true       // benching (if we have no data, we need at least one)
-                                     });
+  auto insert = samples.try_emplace(loc, LocalData{
+                                             threshold, // threshold
+                                             0.0,       // relstderr
+                                             0.0,       // mean
+                                             0.0,       // sum
+                                             0.0,       // sum_pow2
+                                             iters,     // iters
+                                             0,         // count
+                                             true       // benching (if we have no data, we need at least one)
+                                         });
   if (insert.second) {
     XBT_DEBUG("XXXXX First time ever on benched nest %s.", loc.c_str());
     xbt_assert(threshold > 0 || iters > 0,
index 55b6c77..e459692 100644 (file)
@@ -104,7 +104,7 @@ simgrid::config::Flag<std::string> _smpi_cfg_comp_adjustment_file{
           auto end             = std::next(tok.begin());
           std::string location = *it;
           boost::trim(location);
-          location2speedup.insert(std::pair<std::string, double>(location, std::stod(*end)));
+          location2speedup.try_emplace(location, std::stod(*end));
         }
       }
     }};
index 929a7be..06f9d85 100644 (file)
@@ -56,9 +56,7 @@ void SMPI_app_instance_register(const char *name, xbt_main_func_t code, int num_
   if (code != nullptr) // When started with smpirun, we will not execute a function
     simgrid::s4u::Engine::get_instance()->register_function(name, code);
 
-  Instance instance(num_processes);
-
-  smpi_instances.insert(std::pair<std::string, Instance>(name, instance));
+  smpi_instances.try_emplace(name, Instance(num_processes));
 }
 
 void smpi_deployment_register_process(const std::string& instance_id, int rank, const simgrid::s4u::Actor* actor)
index 24ae088..405d985 100644 (file)
@@ -278,7 +278,7 @@ static void smpi_init_papi()
     std::string unit_name    = *(event_tokens.begin());
     papi_process_data config = {.counter_data = std::move(counters2values), .event_set = event_set};
 
-    units2papi_setup.insert(std::make_pair(unit_name, std::move(config)));
+    units2papi_setup.try_emplace(unit_name, std::move(config));
   }
 #endif
 }
index 4f9658b..4edd30f 100644 (file)
@@ -126,7 +126,7 @@ static void *smpi_shared_malloc_local(size_t size, const char *file, int line)
 {
   void* mem;
   smpi_source_location loc(file, line);
-  auto res = allocs.insert(std::make_pair(loc, shared_data_t()));
+  auto res  = allocs.try_emplace(loc, shared_data_t());
   auto data = res.first;
   if (res.second) {
     // The new element was inserted.
index 77436cc..591b33d 100644 (file)
@@ -121,7 +121,7 @@ void account_malloc_size(size_t size, const std::string& file, int line, void* p
     metadata.line = line;
     metadata.numcall = 1;
     metadata.file    = file;
-    allocs.emplace(ptr, metadata);
+    allocs.try_emplace(ptr, metadata);
 
     total_malloc_size += size;
     if(size > max_malloc.size){
@@ -184,7 +184,7 @@ static void print_leaked_handles()
       key += " at " + elem.second->call_location();
     else
       display_advice = true;
-    auto result      = count.emplace(key, 1);
+    auto result = count.try_emplace(key, 1);
     if (result.second == false)
       result.first->second++;
   }
@@ -232,7 +232,7 @@ static void print_leaked_buffers()
     std::string key = "leaked allocations";
     if (not xbt_log_no_loc)
       key = elem.second.file + ":" + std::to_string(elem.second.line) + ": " + key;
-    auto result = leaks_aggreg.emplace(key, buff_leak{1, elem.second.size, elem.second.size, elem.second.size});
+    auto result = leaks_aggreg.try_emplace(key, buff_leak{1, elem.second.size, elem.second.size, elem.second.size});
     if (result.second == false) {
       result.first->second.count++;
       result.first->second.total_size += elem.second.size;
@@ -354,7 +354,7 @@ int check_collectives_ordering(MPI_Comm comm, const std::string& call)
   comm->increment_collectives_count();
   auto vec = collective_calls.find(comm->id());
   if (vec == collective_calls.end()) {
-    collective_calls.emplace(comm->id(), std::vector<std::string>{call});
+    collective_calls.try_emplace(comm->id(), std::vector<std::string>{call});
   } else {
     // are we the first ? add the call
     if (vec->second.size() == count) {
index d5f3d21..d9bf6de 100644 (file)
@@ -128,7 +128,7 @@ Datatype::Datatype(int size, MPI_Aint lb, MPI_Aint ub, int flags) : size_(size),
 Datatype::Datatype(const char* name, int ident, int size, MPI_Aint lb, MPI_Aint ub, int flags)
     : name_(name), id(std::to_string(ident)), size_(size), lb_(lb), ub_(ub), flags_(flags), refcount_(0)
 {
-  id2type_lookup.insert({id, this});
+  id2type_lookup.try_emplace(id, this);
 #if SIMGRID_HAVE_MC
   if(MC_is_active())
     MC_ignore(&refcount_, sizeof refcount_);
index 1555059..c6362f6 100644 (file)
@@ -53,7 +53,7 @@ void NetworkIBModel::IB_create_host_callback(s4u::Host const& host)
 {
   static int id = 0;
   auto* ibModel = static_cast<NetworkIBModel*>(host.get_netpoint()->get_englobing_zone()->get_network_model().get());
-  ibModel->active_nodes.emplace(host.get_name(), IBNode(id));
+  ibModel->active_nodes.try_emplace(host.get_name(), IBNode(id));
   id++;
 }
 
index 3fbd4ca..a02a956 100644 (file)
@@ -545,8 +545,8 @@ NetworkNS3Action::NetworkNS3Action(Model* model, double totalBytes, s4u::Host* s
   XBT_DEBUG("Create socket %s for a flow of %.0f Bytes from %s to %s with Interface %s",
             transform_socket_ptr(sock).c_str(), totalBytes, src->get_cname(), dst->get_cname(), addr.c_str());
 
-  flow_from_sock.insert({transform_socket_ptr(sock), new SgFlow(static_cast<uint32_t>(totalBytes), this)});
-  sink_from_sock.insert({transform_socket_ptr(sock), apps});
+  flow_from_sock.try_emplace(transform_socket_ptr(sock), new SgFlow(static_cast<uint32_t>(totalBytes), this));
+  sink_from_sock.try_emplace(transform_socket_ptr(sock), apps);
 
   sock->Bind(ns3::InetSocketAddress(port_number));
   ns3::Simulator::ScheduleNow(&start_flow, sock, addr.c_str(), port_number);
index a149825..62b345e 100644 (file)
@@ -601,5 +601,5 @@ void sg_platf_new_trace(simgrid::kernel::routing::ProfileCreationArgs* args)
                args->id.c_str());
     profile = simgrid::kernel::profile::ProfileBuilder::from_string(args->id, args->pc_data, args->periodicity);
   }
-  traces_set_list.insert({args->id, profile});
+  traces_set_list.try_emplace(args->id, profile);
 }
index f3773aa..7b0e782 100644 (file)
@@ -225,7 +225,7 @@ void ETag_surfxml_platform(){
 
 void STag_surfxml_prop()
 {
-  property_sets.back().insert({A_surfxml_prop_id, A_surfxml_prop_value});
+  property_sets.back().try_emplace(A_surfxml_prop_id, A_surfxml_prop_value);
   XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
             &(property_sets.back()));
 }
index d61be87..c2a5fdb 100644 (file)
@@ -310,7 +310,7 @@ void Config::alias(const std::string& realname, const std::string& aliasname)
   xbt_assert(aliases.find(aliasname) == aliases.end(), "Alias '%s' already.", aliasname.c_str());
   ConfigurationElement* element = this->get_dict_element(realname);
   xbt_assert(element, "Cannot define an alias to the non-existing option '%s'.", realname.c_str());
-  this->aliases.insert({aliasname, element});
+  this->aliases.try_emplace(aliasname, element);
 }
 
 /** @brief Displays the declared aliases and their replacement */
index c64910a..41c6d57 100644 (file)
@@ -81,7 +81,7 @@ static ReplayAction* get_action(const char* name)
         otherqueue = act->second;
       } else { // Damn. Create the queue of that guy
         otherqueue = new std::queue<ReplayAction*>();
-        action_queues.insert({evtname, otherqueue});
+        action_queues.try_emplace(evtname, otherqueue);
       }
       otherqueue->push(action);
     }