Skip to content

Commit

Permalink
PERF: emplace_back method results in potentially more efficient code
Browse files Browse the repository at this point in the history
The check flags insertions to an STL-style container done by calling the
push_back method with an explicitly-constructed temporary of the container
element type. In this case, the corresponding emplace_back method results in
less verbose and potentially more efficient code.
  • Loading branch information
hjmjohnson authored and dzenanz committed Dec 27, 2024
1 parent 8e039bb commit 0d1353b
Show file tree
Hide file tree
Showing 28 changed files with 62 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Examples/RegistrationITKv4/DeformableRegistration6.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ main(int argc, char * argv[])
bsplineAdaptor->SetRequiredTransformDomainPhysicalDimensions(
fixedPhysicalDimensions);

adaptors.push_back(bsplineAdaptor);
adaptors.emplace_back(bsplineAdaptor);
}

registration->SetTransformParametersAdaptorsPerLevel(adaptors);
Expand Down
2 changes: 1 addition & 1 deletion Examples/Statistics/BayesianPluginClassifier.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ main(int, char *[])
membershipFunction->SetMean(covarianceEstimators[i]->GetMean());
membershipFunction->SetCovariance(
covarianceEstimators[i]->GetCovarianceMatrix());
membershipFunctionVector.push_back(membershipFunction);
membershipFunctionVector.emplace_back(membershipFunction);
}
membershipFunctionVectorObject->Set(membershipFunctionVector);
classifier->SetMembershipFunctions(membershipFunctionVectorObject);
Expand Down
2 changes: 1 addition & 1 deletion Examples/Statistics/KdTreeBasedKMeansClustering.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ main()
centroid[j] = estimatedMeans[index++];
}
membershipFunction->SetCentroid(centroid);
membershipFunctionVector.push_back(membershipFunction);
membershipFunctionVector.emplace_back(membershipFunction);
}
classifier->SetMembershipFunctions(membershipFunctionVectorObject);

Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/src/itkProcessObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ ProcessObject::GetOutputs()
// only include the primary if it's required or set
if (output.first != m_IndexedOutputs[0]->first || output.second.IsNotNull())
{
res.push_back(output.second.GetPointer());
res.emplace_back(output.second.GetPointer());
}
}
return res;
Expand Down Expand Up @@ -967,7 +967,7 @@ ProcessObject::GetInputs()
// only include the primary if it's required or set
if (input.first != m_IndexedInputs[0]->first || input.second.IsNotNull() || this->IsRequiredInputName(input.first))
{
res.push_back(input.second.GetPointer());
res.emplace_back(input.second.GetPointer());
}
}
return res;
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/src/itkThreadLogger.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ ThreadLogger::AddLogOutput(OutputType * output)
{
const std::lock_guard<std::mutex> lockGuard(m_Mutex);
this->m_OperationQ.push(ADD_LOG_OUTPUT);
this->m_OutputQ.push(output);
this->m_OutputQ.emplace(output);
}

