Skip to content

Commit

Permalink
PERF: Let Singleton move its deleteFunc argument
Browse files Browse the repository at this point in the history
The move-constructor of `std::function<void()>` may be faster than its
copy-constructor.
  • Loading branch information
N-Dekker authored and hjmjohnson committed Jan 22, 2024
1 parent 06629c2 commit 5276de7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Modules/Core/Common/include/itkSingleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ITKCommon_EXPORT SingletonIndex
void
SetGlobalInstance(const char * globalName, T * global, std::function<void()> deleteFunc)
{
this->SetGlobalInstancePrivate(globalName, global, deleteFunc);
this->SetGlobalInstancePrivate(globalName, global, std::move(deleteFunc));
}

#ifndef ITK_FUTURE_LEGACY_REMOVE
Expand All @@ -84,7 +84,7 @@ class ITKCommon_EXPORT SingletonIndex
std::function<void(void *)> itkNotUsed(func),
std::function<void()> deleteFunc)
{
this->SetGlobalInstance(globalName, global, deleteFunc);
this->SetGlobalInstance(globalName, global, std::move(deleteFunc));
// Just returns true for backward compatibility (legacy only).
return true;
}
Expand Down Expand Up @@ -132,7 +132,7 @@ Singleton(const char * globalName, std::function<void()> deleteFunc)
if (instance == nullptr)
{
instance = new T;
SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, deleteFunc);
SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, std::move(deleteFunc));
}
return instance;
}
Expand All @@ -143,7 +143,7 @@ template <typename T>
[[deprecated("Prefer calling the Singleton(globalName, deleteFunc) overload (without the unused func parameter)!")]] T *
Singleton(const char * globalName, std::function<void(void *)> itkNotUsed(func), std::function<void()> deleteFunc)
{
return Singleton<T>(globalName, deleteFunc);
return Singleton<T>(globalName, std::move(deleteFunc));
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/src/itkSingleton.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ SingletonIndex::GetGlobalInstancePrivate(const char * globalName)
void
SingletonIndex::SetGlobalInstancePrivate(const char * globalName, void * global, std::function<void()> deleteFunc)
{
m_GlobalObjects.insert_or_assign(globalName, std::make_tuple(global, deleteFunc));
m_GlobalObjects.insert_or_assign(globalName, std::make_tuple(global, std::move(deleteFunc)));
}

SingletonIndex *
Expand Down

0 comments on commit 5276de7

Please sign in to comment.