Skip to content

Commit

Permalink
#1469: util: use const ref instead of std::unique_ptr when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
cz4rs authored and lifflander committed Jun 28, 2021
1 parent 89926e8 commit 1852358
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 31 deletions.
19 changes: 6 additions & 13 deletions src/vt/vrt/collection/balance/stats_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,16 @@ std::unique_ptr<nlohmann::json> StatsData::toJson(PhaseType phase) const {
return std::make_unique<json>(std::move(j));
}

/*static*/ std::unique_ptr<StatsData> StatsData::fromJson(
std::unique_ptr<nlohmann::json> jin
) {
StatsData::StatsData(nlohmann::json const& j) {
auto this_node = theContext()->getNode();

auto sd = std::make_unique<StatsData>();

auto const& j = *jin;
auto phases = j["phases"];
if (phases.is_array()) {
for (auto const& phase : phases) {
auto id = phase["id"];
auto tasks = phase["tasks"];

sd->node_data_[id];
this->node_data_[id];

if (tasks.is_array()) {
for (auto const& task : tasks) {
Expand All @@ -193,7 +188,7 @@ std::unique_ptr<nlohmann::json> StatsData::toJson(PhaseType phase) const {
}

auto elm = ElementIDStruct{object, home, node};
sd->node_data_[id][elm] = time;
this->node_data_[id][elm] = time;

if (
task["entity"].find("collection_id") != task["entity"].end() and
Expand All @@ -204,7 +199,7 @@ std::unique_ptr<nlohmann::json> StatsData::toJson(PhaseType phase) const {
if (cid.is_number() && idx.is_array()) {
std::vector<uint64_t> arr = idx;
auto proxy = static_cast<VirtualProxyType>(cid);
sd->node_idx_[elm] = std::make_tuple(proxy, arr);
this->node_idx_[elm] = std::make_tuple(proxy, arr);
}
}

Expand All @@ -218,10 +213,10 @@ std::unique_ptr<nlohmann::json> StatsData::toJson(PhaseType phase) const {
vtAssertExpr(sid.is_number());
vtAssertExpr(stime.is_number());

sd->node_subphase_data_[id][elm].resize(
this->node_subphase_data_[id][elm].resize(
static_cast<std::size_t>(sid) + 1
);
sd->node_subphase_data_[id][elm][sid] = stime;
this->node_subphase_data_[id][elm][sid] = stime;
}
}
}
Expand All @@ -233,8 +228,6 @@ std::unique_ptr<nlohmann::json> StatsData::toJson(PhaseType phase) const {

// @todo: implement communication de-serialization, no use for it right now, so
// it will be ignored

return sd;
}

void StatsData::clear() {
Expand Down
16 changes: 7 additions & 9 deletions src/vt/vrt/collection/balance/stats_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ namespace vt { namespace vrt { namespace collection { namespace balance {
struct StatsData {
StatsData() = default;

/**
* \brief Create \c StatsData from input JSON
*
* \param[in] j the json that contains the stats
*/
StatsData(nlohmann::json const& j);

template <typename SerializerT>
void serialize(SerializerT& s) {
s | node_data_;
Expand All @@ -82,15 +89,6 @@ struct StatsData {
*/
std::unique_ptr<nlohmann::json> toJson(PhaseType phase) const;

/**
* \brief Create \c StatsData from input JSON
*
* \param[in] j the json that contains the stats
*
* \return the new \c StatsData
*/
static std::unique_ptr<StatsData> fromJson(std::unique_ptr<nlohmann::json> j);

/**
* \brief Clear all statistics
*/
Expand Down
15 changes: 7 additions & 8 deletions src/vt/vrt/collection/balance/stats_restart_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ StatsRestartReader::getMigrationList() const {
}

std::deque<std::set<ElementIDType>> StatsRestartReader::readIntoElementHistory(
std::unique_ptr<StatsData> sd
StatsData const& sd
) {
std::deque<std::set<ElementIDType>> element_history;
for (PhaseType phase = 0; phase < sd->node_data_.size(); phase++) {
for (PhaseType phase = 0; phase < sd.node_data_.size(); phase++) {
std::set<ElementIDType> buffer;
for (auto const& obj : sd->node_data_[phase]) {
for (auto const& obj : sd.node_data_.at(phase)) {
buffer.insert(obj.first.id);
}
element_history.emplace_back(std::move(buffer));
Expand All @@ -125,10 +125,9 @@ void StatsRestartReader::readStatsFromStream(std::stringstream stream) {
DecompressionInputContainer::AnyStreamTag{}, std::move(stream)
);
json j = json::parse(c);
auto jptr = std::make_unique<json>(std::move(j));
auto sd = StatsData::fromJson(std::move(jptr));
auto sd = StatsData(j);

auto element_history = readIntoElementHistory(std::move(sd));
auto element_history = readIntoElementHistory(sd);
constructMoveList(std::move(element_history));
}

Expand Down Expand Up @@ -166,9 +165,9 @@ StatsRestartReader::inputStatsFile(std::string const& fileName) {

Reader r{fileName};
auto json = r.readFile();
auto sd = StatsData::fromJson(std::move(json));
auto sd = StatsData(*json);

return readIntoElementHistory(std::move(sd));
return readIntoElementHistory(sd);
}

void StatsRestartReader::createMigrationInfo(
Expand Down
2 changes: 1 addition & 1 deletion src/vt/vrt/collection/balance/stats_restart_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct StatsRestartReader : runtime::component::Component<StatsRestartReader> {
void readStats(std::string const& fileName);

std::deque<std::set<ElementIDType>> readIntoElementHistory(
std::unique_ptr<StatsData> sd
StatsData const& sd
);

std::vector<ElementIDType> const& getMoveList(PhaseType phase) const;
Expand Down

0 comments on commit 1852358

Please sign in to comment.