void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ TEST(ImageRandomConstIteratorWithIndex, IsDeterministicWhenGlobalRandomSeedsAreR

while (!iterator.IsAtEnd())
{
samples.push_back({ iterator.Get(), iterator.GetIndex() });
samples.emplace_back(iterator.Get(), iterator.GetIndex());
++iterator;
}
return samples;
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/test/itkLoggerThreadWrapperTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ create_threaded_data2(int num_threads, itk::LoggerBase * logger)
ThreadDataVec threadData;
for (int ii = 0; ii < num_threads; ++ii)
{
threadData.push_back(ThreadDataStruct());
threadData.emplace_back();
threadData[ii].logger = logger;
}
return threadData;
Expand Down
30 changes: 15 additions & 15 deletions Modules/Core/Common/test/itkObjectFactoryTest3.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ itkObjectFactoryTest3(int, char *[])
itk::ObjectFactoryBase::RegisterFactory(factory2);
itk::ObjectFactoryBase::RegisterFactory(factory3);

descriptionList.push_back("factory1");
descriptionList.push_back("factory2");
descriptionList.push_back("factory3");
descriptionList.emplace_back("factory1");
descriptionList.emplace_back("factory2");
descriptionList.emplace_back("factory3");

int result = ListRegisteredFactories("TryA", descriptionList);

Expand All @@ -171,10 +171,10 @@ itkObjectFactoryTest3(int, char *[])
itk::ObjectFactoryBase::RegisterFactory(factory3);
itk::ObjectFactoryBase::RegisterFactory(factory4, itk::ObjectFactoryBase::InsertionPositionEnum::INSERT_AT_FRONT);

descriptionList.push_back("factory4");
descriptionList.push_back("factory1");
descriptionList.push_back("factory2");
descriptionList.push_back("factory3");
descriptionList.emplace_back("factory4");
descriptionList.emplace_back("factory1");
descriptionList.emplace_back("factory2");
descriptionList.emplace_back("factory3");

result = ListRegisteredFactories("TryB", descriptionList);

Expand All @@ -185,7 +185,7 @@ itkObjectFactoryTest3(int, char *[])


itk::ObjectFactoryBase::RegisterFactory(factory5, itk::ObjectFactoryBase::InsertionPositionEnum::INSERT_AT_BACK);
descriptionList.push_back("factory5");
descriptionList.emplace_back("factory5");

result = ListRegisteredFactories("TryC", descriptionList);

Expand All @@ -198,12 +198,12 @@ itkObjectFactoryTest3(int, char *[])
itk::ObjectFactoryBase::RegisterFactory(
factory6, itk::ObjectFactoryBase::InsertionPositionEnum::INSERT_AT_POSITION, 3);
descriptionList.clear();
descriptionList.push_back("factory4"); // position 0
descriptionList.push_back("factory1"); // position 1
descriptionList.push_back("factory2"); // position 2
descriptionList.push_back("factory6"); // position 3
descriptionList.push_back("factory3"); // position 4
descriptionList.push_back("factory5"); // position 5
descriptionList.emplace_back("factory4"); // position 0
descriptionList.emplace_back("factory1"); // position 1
descriptionList.emplace_back("factory2"); // position 2
descriptionList.emplace_back("factory6"); // position 3
descriptionList.emplace_back("factory3"); // position 4
descriptionList.emplace_back("factory5"); // position 5

result = ListRegisteredFactories("TryD", descriptionList);

Expand All @@ -214,7 +214,7 @@ itkObjectFactoryTest3(int, char *[])


itk::ObjectFactoryBase::RegisterFactory(factory7, itk::ObjectFactoryBase::InsertionPositionEnum::INSERT_AT_BACK);
descriptionList.push_back("factory7");
descriptionList.emplace_back("factory7");

result = ListRegisteredFactories("TryE", descriptionList);

Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/test/itkThreadLoggerTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ create_threaded_data(int num_threads, itk::LoggerBase * logger)
ThreadDataVec threadData;
for (int ii = 0; ii < num_threads; ++ii)
{
threadData.push_back(ThreadDataStruct());
threadData.emplace_back();
threadData[ii].logger = logger;
}
return threadData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ TriangleMeshToBinaryImageFilter<TInputMesh, TOutputImage>::PolygonToImageRaster(
if (extent[2] <= y && y <= extent[3])
{
const int zyidx = (z - extent[4]) * zInc + (y - extent[2]);
zymatrix[zyidx].push_back(Point1D(X, sign));
zymatrix[zyidx].emplace_back(X, sign);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ itkDTITubeSpatialObjectTest(int, char *[])
tubeNet1->AddChild(tube3);

// testing the GetChildren() function...
childrenList.push_back(tube1);
childrenList.push_back(tube2);
childrenList.push_back(tube3);
childrenList.emplace_back(tube1);
childrenList.emplace_back(tube2);
childrenList.emplace_back(tube3);

returnedList = tubeNet1->GetChildren();

Expand Down
6 changes: 3 additions & 3 deletions Modules/Core/SpatialObjects/test/itkTubeSpatialObjectTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ itkTubeSpatialObjectTest(int, char *[])
tubeNet1->AddChild(tube3);

// testing the GetChildren() function...
childrenList.push_back(tube1);
childrenList.push_back(tube2);
childrenList.push_back(tube3);
childrenList.emplace_back(tube1);
childrenList.emplace_back(tube2);
childrenList.emplace_back(tube3);

returnedList = tubeNet1->GetChildren();

Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/GDCM/src/itkGDCMSeriesFileNames.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ GDCMSeriesFileNames::GetSeriesUIDs()
// Create its unique series ID
const std::string id = m_SerieHelper->CreateUniqueSeriesIdentifier(file).c_str();

m_SeriesUIDs.push_back(id.c_str());
m_SeriesUIDs.emplace_back(id.c_str());
}
flist = m_SerieHelper->GetNextSingleSerieUIDFileSet();
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/ImageBase/include/itkImageSeriesWriter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ ImageSeriesWriter<TInputImage, TOutputImage>::GenerateNumericFileNames()
ITK_GCC_SUPPRESS_Wformat_nonliteral
snprintf(fileName, IOCommon::ITK_MAXPATHLEN + 1, m_SeriesFormat.c_str(), fileNumber);
ITK_GCC_PRAGMA_POP
m_FileNames.push_back(fileName);
m_FileNames.emplace_back(fileName);
fileNumber += this->m_IncrementIndex;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/IO/ImageBase/src/itkImageIOBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ ImageIOBase::GetSupportedReadExtensions() const
void
ImageIOBase::AddSupportedReadExtension(const char * extension)
{
this->m_SupportedReadExtensions.push_back(extension);
this->m_SupportedReadExtensions.emplace_back(extension);
}

void
ImageIOBase::AddSupportedWriteExtension(const char * extension)
{
this->m_SupportedWriteExtensions.push_back(extension);
this->m_SupportedWriteExtensions.emplace_back(extension);
}

void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ itkImageSeriesReaderDimensionsTest(int argc, char * argv[])
using Reader5DType = itk::ImageSeriesReader<Image5DType>;

Reader2DType::FileNamesContainer fname;
fname.push_back(argv[1]);
fname.emplace_back(argv[1]);

Reader2DType::FileNamesContainer fnames;
for (int i = 1; i < argc; ++i)
{
fnames.push_back(argv[i]);
fnames.emplace_back(argv[i]);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ itkImageSeriesReaderSamplingTest(int argc, char * argv[])
for (int i = 1; i < argc; ++i)
{
std::cout << argv[i] << std::endl;
fnames.push_back(argv[i]);
fnames.emplace_back(argv[i]);
}

std::cout << "testing reading a series of 2D images to 3D with extra slices" << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ itkImageSeriesReaderVectorTest(int argc, char * argv[])
VectorImageSeriesReader::FileNamesContainer fnames;
for (int i = 1; i < argc; ++i)
{
fnames.push_back(argv[i]);
fnames.emplace_back(argv[i]);
}


Expand Down
4 changes: 2 additions & 2 deletions Modules/IO/MeshBase/src/itkMeshIOBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ MeshIOBase::GetSupportedWriteExtensions() const
void
MeshIOBase::AddSupportedReadExtension(const char * extension)
{
this->m_SupportedReadExtensions.push_back(extension);
this->m_SupportedReadExtensions.emplace_back(extension);
}

void
MeshIOBase::AddSupportedWriteExtension(const char * extension)
{
this->m_SupportedWriteExtensions.push_back(extension);
this->m_SupportedWriteExtensions.emplace_back(extension);
}

unsigned int
Expand Down
4 changes: 2 additions & 2 deletions Modules/IO/PNG/test/itkPNGImageIOTest2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ itkPNGImageIOTest2(int argc, char * argv[])
// Check supported file extensions
// Expecting ".png" and ".PNG"
itk::ImageIOBase::ArrayOfExtensionsType expectedExtensions;
expectedExtensions.push_back(".png");
expectedExtensions.push_back(".PNG");
expectedExtensions.emplace_back(".png");
expectedExtensions.emplace_back(".PNG");

// Read extensions
itk::ImageIOBase::ArrayOfExtensionsType readExtensions = io->GetSupportedReadExtensions();
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/XML/src/itkDOMNode.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ DOMNode::AddChildAtEnd(DOMNode * node)
}

node->m_Parent = this;
this->m_Children.push_back(Pointer(node));
this->m_Children.emplace_back(node);
}

/** Replace a child (throw exception if not able to replace). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ IBPSOTest1(typename OptimizerType::CoefficientType inertiaCoefficient,

// set optimizer parameters
OptimizerType::ParameterBoundsType bounds;
bounds.push_back(std::make_pair(-10, 10));
bounds.emplace_back(-10, 10);
constexpr unsigned int numberOfParticles = 10;
constexpr unsigned int maxIterations = 200;
constexpr double xTolerance = 0.1;
Expand Down Expand Up @@ -289,8 +289,8 @@ IBPSOTest2(typename OptimizerType::CoefficientType inertiaCoefficient,

// set optimizer parameters
OptimizerType::ParameterBoundsType bounds;
bounds.push_back(std::make_pair(-10, 10));
bounds.push_back(std::make_pair(-10, 10));
bounds.emplace_back(-10, 10);
bounds.emplace_back(-10, 10);
constexpr unsigned int numberOfParticles = 10;
constexpr unsigned int maxIterations = 200;
constexpr double xTolerance = 0.1;
Expand Down Expand Up @@ -388,8 +388,8 @@ IBPSOTest3(typename OptimizerType::CoefficientType inertiaCoefficient,

// set optimizer parameters
OptimizerType::ParameterBoundsType bounds;
bounds.push_back(std::make_pair(-100, 100));
bounds.push_back(std::make_pair(-100, 100));
bounds.emplace_back(-100, 100);
bounds.emplace_back(-100, 100);
constexpr unsigned int numberOfParticles = 100;
constexpr unsigned int maxIterations = 1000;
constexpr double xTolerance = 0.1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ PSOTest1()

// set optimizer parameters
OptimizerType::ParameterBoundsType bounds;
bounds.push_back(std::make_pair(-10, 10));
bounds.emplace_back(-10, 10);
constexpr unsigned int numberOfParticles = 10;
constexpr unsigned int maxIterations = 100;
constexpr double xTolerance = 0.1;
Expand Down Expand Up @@ -220,8 +220,8 @@ PSOTest2()

// set optimizer parameters
OptimizerType::ParameterBoundsType bounds;
bounds.push_back(std::make_pair(-10, 10));
bounds.push_back(std::make_pair(-10, 10));
bounds.emplace_back(-10, 10);
bounds.emplace_back(-10, 10);
constexpr unsigned int numberOfParticles = 10;
constexpr unsigned int maxIterations = 100;
constexpr double xTolerance = 0.1;
Expand Down Expand Up @@ -299,8 +299,8 @@ PSOTest3()

// set optimizer parameters
OptimizerType::ParameterBoundsType bounds;
bounds.push_back(std::make_pair(-100, 100));
bounds.push_back(std::make_pair(-100, 100));
bounds.emplace_back(-100, 100);
bounds.emplace_back(-100, 100);
constexpr unsigned int numberOfParticles = 100;
constexpr unsigned int maxIterations = 200;
constexpr double xTolerance = 0.1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ itkBSplineSyNPointSetRegistrationTest(int itkNotUsed(argc), char * itkNotUsed(ar
fieldTransformAdaptor->SetMeshSizeForTheUpdateField(newUpdateMeshSize);
fieldTransformAdaptor->SetMeshSizeForTheTotalField(newTotalMeshSize);

adaptors.push_back(fieldTransformAdaptor);
adaptors.emplace_back(fieldTransformAdaptor);
}

displacementFieldRegistration->SetFixedPointSet(fixedPoints);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ itkSyNPointSetRegistrationTest(int itkNotUsed(argc), char * itkNotUsed(argv)[])
fieldTransformAdaptor->SetRequiredOrigin(shrinkFilter->GetOutput()->GetOrigin());
fieldTransformAdaptor->SetTransform(outputTransform);

adaptors.push_back(fieldTransformAdaptor);
adaptors.emplace_back(fieldTransformAdaptor);
}

displacementFieldRegistration->SetFixedPointSet(fixedPoints);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ itkTimeVaryingBSplineVelocityFieldPointSetRegistrationTest(int itkNotUsed(argc),
fieldTransformAdaptor->SetRequiredTransformDomainOrigin(velocityFieldOrigin);
fieldTransformAdaptor->SetRequiredTransformDomainMeshSize(transformDomainMeshSize);

adaptors.push_back(fieldTransformAdaptor);
adaptors.emplace_back(fieldTransformAdaptor);
}
velocityFieldRegistration->SetTransformParametersAdaptorsPerLevel(adaptors);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,21 @@ itkSampleClassifierFilterTest1(int, char *[])
MembershipFunctionType::CentroidType centroid1;
itk::NumericTraits<MembershipFunctionType::CentroidType>::SetLength(centroid1, numberOfComponents);
membershipFunction1->SetCentroid(centroid1);
membershipFunctionsVector.push_back(membershipFunction1);
membershipFunctionsVector.emplace_back(membershipFunction1);

const MembershipFunctionPointer membershipFunction2 = MembershipFunctionType::New();
membershipFunction1->SetMeasurementVectorSize(numberOfComponents);
MembershipFunctionType::CentroidType centroid2;
itk::NumericTraits<MembershipFunctionType::CentroidType>::SetLength(centroid2, numberOfComponents);
membershipFunction2->SetCentroid(centroid2);
membershipFunctionsVector.push_back(membershipFunction2);
membershipFunctionsVector.emplace_back(membershipFunction2);

const MembershipFunctionPointer membershipFunction3 = MembershipFunctionType::New();
membershipFunction3->SetMeasurementVectorSize(numberOfComponents);
MembershipFunctionType::CentroidType centroid3;
itk::NumericTraits<MembershipFunctionType::CentroidType>::SetLength(centroid3, numberOfComponents);
membershipFunction3->SetCentroid(centroid3);
membershipFunctionsVector.push_back(membershipFunction3);
membershipFunctionsVector.emplace_back(membershipFunction3);

try
{
Expand Down
Loading

0 comments on commit 0d1353b

Please sign in to comment.