Skip to content

Commit

Permalink
Use std::vector::reserve in Element::Clone
Browse files Browse the repository at this point in the history
There are several for-loops in Element::Clone that call
std::vector::push_back, and since the vector size is
known in advance, use std::vector::reserve to avoid
multiple vector resizes.

Signed-off-by: Steve Peters <scpeters@openrobotics.org>
  • Loading branch information
scpeters committed Sep 9, 2024
1 parent c2c4f91 commit fc2dc63
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ ElementPtr Element::Clone(sdf::Errors &_errors) const
clone->dataPtr->explicitlySetInFile = this->dataPtr->explicitlySetInFile;

Param_V::const_iterator aiter;
clone->dataPtr->attributes.reserve(this->dataPtr->attributes.size());
for (aiter = this->dataPtr->attributes.begin();
aiter != this->dataPtr->attributes.end(); ++aiter)
{
Expand All @@ -263,12 +264,15 @@ ElementPtr Element::Clone(sdf::Errors &_errors) const
}

ElementPtr_V::const_iterator eiter;
clone->dataPtr->elementDescriptions.reserve(
this->dataPtr->elementDescriptions.size());
for (eiter = this->dataPtr->elementDescriptions.begin();
eiter != this->dataPtr->elementDescriptions.end(); ++eiter)
{
clone->dataPtr->elementDescriptions.push_back((*eiter)->Clone(_errors));
}

clone->dataPtr->elements.reserve(this->dataPtr->elements.size());
for (eiter = this->dataPtr->elements.begin();
eiter != this->dataPtr->elements.end(); ++eiter)
{
Expand Down

0 comments on commit fc2dc63

Please sign in to comment.