From 22b73e951cc052e33689b42fd9a747334bc22c8a Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Thu, 9 Mar 2017 12:04:02 +0100 Subject: [PATCH] prefer the stack to the heap (+ don't refill the host list if already full) --- include/simgrid/s4u/NetZone.hpp | 2 +- src/s4u/s4u_netzone.cpp | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/simgrid/s4u/NetZone.hpp b/include/simgrid/s4u/NetZone.hpp index 38cfab22c4..c05a251082 100644 --- a/include/simgrid/s4u/NetZone.hpp +++ b/include/simgrid/s4u/NetZone.hpp @@ -40,7 +40,7 @@ protected: explicit NetZone(NetZone * father, const char* name); virtual ~NetZone(); - std::vector* hosts_ = new std::vector(); + std::vector hosts_; public: /** @brief Seal your netzone once you're done adding content, and before routing stuff through it */ diff --git a/src/s4u/s4u_netzone.cpp b/src/s4u/s4u_netzone.cpp index 780baadf35..0a343db706 100644 --- a/src/s4u/s4u_netzone.cpp +++ b/src/s4u/s4u_netzone.cpp @@ -38,7 +38,6 @@ NetZone::~NetZone() delete static_cast(elem); } - delete hosts_; xbt_dict_free(&children_); xbt_free(name_); } @@ -76,12 +75,13 @@ NetZone* NetZone::father() std::vector* NetZone::hosts() { - for (auto card : vertices_) { - s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->name()); - if (host != nullptr) - hosts_->push_back(host); - } - return hosts_; + if (hosts_.empty()) // Lazy initialization + for (auto card : vertices_) { + s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->name()); + if (host != nullptr) + hosts_.push_back(host); + } + return &hosts_; } int NetZone::addComponent(kernel::routing::NetPoint* elm) -- 2.20.1