From: Arnaud Giersch Date: Fri, 4 Aug 2017 07:46:24 +0000 (+0200) Subject: Use (const) references with range-based for loops. X-Git-Tag: v3_17~152^2~13 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/74c1bf2b26c5a3aa0d8c29674dc12993e7c0de15 Use (const) references with range-based for loops. This avoids unnecessary copies and saves a few milliseconds. --- diff --git a/examples/s4u/app-bittorrent/s4u_bittorrent.cpp b/examples/s4u/app-bittorrent/s4u_bittorrent.cpp index cbc093b293..e45b1e7f8d 100644 --- a/examples/s4u/app-bittorrent/s4u_bittorrent.cpp +++ b/examples/s4u/app-bittorrent/s4u_bittorrent.cpp @@ -23,7 +23,7 @@ int main(int argc, char* argv[]) std::vector list; simgrid::s4u::Engine::getInstance()->getHostList(&list); - for (auto host : list) + for (auto const& host : list) host->extension_set(new HostBittorrent(host)); e->registerFunction("tracker"); diff --git a/examples/s4u/app-bittorrent/s4u_peer.cpp b/examples/s4u/app-bittorrent/s4u_peer.cpp index a9c38f868f..431f433b30 100644 --- a/examples/s4u/app-bittorrent/s4u_peer.cpp +++ b/examples/s4u/app-bittorrent/s4u_peer.cpp @@ -57,7 +57,7 @@ Peer::Peer(std::vector args) Peer::~Peer() { - for (auto peer : connected_peers) + for (auto const& peer : connected_peers) delete peer.second; delete[] pieces_count; } @@ -102,7 +102,7 @@ bool Peer::getPeersFromTracker() try { TrackerAnswer* answer = static_cast(mailbox_->get(GET_PEERS_TIMEOUT)); // Add the peers the tracker gave us to our peer list. - for (auto peer_id : *answer->getPeers()) + for (auto const& peer_id : *answer->getPeers()) if (id != peer_id) connected_peers[peer_id] = new Connection(peer_id); delete answer; @@ -117,7 +117,7 @@ bool Peer::getPeersFromTracker() void Peer::sendHandshakeToAllPeers() { - for (auto kv : connected_peers) { + for (auto const& kv : connected_peers) { Connection* remote_peer = kv.second; Message* handshake = new Message(MESSAGE_HANDSHAKE, id, mailbox_); remote_peer->mailbox_->put_init(handshake, MESSAGE_HANDSHAKE_SIZE)->detach(); @@ -151,7 +151,7 @@ void Peer::sendPiece(simgrid::s4u::MailboxPtr mailbox, unsigned int piece, int b void Peer::sendHaveToAllPeers(unsigned int piece) { XBT_DEBUG("Sending HAVE message to all my peers"); - for (auto kv : connected_peers) { + for (auto const& kv : connected_peers) { Connection* remote_peer = kv.second; remote_peer->mailbox_->put_init(new Message(MESSAGE_HAVE, id, mailbox_, piece), MESSAGE_HAVE_SIZE)->detach(); } @@ -220,7 +220,7 @@ unsigned int Peer::countPieces(unsigned int bitfield) int Peer::nbInterestedPeers() { int nb = 0; - for (auto kv : connected_peers) + for (auto const& kv : connected_peers) if (kv.second->interested) nb++; return nb; @@ -558,7 +558,7 @@ void Peer::updateChokedPeers() if (hasFinished()) { Connection* remote_peer; double unchoke_time = simgrid::s4u::Engine::getClock() + 1; - for (auto kv : connected_peers) { + for (auto const& kv : connected_peers) { remote_peer = kv.second; if (remote_peer->last_unchoke < unchoke_time && remote_peer->interested && remote_peer->choked_upload) { unchoke_time = remote_peer->last_unchoke; @@ -585,7 +585,7 @@ void Peer::updateChokedPeers() } else { // Use the "fastest download" policy. double fastest_speed = 0.0; - for (auto kv : connected_peers) { + for (auto const& kv : connected_peers) { Connection* remote_peer = kv.second; if (remote_peer->peer_speed > fastest_speed && remote_peer->choked_upload && remote_peer->interested) { chosen_peer = remote_peer; @@ -622,7 +622,7 @@ void Peer::updateChokedPeers() /** @brief Update "interested" state of peers: send "not interested" to peers that don't have any more pieces we want.*/ void Peer::updateInterestedAfterReceive() { - for (auto kv : connected_peers) { + for (auto const& kv : connected_peers) { Connection* remote_peer = kv.second; if (remote_peer->am_interested) { bool interested = false; diff --git a/examples/s4u/app-token-ring/s4u_app-token-ring.cpp b/examples/s4u/app-token-ring/s4u_app-token-ring.cpp index ead05bfbef..a5c3d7fa4a 100644 --- a/examples/s4u/app-token-ring/s4u_app-token-ring.cpp +++ b/examples/s4u/app-token-ring/s4u_app-token-ring.cpp @@ -63,7 +63,7 @@ int main(int argc, char** argv) int id = 0; std::vector list; e->getHostList(&list); - for (auto host : list) { + for (auto const& host : list) { /* - Give a unique rank to each host and create a @ref relay_runner process on each */ simgrid::s4u::Actor::createActor((std::to_string(id)).c_str(), host, RelayRunner()); id++; diff --git a/examples/s4u/dht-chord/s4u_dht-chord.cpp b/examples/s4u/dht-chord/s4u_dht-chord.cpp index 1a9db78bf2..6bd3cc8368 100644 --- a/examples/s4u/dht-chord/s4u_dht-chord.cpp +++ b/examples/s4u/dht-chord/s4u_dht-chord.cpp @@ -30,7 +30,7 @@ static void chord_init() std::vector list; simgrid::s4u::Engine::getInstance()->getHostList(&list); - for (auto host : list) + for (auto const& host : list) host->extension_set(new HostChord(host)); } diff --git a/examples/simdag/test/sd_test.cpp b/examples/simdag/test/sd_test.cpp index 3b3de64480..f5fad58579 100644 --- a/examples/simdag/test/sd_test.cpp +++ b/examples/simdag/test/sd_test.cpp @@ -42,7 +42,7 @@ int main(int argc, char **argv) double latency = 0; h1->routeTo(h2, &route, &latency); - for (auto link : route) + for (auto const& link : route) XBT_INFO(" Link %s: latency = %f, bandwidth = %f", sg_link_name(link), sg_link_latency(link), sg_link_bandwidth(link)); @@ -128,7 +128,7 @@ int main(int argc, char **argv) SD_task_schedule(taskD, 2, host_list, computation_amount, communication_amount, -1); std::set *changed_tasks = simgrid::sd::simulate(-1.0); - for (auto task: *changed_tasks){ + for (auto const& task : *changed_tasks) { XBT_INFO("Task '%s' start time: %f, finish time: %f", SD_task_get_name(task), SD_task_get_start_time(task), SD_task_get_finish_time(task)); } diff --git a/include/simgrid/s4u/Comm.hpp b/include/simgrid/s4u/Comm.hpp index dd4dcc811c..c972fe0de5 100644 --- a/include/simgrid/s4u/Comm.hpp +++ b/include/simgrid/s4u/Comm.hpp @@ -41,7 +41,7 @@ public: xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), [](void*ptr){ intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr); }); - for (auto comm : *comms_in) { + for (auto const& comm : *comms_in) { if (comm->state_ == inited) comm->start(); xbt_assert(comm->state_ == started); diff --git a/include/xbt/signal.hpp b/include/xbt/signal.hpp index 508822cf2b..d5ad826fa2 100644 --- a/include/xbt/signal.hpp +++ b/include/xbt/signal.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2014-2015. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2014-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. */ @@ -34,7 +34,7 @@ namespace xbt { } R operator()(P... args) const { - for (auto& handler : handlers_) + for (auto const& handler : handlers_) handler(args...); } void disconnect_all_slots() diff --git a/src/bindings/java/JavaContext.cpp b/src/bindings/java/JavaContext.cpp index c796b18c0d..7865d16d6d 100644 --- a/src/bindings/java/JavaContext.cpp +++ b/src/bindings/java/JavaContext.cpp @@ -47,7 +47,7 @@ JavaContext* JavaContextFactory::create_context( void JavaContextFactory::run_all() { - for (smx_actor_t process : simgrid::simix::process_get_runnable()) { + for (smx_actor_t const& process : simgrid::simix::process_get_runnable()) { static_cast(process->context)->resume(); } } diff --git a/src/bindings/java/jmsg.cpp b/src/bindings/java/jmsg.cpp index 11342f25b4..09d41702e3 100644 --- a/src/bindings/java/jmsg.cpp +++ b/src/bindings/java/jmsg.cpp @@ -154,7 +154,7 @@ JNIEXPORT void JNICALL JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass xbt_dynar_free(&hosts); /* Cleanup java storages */ - for (auto elm : java_storage_map) + for (auto const& elm : java_storage_map) jstorage_unref(env, elm.second); } diff --git a/src/bindings/java/jmsg_as.cpp b/src/bindings/java/jmsg_as.cpp index cd344be661..41ed0b39b8 100644 --- a/src/bindings/java/jmsg_as.cpp +++ b/src/bindings/java/jmsg_as.cpp @@ -78,7 +78,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getSons(JNIEnv * env, job return nullptr; } - for (auto tmp_as : *self_as->getChildren()) { + for (auto const& tmp_as : *self_as->getChildren()) { jobject tmp_jas = jnetzone_new_instance(env); if (not tmp_jas) { jxbt_throw_jni(env, "java As instantiation failed"); @@ -140,7 +140,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getHosts(JNIEnv * env, jo } int index = 0; - for (auto host : table) { + for (auto const& host : table) { jhost = static_cast(host->extension(JAVA_HOST_LEVEL)); if (not jhost) { jname = env->NewStringUTF(host->getCname()); diff --git a/src/bindings/java/jmsg_host.cpp b/src/bindings/java/jmsg_host.cpp index 28804e76e9..a255e2e147 100644 --- a/src/bindings/java/jmsg_host.cpp +++ b/src/bindings/java/jmsg_host.cpp @@ -249,7 +249,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getMountedStorage(JNIEn return nullptr; } - for (auto elm : mounted_storages) { + for (auto const& elm : mounted_storages) { jname = env->NewStringUTF(elm.second->getName()); jstorage = Java_org_simgrid_msg_Storage_getByName(env,cls,jname); env->SetObjectArrayElement(jtable, index, jstorage); diff --git a/src/bindings/lua/lua_platf.cpp b/src/bindings/lua/lua_platf.cpp index f0ca4380da..cddfe8aaa4 100644 --- a/src/bindings/lua/lua_platf.cpp +++ b/src/bindings/lua/lua_platf.cpp @@ -337,7 +337,7 @@ int console_add_route(lua_State *L) { route.link_list->push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1))); } else { // Several names separated by , \t\r\n - for (auto name : names) { + for (auto const& name : names) { if (name.length() > 0) { simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name); route.link_list->push_back(link); @@ -418,7 +418,7 @@ int console_add_ASroute(lua_State *L) { ASroute.link_list->push_back(simgrid::surf::LinkImpl::byName(lua_tostring(L, -1))); } else { // Several names separated by , \t\r\n - for (auto name : names) { + for (auto const& name : names) { if (name.length() > 0) { simgrid::surf::LinkImpl* link = simgrid::surf::LinkImpl::byName(name); ASroute.link_list->push_back(link); diff --git a/src/instr/instr_config.cpp b/src/instr/instr_config.cpp index daa18c4329..a3318319cf 100644 --- a/src/instr/instr_config.cpp +++ b/src/instr/instr_config.cpp @@ -482,7 +482,7 @@ static void output_categories(const char* name, FILE* file) { unsigned int i = created_categories.size(); fprintf (file, " values = ("); - for (auto cat : created_categories) { + for (auto const& cat : created_categories) { --i; fprintf(file, "\"%s%s\"", name, cat.c_str()); if (i > 0) { diff --git a/src/instr/instr_interface.cpp b/src/instr/instr_interface.cpp index 24b187f70a..a65032c113 100644 --- a/src/instr/instr_interface.cpp +++ b/src/instr/instr_interface.cpp @@ -35,7 +35,7 @@ static xbt_dynar_t instr_set_to_dynar(std::set* filter) return nullptr; xbt_dynar_t ret = xbt_dynar_new (sizeof(char*), &xbt_free_ref); - for (auto name : *filter) + for (auto const& name : *filter) xbt_dynar_push_as(ret, char*, xbt_strdup(name.c_str())); return ret; @@ -314,7 +314,7 @@ static void instr_user_srcdst_variable(double time, const char *src, const char std::vector route; simgrid::kernel::routing::NetZoneImpl::getGlobalRoute(src_elm, dst_elm, &route, nullptr); - for (auto link : route) + for (auto const& link : route) instr_user_variable(time, link->cname(), variable, father_type, value, what, nullptr, &user_link_variables); } diff --git a/src/instr/instr_paje_trace.cpp b/src/instr/instr_paje_trace.cpp index cd2a3ceb7c..6e9df5552f 100644 --- a/src/instr/instr_paje_trace.cpp +++ b/src/instr/instr_paje_trace.cpp @@ -68,14 +68,14 @@ void TRACE_paje_dump_buffer (int force) return; XBT_DEBUG("%s: dump until %f. starts", __FUNCTION__, TRACE_last_timestamp_to_dump); if (force){ - for (auto event : buffer){ + for (auto const& event : buffer) { event->print(); delete event; } buffer.clear(); }else{ std::vector::iterator i = buffer.begin(); - for (auto event :buffer){ + for (auto const& event : buffer) { double head_timestamp = event->timestamp; if (head_timestamp > TRACE_last_timestamp_to_dump) break; @@ -92,7 +92,7 @@ void buffer_debug(std::vector *buf); void buffer_debug(std::vector *buf) { return; XBT_DEBUG(">>>>>> Dump the state of the buffer. %zu events", buf->size()); - for (auto event :*buf){ + for (auto const& event : *buf) { event->print(); XBT_DEBUG("%p %s", event, stream.str().c_str()); stream.str(""); diff --git a/src/instr/jedule/jedule.cpp b/src/instr/jedule/jedule.cpp index 4f7841fe43..46b1611702 100644 --- a/src/instr/jedule/jedule.cpp +++ b/src/instr/jedule/jedule.cpp @@ -14,7 +14,7 @@ namespace jedule { Jedule::~Jedule() { delete this->root_container; - for (auto evt: this->event_set) + for (auto const& evt : this->event_set) delete evt; this->event_set.clear(); } @@ -32,7 +32,7 @@ void Jedule::writeOutput(FILE *file) { if (not this->meta_info.empty()) { fprintf(file, " \n"); - for (auto elm: this->meta_info) + for (auto const& elm : this->meta_info) fprintf(file, " \n",elm.first,elm.second); fprintf(file, " \n"); } @@ -42,7 +42,7 @@ void Jedule::writeOutput(FILE *file) { fprintf(file, " \n"); fprintf(file, " \n"); - for (auto event :this->event_set) + for (auto const& event : this->event_set) event->print(file); fprintf(file, " \n"); diff --git a/src/instr/jedule/jedule_events.cpp b/src/instr/jedule/jedule_events.cpp index afe12f3cac..01ca092499 100644 --- a/src/instr/jedule/jedule_events.cpp +++ b/src/instr/jedule/jedule_events.cpp @@ -21,7 +21,7 @@ Event::Event(std::string name, double start_time, double end_time, std::string t Event::~Event() { if (not this->resource_subsets->empty()) { - for (auto subset: *this->resource_subsets) + for (auto const& subset : *this->resource_subsets) delete subset; delete this->resource_subsets; } @@ -53,7 +53,7 @@ void Event::print(FILE *jed_file) xbt_assert(not this->resource_subsets->empty()); fprintf(jed_file, " \n"); - for (auto subset: *this->resource_subsets) { + for (auto const& subset : *this->resource_subsets) { fprintf(jed_file, "