From c5cc61b110480a24f06c18bbeaaea8dd4b92683f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Fri, 1 Dec 2023 14:59:20 +0100 Subject: [PATCH] PlannerObjectiveStatement: turn model_tree into a std::unique_ptr Avoids an unnecessary copy. --- src/ComputingTasks.cc | 24 ++++++++++++------------ src/ComputingTasks.hh | 7 ++++--- src/ParsingDriver.cc | 2 +- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/ComputingTasks.cc b/src/ComputingTasks.cc index aabcb5fb..1c958ed3 100644 --- a/src/ComputingTasks.cc +++ b/src/ComputingTasks.cc @@ -2400,8 +2400,8 @@ ModelComparisonStatement::writeJsonOutput(ostream& output) const output << "}"; } -PlannerObjectiveStatement::PlannerObjectiveStatement(const PlannerObjective& model_tree_arg) : - model_tree {model_tree_arg} +PlannerObjectiveStatement::PlannerObjectiveStatement(unique_ptr model_tree_arg) : + model_tree {move(model_tree_arg)} { } @@ -2409,8 +2409,8 @@ void PlannerObjectiveStatement::checkPass(ModFileStructure& mod_file_struct, [[maybe_unused]] WarningConsolidation& warnings) { - assert(model_tree.equation_number() == 1); - if (model_tree.exoPresentInEqs()) + assert(model_tree->equation_number() == 1); + if (model_tree->exoPresentInEqs()) { cerr << "ERROR: You cannot include exogenous variables (or variables of undeclared type) in " "the planner objective. Please " @@ -2424,13 +2424,13 @@ PlannerObjectiveStatement::checkPass(ModFileStructure& mod_file_struct, const PlannerObjective& PlannerObjectiveStatement::getPlannerObjective() const { - return model_tree; + return *model_tree; } void PlannerObjectiveStatement::computingPass(const ModFileStructure& mod_file_struct) { - model_tree.computingPass(max(3, mod_file_struct.order_option), 0, {}, false, false, false); + model_tree->computingPass(max(3, mod_file_struct.order_option), 0, {}, false, false, false); computing_pass_called = true; } @@ -2439,14 +2439,14 @@ PlannerObjectiveStatement::writeOutput(ostream& output, const string& basename, [[maybe_unused]] bool minimal_workspace) const { output << "M_.NNZDerivatives_objective = ["; - for (int i = 1; i < static_cast(model_tree.getNNZDerivatives().size()); i++) - output << (i > model_tree.getComputedDerivsOrder() ? -1 : model_tree.getNNZDerivatives()[i]) + for (int i = 1; i < static_cast(model_tree->getNNZDerivatives().size()); i++) + output << (i > model_tree->getComputedDerivsOrder() ? -1 : model_tree->getNNZDerivatives()[i]) << ";"; output << "];" << endl << "M_.objective_tmp_nbr = ["; - for (const auto& temporary_terms_derivative : model_tree.getTemporaryTermsDerivatives()) + for (const auto& temporary_terms_derivative : model_tree->getTemporaryTermsDerivatives()) output << temporary_terms_derivative.size() << "; "; output << "];" << endl; - model_tree.writeStaticFile(basename + ".objective", false, "", {}, false); + model_tree->writeStaticFile(basename + ".objective", false, "", {}, false); } void @@ -2455,9 +2455,9 @@ PlannerObjectiveStatement::writeJsonOutput(ostream& output) const output << R"({"statementName": "planner_objective")" << ", "; if (computing_pass_called) - model_tree.writeJsonComputingPassOutput(output, false); + model_tree->writeJsonComputingPassOutput(output, false); else - model_tree.writeJsonOutput(output); + model_tree->writeJsonOutput(output); output << "}"; } diff --git a/src/ComputingTasks.hh b/src/ComputingTasks.hh index 15378110..73b9cf7e 100644 --- a/src/ComputingTasks.hh +++ b/src/ComputingTasks.hh @@ -20,6 +20,7 @@ #ifndef _COMPUTINGTASKS_HH #define _COMPUTINGTASKS_HH +#include #include #include @@ -618,11 +619,11 @@ public: class PlannerObjectiveStatement : public Statement { private: - PlannerObjective model_tree; + unique_ptr model_tree; bool computing_pass_called {false}; public: - explicit PlannerObjectiveStatement(const PlannerObjective& model_tree_arg); + explicit PlannerObjectiveStatement(unique_ptr model_tree_arg); /*! \todo check there are only endogenous variables at the current period in the objective (no exogenous, no lead/lag) */ void checkPass(ModFileStructure& mod_file_struct, WarningConsolidation& warnings) override; @@ -631,7 +632,7 @@ public: void writeOutput(ostream& output, const string& basename, bool minimal_workspace) const override; void writeJsonOutput(ostream& output) const override; //! Return a reference the Planner Objective model tree - const PlannerObjective& getPlannerObjective() const; + [[nodiscard]] const PlannerObjective& getPlannerObjective() const; }; class BVARDensityStatement : public Statement diff --git a/src/ParsingDriver.cc b/src/ParsingDriver.cc index bf40c2ea..ae2c74cf 100644 --- a/src/ParsingDriver.cc +++ b/src/ParsingDriver.cc @@ -2235,7 +2235,7 @@ ParsingDriver::end_planner_objective(expr_t expr) expr_t eq = model_tree->AddEqual(expr, model_tree->Zero); model_tree->addEquation(eq, location.begin.line); - mod_file->addStatement(make_unique(*planner_objective)); + mod_file->addStatement(make_unique(move(planner_objective))); // Handle undeclared variables (see #81) bool exit_after_write = false;