Pre-allocate std::vector instances filled from loops

Automatically detected by clang-tidy using
performance-inefficient-vector-operation check.
master
Sébastien Villemot 2023-12-13 16:19:31 +01:00
parent 22675728aa
commit b31de3d9a6
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
4 changed files with 10 additions and 0 deletions

View File

@ -6917,6 +6917,7 @@ AbstractExternalFunctionNode::computeDerivative(int deriv_id)
{
assert(datatree.external_functions_table.getNargs(symb_id) > 0);
vector<expr_t> dargs;
dargs.reserve(arguments.size());
for (auto argument : arguments)
dargs.push_back(argument->getDerivative(deriv_id));
return composeDerivatives(dargs);
@ -6930,6 +6931,7 @@ AbstractExternalFunctionNode::computeChainRuleDerivative(
{
assert(datatree.external_functions_table.getNargs(symb_id) > 0);
vector<expr_t> dargs;
dargs.reserve(arguments.size());
for (auto argument : arguments)
dargs.push_back(argument->getChainRuleDerivative(deriv_id, recursive_variables,
non_null_chain_rule_derivatives, cache));
@ -7405,6 +7407,7 @@ expr_t
AbstractExternalFunctionNode::toStatic(DataTree& static_datatree) const
{
vector<expr_t> static_arguments;
static_arguments.reserve(arguments.size());
for (auto argument : arguments)
static_arguments.push_back(argument->toStatic(static_datatree));
return buildSimilarExternalFunctionNode(static_arguments, static_datatree);
@ -7414,6 +7417,7 @@ expr_t
AbstractExternalFunctionNode::clone(DataTree& alt_datatree) const
{
vector<expr_t> dynamic_arguments;
dynamic_arguments.reserve(arguments.size());
for (auto argument : arguments)
dynamic_arguments.push_back(argument->clone(alt_datatree));
return buildSimilarExternalFunctionNode(dynamic_arguments, alt_datatree);
@ -7423,6 +7427,7 @@ expr_t
ExternalFunctionNode::composeDerivatives(const vector<expr_t>& dargs)
{
vector<expr_t> dNodes;
dNodes.reserve(dargs.size());
for (int i = 0; i < static_cast<int>(dargs.size()); i++)
dNodes.push_back(datatree.AddTimes(
dargs.at(i), datatree.AddFirstDerivExternalFunction(symb_id, arguments, i + 1)));
@ -7715,6 +7720,7 @@ expr_t
FirstDerivExternalFunctionNode::composeDerivatives(const vector<expr_t>& dargs)
{
vector<expr_t> dNodes;
dNodes.reserve(dargs.size());
for (int i = 0; i < static_cast<int>(dargs.size()); i++)
dNodes.push_back(datatree.AddTimes(dargs.at(i), datatree.AddSecondDerivExternalFunction(
symb_id, arguments, inputIndex, i + 1)));

View File

@ -529,6 +529,7 @@ Epilogue::writeOutput(ostream& output) const
expr->collectVariables(SymbolType::endogenous, endogs);
vector<string> symbol_list;
symbol_list.reserve(endogs.size());
for (auto symb_id : endogs)
symbol_list.push_back(symbol_table.getName(symb_id));
SymbolList {move(symbol_list)}.writeOutput("M_.epilogue_var_list_", output);

View File

@ -854,6 +854,7 @@ StaticModel::computeRamseyMultipliersDerivatives(int ramsey_orig_endo_nbr, bool
// Compute derivation IDs of Lagrange multipliers
set<int> mult_symb_ids {symbol_table.getLagrangeMultipliers()};
vector<int> mult_deriv_ids;
mult_deriv_ids.reserve(mult_symb_ids.size());
for (int symb_id : mult_symb_ids)
mult_deriv_ids.push_back(getDerivID(symb_id, 0));

View File

@ -640,6 +640,7 @@ BaseTypePtr
Array::eval(Environment& env) const
{
vector<ExpressionPtr> retval;
retval.reserve(arr.size());
for (const auto& it : arr)
retval.emplace_back(it->eval(env));
return make_shared<Array>(retval);
@ -649,6 +650,7 @@ BaseTypePtr
Tuple::eval(Environment& env) const
{
vector<ExpressionPtr> retval;
retval.reserve(tup.size());
for (const auto& it : tup)
retval.emplace_back(it->eval(env));
return make_shared<Tuple>(retval);