Skip to content

Commit

Permalink
PERF: Use std::move inside itkSetMacro, declare parameter non-const
Browse files Browse the repository at this point in the history
Avoids an unnecessary copy (possibly involving memory allocation) of the
argument of a Set member function.

Note that `itkSetMacro` calls that define a Set member function which passes the
argument by const-reference must explicitly specify the `const` keyword now.
  • Loading branch information
N-Dekker authored and hjmjohnson committed Jun 20, 2023
1 parent da9ecbb commit e5011ae
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Modules/Core/Common/include/itkMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -966,17 +966,17 @@ compilers.
itkSetDecoratedObjectInputMacro(name, type); \
itkGetDecoratedObjectInputMacro(name, type)
/** Set built-in type. Creates member Set"name"() (e.g., SetVisibility()); */
/** Set built-in type or regular C++ type. Creates member Set"name"() (e.g., SetVisibility()); */
// clang-format off
#define itkSetMacro(name, type) \
virtual void Set##name(const type _arg) \
virtual void Set##name(type _arg) \
{ \
itkDebugMacro("setting " #name " to " << _arg); \
CLANG_PRAGMA_PUSH \
CLANG_SUPPRESS_Wfloat_equal \
if (this->m_##name != _arg) \
{ \
this->m_##name = _arg; \
this->m_##name = std::move(_arg); \
this->Modified(); \
} \
CLANG_PRAGMA_POP \
Expand Down
4 changes: 2 additions & 2 deletions Modules/IO/XML/include/itkDOMNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ class ITKIOXML_EXPORT DOMNode : public Object
GetParent() const;

/** Retrieve the tag name of this node. */
itkSetMacro(Name, std::string &);
itkSetMacro(Name, const std::string &);
itkGetConstReferenceMacro(Name, std::string);

/** Retrieve the special attribute "id" of this node. */
itkSetMacro(ID, std::string &);
itkSetMacro(ID, const std::string &);
itkGetConstReferenceMacro(ID, std::string);

/** Retrieve an attribute by key (return an empty string if not found). */
Expand Down
2 changes: 1 addition & 1 deletion Modules/IO/XML/include/itkDOMTextNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class DOMTextNode : public DOMNode
itkTypeMacro(DOMTextNode, DOMNode);

/** Functions to set/get the enclosed text of this node. */
itkSetMacro(Text, std::string &);
itkSetMacro(Text, const std::string &);
itkGetConstReferenceMacro(Text, std::string);

protected:
Expand Down

0 comments on commit e5011ae

Please sign in to comment.