From db9ef2223acb402e44eec406541e671bfebd5ade Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Mon, 25 Apr 2022 11:43:57 +0200 Subject: [PATCH] Prefer using "try_emplace" (sonar, c++17). --- examples/cpp/app-bittorrent/s4u-peer.cpp | 4 ++-- examples/cpp/replay-io/s4u-replay-io.cpp | 2 +- include/simgrid/plugins/file_system.h | 5 +---- src/dag/loaders.cpp | 6 +++--- src/instr/instr_paje_containers.cpp | 6 +++--- src/instr/instr_paje_types.cpp | 2 +- src/kernel/resource/WifiLinkImpl.cpp | 2 +- src/kernel/resource/profile/Profile.cpp | 2 +- src/kernel/routing/ClusterZone.cpp | 2 +- src/kernel/routing/DijkstraZone.cpp | 2 +- src/kernel/routing/FatTreeZone.cpp | 2 +- src/kernel/routing/NetZoneImpl.cpp | 2 +- src/kernel/routing/StarZone.cpp | 2 +- src/plugins/file_system/s4u_FileSystem.cpp | 5 +++++ src/s4u/s4u_Engine.cpp | 2 +- src/smpi/internals/smpi_bench.cpp | 20 ++++++++++---------- src/smpi/internals/smpi_config.cpp | 2 +- src/smpi/internals/smpi_deployment.cpp | 4 +--- src/smpi/internals/smpi_global.cpp | 2 +- src/smpi/internals/smpi_shared.cpp | 2 +- src/smpi/internals/smpi_utils.cpp | 8 ++++---- src/smpi/mpi/smpi_datatype.cpp | 2 +- src/surf/network_ib.cpp | 2 +- src/surf/network_ns3.cpp | 4 ++-- src/surf/sg_platf.cpp | 2 +- src/surf/xml/surfxml_sax_cb.cpp | 2 +- src/xbt/config.cpp | 2 +- src/xbt/xbt_replay.cpp | 2 +- 28 files changed, 50 insertions(+), 50 deletions(-) diff --git a/examples/cpp/app-bittorrent/s4u-peer.cpp b/examples/cpp/app-bittorrent/s4u-peer.cpp index a2169a1cdf..409418fb96 100644 --- a/examples/cpp/app-bittorrent/s4u-peer.cpp +++ b/examples/cpp/app-bittorrent/s4u-peer.cpp @@ -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 diff --git a/examples/cpp/replay-io/s4u-replay-io.cpp b/examples/cpp/replay-io/s4u-replay-io.cpp index 96ee3f778c..5b586fd012 100644 --- a/examples/cpp/replay-io/s4u-replay-io.cpp +++ b/examples/cpp/replay-io/s4u-replay-io.cpp @@ -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); } diff --git a/include/simgrid/plugins/file_system.h b/include/simgrid/plugins/file_system.h index 8498c1ea1e..28c31ea4d9 100644 --- a/include/simgrid/plugins/file_system.h +++ b/include/simgrid/plugins/file_system.h @@ -152,10 +152,7 @@ public: std::map>* 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); diff --git a/src/dag/loaders.cpp b/src/dag/loaders.cpp index 071e446de2..5a313317b0 100644 --- a/src/dag/loaders.cpp +++ b/src/dag/loaders.cpp @@ -202,7 +202,7 @@ std::vector create_DAG_from_dot(const std::string& filename) if (activities.find(name) == activities.end()) { XBT_DEBUG("See ", 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 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 ", 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"); diff --git a/src/instr/instr_paje_containers.cpp b/src/instr/instr_paje_containers.cpp index 3802ba9376..3d680b27b1 100644 --- a/src/instr/instr_paje_containers.cpp +++ b/src/instr/instr_paje_containers.cpp @@ -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(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(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())); diff --git a/src/instr/instr_paje_types.cpp b/src/instr/instr_paje_types.cpp index 600e6e322b..0e31281001 100644 --- a/src/instr/instr_paje_types.cpp +++ b/src/instr/instr_paje_types.cpp @@ -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)); } } diff --git a/src/kernel/resource/WifiLinkImpl.cpp b/src/kernel/resource/WifiLinkImpl.cpp index f61fd12ed0..640bc2b252 100644 --- a/src/kernel/resource/WifiLinkImpl.cpp +++ b/src/kernel/resource/WifiLinkImpl.cpp @@ -28,7 +28,7 @@ WifiLinkImpl::WifiLinkImpl(const std::string& name, const std::vector& 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; diff --git a/src/kernel/resource/profile/Profile.cpp b/src/kernel/resource/profile/Profile.cpp index a00d6bc40f..dfb68d1758 100644 --- a/src/kernel/resource/profile/Profile.cpp +++ b/src/kernel/resource/profile/Profile.cpp @@ -71,7 +71,7 @@ Profile::Profile(const std::string& name, const std::function link) { - private_links_.insert({position, link}); + private_links_.try_emplace(position, link); } void ClusterBase::set_gateway(unsigned long position, NetPoint* gateway) diff --git a/src/kernel/routing/DijkstraZone.cpp b/src/kernel/routing/DijkstraZone.cpp index e8d23aeb19..150bf22685 100644 --- a/src/kernel/routing/DijkstraZone.cpp +++ b/src/kernel/routing/DijkstraZone.cpp @@ -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()); + auto elm = route_cache_.try_emplace(src_id, std::vector()); std::vector& pred_arr = elm.first->second; if (elm.second) { /* new element was inserted (not cached mode, or cache miss) */ diff --git a/src/kernel/routing/FatTreeZone.cpp b/src/kernel/routing/FatTreeZone.cpp index 3c24baa761..7f7337d379 100644 --- a/src/kernel/routing/FatTreeZone.cpp +++ b/src/kernel/routing/FatTreeZone.cpp @@ -341,7 +341,7 @@ void FatTreeZone::add_processing_node(int id, resource::StandardLinkImpl* limite position++; newNode->parents.resize(static_cast(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); } diff --git a/src/kernel/routing/NetZoneImpl.cpp b/src/kernel/routing/NetZoneImpl.cpp index cc3773389c..703a97d31c 100644 --- a/src/kernel/routing/NetZoneImpl.cpp +++ b/src/kernel/routing/NetZoneImpl.cpp @@ -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 diff --git a/src/kernel/routing/StarZone.cpp b/src/kernel/routing/StarZone.cpp index 489ecd9c88..6d4b0f7da6 100644 --- a/src/kernel/routing/StarZone.cpp +++ b/src/kernel/routing/StarZone.cpp @@ -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; diff --git a/src/plugins/file_system/s4u_FileSystem.cpp b/src/plugins/file_system/s4u_FileSystem.cpp index be2ab2b331..98acd5957a 100644 --- a/src/plugins/file_system/s4u_FileSystem.cpp +++ b/src/plugins/file_system/s4u_FileSystem.cpp @@ -386,6 +386,11 @@ std::map>* 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; }); diff --git a/src/s4u/s4u_Engine.cpp b/src/s4u/s4u_Engine.cpp index 858ee1f367..76166e0f19 100644 --- a/src/s4u/s4u_Engine.cpp +++ b/src/s4u/s4u_Engine.cpp @@ -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()); diff --git a/src/smpi/internals/smpi_bench.cpp b/src/smpi/internals/smpi_bench.cpp index d8fd0ecfab..6da2855adf 100644 --- a/src/smpi/internals/smpi_bench.cpp +++ b/src/smpi/internals/smpi_bench.cpp @@ -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, diff --git a/src/smpi/internals/smpi_config.cpp b/src/smpi/internals/smpi_config.cpp index 55b6c77825..e459692755 100644 --- a/src/smpi/internals/smpi_config.cpp +++ b/src/smpi/internals/smpi_config.cpp @@ -104,7 +104,7 @@ simgrid::config::Flag _smpi_cfg_comp_adjustment_file{ auto end = std::next(tok.begin()); std::string location = *it; boost::trim(location); - location2speedup.insert(std::pair(location, std::stod(*end))); + location2speedup.try_emplace(location, std::stod(*end)); } } }}; diff --git a/src/smpi/internals/smpi_deployment.cpp b/src/smpi/internals/smpi_deployment.cpp index 929a7bee35..06f9d85c97 100644 --- a/src/smpi/internals/smpi_deployment.cpp +++ b/src/smpi/internals/smpi_deployment.cpp @@ -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(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) diff --git a/src/smpi/internals/smpi_global.cpp b/src/smpi/internals/smpi_global.cpp index 24ae088863..405d985238 100644 --- a/src/smpi/internals/smpi_global.cpp +++ b/src/smpi/internals/smpi_global.cpp @@ -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 } diff --git a/src/smpi/internals/smpi_shared.cpp b/src/smpi/internals/smpi_shared.cpp index 4f9658bef8..4edd30fd3b 100644 --- a/src/smpi/internals/smpi_shared.cpp +++ b/src/smpi/internals/smpi_shared.cpp @@ -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. diff --git a/src/smpi/internals/smpi_utils.cpp b/src/smpi/internals/smpi_utils.cpp index 77436cc238..591b33d6f9 100644 --- a/src/smpi/internals/smpi_utils.cpp +++ b/src/smpi/internals/smpi_utils.cpp @@ -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{call}); + collective_calls.try_emplace(comm->id(), std::vector{call}); } else { // are we the first ? add the call if (vec->second.size() == count) { diff --git a/src/smpi/mpi/smpi_datatype.cpp b/src/smpi/mpi/smpi_datatype.cpp index d5f3d212dd..d9bf6de67f 100644 --- a/src/smpi/mpi/smpi_datatype.cpp +++ b/src/smpi/mpi/smpi_datatype.cpp @@ -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_); diff --git a/src/surf/network_ib.cpp b/src/surf/network_ib.cpp index 1555059fdf..c6362f6a44 100644 --- a/src/surf/network_ib.cpp +++ b/src/surf/network_ib.cpp @@ -53,7 +53,7 @@ void NetworkIBModel::IB_create_host_callback(s4u::Host const& host) { static int id = 0; auto* ibModel = static_cast(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++; } diff --git a/src/surf/network_ns3.cpp b/src/surf/network_ns3.cpp index 3fbd4ca95e..a02a956300 100644 --- a/src/surf/network_ns3.cpp +++ b/src/surf/network_ns3.cpp @@ -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(totalBytes), this)}); - sink_from_sock.insert({transform_socket_ptr(sock), apps}); + flow_from_sock.try_emplace(transform_socket_ptr(sock), new SgFlow(static_cast(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); diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index a1498256a6..62b345e109 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -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); } diff --git a/src/surf/xml/surfxml_sax_cb.cpp b/src/surf/xml/surfxml_sax_cb.cpp index f3773aa753..7b0e782a68 100644 --- a/src/surf/xml/surfxml_sax_cb.cpp +++ b/src/surf/xml/surfxml_sax_cb.cpp @@ -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())); } diff --git a/src/xbt/config.cpp b/src/xbt/config.cpp index d61be8721b..c2a5fdbb8a 100644 --- a/src/xbt/config.cpp +++ b/src/xbt/config.cpp @@ -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 */ diff --git a/src/xbt/xbt_replay.cpp b/src/xbt/xbt_replay.cpp index c64910ab88..41c6d57e74 100644 --- a/src/xbt/xbt_replay.cpp +++ b/src/xbt/xbt_replay.cpp @@ -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(); - action_queues.insert({evtname, otherqueue}); + action_queues.try_emplace(evtname, otherqueue); } otherqueue->push(action); } -- 2.20.1