From 7c6402cc349968e365a1d6e6c9ab929f81c03796 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 23 Mar 2023 12:50:18 +0100
Subject: [PATCH] DataTree: optimize the filling of node maps

Use map::{try,}emplace() instead of operator[], which should in theory be
slightly faster.
---
 src/DataTree.cc | 20 ++++++++++----------
 src/DataTree.hh |  8 ++++----
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/src/DataTree.cc b/src/DataTree.cc
index 85804b19..129d1b85 100644
--- a/src/DataTree.cc
+++ b/src/DataTree.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2003-2022 Dynare Team
+ * Copyright © 2003-2023 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -132,7 +132,7 @@ DataTree::AddNonNegativeConstant(const string &value)
   auto sp = make_unique<NumConstNode>(*this, node_list.size(), id);
   auto p = sp.get();
   node_list.push_back(move(sp));
-  num_const_node_map[id] = p;
+  num_const_node_map.emplace(id, p);
   return p;
 }
 
@@ -152,7 +152,7 @@ DataTree::AddVariable(int symb_id, int lag)
   auto sp = make_unique<VariableNode>(*this, node_list.size(), symb_id, lag);
   auto p = sp.get();
   node_list.push_back(move(sp));
-  variable_node_map[{ symb_id, lag }] = p;
+  variable_node_map.try_emplace({ symb_id, lag }, p);
   return p;
 }
 
@@ -687,7 +687,7 @@ DataTree::AddVarExpectation(const string &model_name)
   auto sp = make_unique<VarExpectationNode>(*this, node_list.size(), model_name);
   auto p = sp.get();
   node_list.push_back(move(sp));
-  var_expectation_node_map[model_name] = p;
+  var_expectation_node_map.emplace(model_name, p);
   return p;
 }
 
@@ -701,7 +701,7 @@ DataTree::AddPacExpectation(const string &model_name)
   auto sp = make_unique<PacExpectationNode>(*this, node_list.size(), model_name);
   auto p = sp.get();
   node_list.push_back(move(sp));
-  pac_expectation_node_map[model_name] = p;
+  pac_expectation_node_map.emplace(model_name, p);
   return p;
 }
 
@@ -715,7 +715,7 @@ DataTree::AddPacTargetNonstationary(const string &model_name)
   auto sp = make_unique<PacTargetNonstationaryNode>(*this, node_list.size(), model_name);
   auto p = sp.get();
   node_list.push_back(move(sp));
-  pac_target_nonstationary_node_map[model_name] = p;
+  pac_target_nonstationary_node_map.emplace(model_name, p);
   return p;
 }
 
@@ -736,7 +736,7 @@ DataTree::AddLocalVariable(int symb_id, expr_t value) noexcept(false)
   if (local_variables_table.contains(symb_id))
     throw LocalVariableException(symbol_table.getName(symb_id));
 
-  local_variables_table[symb_id] = value;
+  local_variables_table.emplace(symb_id, value);
   local_variables_vector.push_back(symb_id);
 }
 
@@ -752,7 +752,7 @@ DataTree::AddExternalFunction(int symb_id, const vector<expr_t> &arguments)
   auto sp = make_unique<ExternalFunctionNode>(*this, node_list.size(), symb_id, arguments);
   auto p = sp.get();
   node_list.push_back(move(sp));
-  external_function_node_map[{ arguments, symb_id }] = p;
+  external_function_node_map.try_emplace({ arguments, symb_id }, p);
   return p;
 }
 
@@ -768,7 +768,7 @@ DataTree::AddFirstDerivExternalFunction(int top_level_symb_id, const vector<expr
   auto sp = make_unique<FirstDerivExternalFunctionNode>(*this, node_list.size(), top_level_symb_id, arguments, input_index);
   auto p = sp.get();
   node_list.push_back(move(sp));
-  first_deriv_external_function_node_map[{ arguments, input_index, top_level_symb_id }] = p;
+  first_deriv_external_function_node_map.try_emplace({ arguments, input_index, top_level_symb_id }, p);
   return p;
 }
 
@@ -785,7 +785,7 @@ DataTree::AddSecondDerivExternalFunction(int top_level_symb_id, const vector<exp
   auto sp = make_unique<SecondDerivExternalFunctionNode>(*this, node_list.size(), top_level_symb_id, arguments, input_index1, input_index2);
   auto p = sp.get();
   node_list.push_back(move(sp));
-  second_deriv_external_function_node_map[{ arguments, input_index1, input_index2, top_level_symb_id }] = p;
+  second_deriv_external_function_node_map.try_emplace({ arguments, input_index1, input_index2, top_level_symb_id }, p);
   return p;
 }
 
diff --git a/src/DataTree.hh b/src/DataTree.hh
index 8afab57c..bfc86938 100644
--- a/src/DataTree.hh
+++ b/src/DataTree.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2003-2022 Dynare Team
+ * Copyright © 2003-2023 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -425,7 +425,7 @@ DataTree::AddUnaryOp(UnaryOpcode op_code, expr_t arg, int arg_exp_info_set, int
   auto sp = make_unique<UnaryOpNode>(*this, node_list.size(), op_code, arg, arg_exp_info_set, param1_symb_id, param2_symb_id, adl_param_name, adl_lags);
   auto p = sp.get();
   node_list.push_back(move(sp));
-  unary_op_node_map[{ arg, op_code, arg_exp_info_set, param1_symb_id, param2_symb_id, adl_param_name, adl_lags }] = p;
+  unary_op_node_map.try_emplace({ arg, op_code, arg_exp_info_set, param1_symb_id, param2_symb_id, adl_param_name, adl_lags }, p);
   return p;
 }
 
@@ -451,7 +451,7 @@ DataTree::AddBinaryOp(expr_t arg1, BinaryOpcode op_code, expr_t arg2, int powerD
   auto sp = make_unique<BinaryOpNode>(*this, node_list.size(), arg1, op_code, arg2, powerDerivOrder);
   auto p = sp.get();
   node_list.push_back(move(sp));
-  binary_op_node_map[{ arg1, arg2, op_code, powerDerivOrder }] = p;
+  binary_op_node_map.try_emplace({ arg1, arg2, op_code, powerDerivOrder }, p);
   return p;
 }
 
@@ -478,7 +478,7 @@ DataTree::AddTrinaryOp(expr_t arg1, TrinaryOpcode op_code, expr_t arg2, expr_t a
   auto sp = make_unique<TrinaryOpNode>(*this, node_list.size(), arg1, op_code, arg2, arg3);
   auto p = sp.get();
   node_list.push_back(move(sp));
-  trinary_op_node_map[{ arg1, arg2, arg3, op_code }] = p;
+  trinary_op_node_map.try_emplace({ arg1, arg2, arg3, op_code }, p);
   return p;
 }
 
-- 
GitLab