From 75fccedfd17dbac12a9c20498d98d699301132b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Wed, 30 Apr 2025 11:16:55 +0200 Subject: [PATCH] Performance improvement for DynamicModel::removeEquations() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The main bottleneck was in EquationTags::erase(), which had quadratic complexity in the number of equations. This commit optimizes it to n·log(n) complexity. --- src/DynamicModel.cc | 6 +++--- src/EquationTags.cc | 15 +++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc index 3a4162f2..398c22e7 100644 --- a/src/DynamicModel.cc +++ b/src/DynamicModel.cc @@ -731,9 +731,9 @@ DynamicModel::removeEquationsHelper( } int n_excl = all_equations.size() - new_equations.size(); - all_equations = new_equations; - all_equations_lineno = new_equations_lineno; - all_complementarity_conditions = new_complementarity_conditions; + all_equations = move(new_equations); + all_equations_lineno = move(new_equations_lineno); + all_complementarity_conditions = move(new_complementarity_conditions); all_equation_tags.erase(eqs_to_delete_by_number, old_eqn_num_2_new); diff --git a/src/EquationTags.cc b/src/EquationTags.cc index db8e1c2e..a446610f 100644 --- a/src/EquationTags.cc +++ b/src/EquationTags.cc @@ -1,5 +1,5 @@ /* - * Copyright © 2020-2023 Dynare Team + * Copyright © 2020-2025 Dynare Team * * This file is part of Dynare. * @@ -74,13 +74,12 @@ EquationTags::erase(const set<int>& eqns, const map<int, int>& old_eqn_num_2_new eqn_tags.erase(eqn); for (const auto& [oldeqn, neweqn] : old_eqn_num_2_new) - for (auto& [eqn, tags] : eqn_tags) - if (eqn == oldeqn) - { - auto tmp = eqn_tags.extract(eqn); - tmp.key() = neweqn; - eqn_tags.insert(move(tmp)); - } + if (eqn_tags.contains(oldeqn)) + { + auto tmp = eqn_tags.extract(oldeqn); + tmp.key() = neweqn; + eqn_tags.insert(move(tmp)); + } } void -- GitLab