Skip to content

PERF: Use region iterator image ComposeBigVectorImageFilter #5306

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged

Conversation

blowekamp
Copy link
Member

This test is timing out on nightly valgrind and coverage builds.

These changes may reduce the number of temporaries heap allocations used, and improve performance. Local test in debug mode, improve performance by more than 10%.

PR Checklist

  • No API changes were made (or the changes have been approved)
  • No major design changes were made (or the changes have been approved)
  • Added test (or behavior not changed)
  • Updated API documentation (or API not changed)
  • Added license to new files (if any)
  • Added Python wrapping to new files (if any) as described in ITK Software Guide Section 9.5
  • Added ITK examples for all new major features (if any)

Refer to the ITK Software Guide for
further development details if necessary.

This test is timing out on nightly valgrind and coverage builds.

These changes may reduce the number of temporaries heap allocations
used, and improve performance. Local test in debug mode, improve
performance by more than 10%.
@github-actions github-actions bot added type:Performance Improvement in terms of compilation or execution time type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Filtering Issues affecting the Filtering module labels Apr 14, 2025
@blowekamp blowekamp marked this pull request as ready for review April 14, 2025 14:45
Copy link
Member

@thewtex thewtex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @blowekamp 💯

@blowekamp blowekamp merged commit abcaf2b into InsightSoftwareConsortium:master Apr 14, 2025
16 checks passed
Comment on lines +72 to +73
itk::ImageRegionConstIterator<VectorImageType> it(img, sliceRegion);
for (it.GoToBegin(); !it.IsAtEnd(); ++it)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blowekamp Cool. 👍 Some small suggestions...

it.GoToBegin() is not necessary. An ImageRegionConstIterator always starts at the begin. So these two lines could be replaced with one:

    for (itk::ImageRegionConstIterator<VectorImageType> it(img, sliceRegion); !it.IsAtEnd(); ++it)

Comment on lines +75 to +77
if (it.Get()[i] != i % 250)
{
itk::Index<3> idx = { { x, y, z } };

auto pixel = img->GetPixel(idx);
if (pixel[i] != i % 250)
{
++values[pixel[i]];
}
++values[it.Get()[i]];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, I would avoid calling it.Get()[i] twice. Instead, would it also work as follows?

    if (const auto channelValue = it.Get()[i]; channelValue != i % 250)
    {
      ++values[channelValue];
    }

for (unsigned int i = 0; i < nchannels; ++i)
{
itk::IndexValueType z = i;
std::map<unsigned int, unsigned int> values;

for (itk::IndexValueType y = 0; y < size; ++y)
sliceRegion.SetIndex(2, z); // Set the index of the z-dimension to the current slice
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If z should always just have the same value as i (the current channel number), I would suggest removing the variable z, and using i directly:

    sliceRegion.SetIndex(2, i);

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area:Filtering Issues affecting the Filtering module type:Performance Improvement in terms of compilation or execution time type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants