InsightSoftwareConsortium / ITK

Insight Toolkit (ITK) -- Official Repository. ITK builds on a proven, spatially-oriented architecture for processing, segmentation, and registration of scientific images in two, three, or more dimensions.
https://itk.org
Apache License 2.0
1.37k stars 660 forks source link

PERF: Add fast `noexcept` "move semantics" to `Array2D` #4728

Closed N-Dekker closed 1 week ago

N-Dekker commented 1 week ago

Added a noexcept move-constructor and move-assignment operator to Array2D, which both move the data of the Array2D.

Previously, an attempt to "move" an Array2D would have done an expensive copy.

N-Dekker commented 1 week ago

Example in the current code, from https://github.com/InsightSoftwareConsortium/ITK/blob/abb592882a3f4665bdfd7ff4ce835bd3862b835d/Modules/Registration/Common/include/itkMultiResolutionPyramidImageFilter.hxx#L62

    m_Schedule = ScheduleType(m_NumberOfLevels, ImageDimension, 0);

ScheduleType is defined as itk::Array2D<unsigned int>, so ScheduleType(m_NumberOfLevels, ImageDimension, 0) yields a temporary Array2D object (an r-value). With this pull request, the statement above here will do a fast noexcept move-assignment. (Until now, it would do a rather expensive copy.)

Probably not the best example, because such a schedule object is usually quite small anyway. But you get the picture 😸