Repository navigation
PERF: Use scanline iterators in per-pixel filter loops - #7258
Conversation
The inner loop of ImageScanlineIterator advances a plain offset, without ImageRegionIterator's per-pixel end-of-span check and call to Increment(). GCC and Clang vectorize these loops; MSVC does not.
The inner loop of ImageScanlineIterator advances a plain offset, without ImageRegionIterator's per-pixel end-of-span check and call to Increment(). GCC and Clang vectorize these loops; MSVC does not.
The inner loop of ImageScanlineIterator advances a plain offset, without ImageRegionIterator's per-pixel end-of-span check and call to Increment().
The inner loop of ImageScanlineIterator advances a plain offset, without ImageRegionIterator's per-pixel end-of-span check and call to Increment(). GCC and Clang vectorize the frame difference loop; MSVC does not.
[Medium impact] The changes affect performance and progress updates in several image-processing workflows. The PR appears safe to merge, with a non-blocking dilation progress issue. Findings
|
…tage ProgressReporter::CompletedPixel() adds a counter update and a branch to every iteration of these copy loops. TotalProgressReporter::Completed() reports the pixels of a whole line, or of a whole copy stage, at once. Unlike ProgressReporter, TotalProgressReporter does not set the progress to 1 when destroyed, and the border tracking stage can report fewer pixels than its share, so GenerateData() now ends with UpdateProgress(1).
1a11fac to
9692af0
Compare
hjmjohnson
left a comment
There was a problem hiding this comment.
Thanks, this looks correct. I checked that each pair of scanline iterators walks identical regions (including the adaptor requested regions in the three Gaussian filters) and that adaptor Get/Set still go through the pixel accessor. I found no blocking issue; everything below is a nit.
- Test for the progress fix (nit):
SimpleFilterWatcheronly fails when no progress event arrives at all, so removingUpdateProgress(1.0f)would not fail any test. Checkingfilter->GetProgress() == 1.0fafterUpdate()initkBinaryDilateImageFilterTestanditkBinaryErodeImageFilterTestwould cover it. - Description (nit): the swap to
TotalProgressReporterchanges the reporter for every loop in those two filters, and it no longer sets progress to 1 when destroyed, which is whyUpdateProgress(1.0f)was added. One sentence in the description would say so. A whole-stageCompleted(N)also checks for abort once per copy stage instead of about every 1%; that is fine for these cheap loops. - Commit bodies (nit): "ImageRegionIterator's per-pixel end-of-span check and call to Increment()." is 74 characters and appears in four commits; prose-budget.md asks for 72.
| // The border tracking stage reports fewer pixels than it was allotted. | ||
| this->UpdateProgress(1.0f); |
There was a problem hiding this comment.
nit: two small points on the Greptile fix, which is needed (UpdateOutputData sets 1.0 only after an abort).
progressis destroyed after this call, and~TotalProgressReporter()then adds its leftover pixels.IncrementProgresswraps past the maximum and clamps back, so observers get a secondProgressEventat 1.0. Putting the reporter in an inner block that ends before this line would avoid it.- In erode, the tracking stage counts neighbors when it tags them, including ones in the padded band, so it can report more or fewer than its share, not only fewer. The same comment is in both filters:
| // The border tracking stage reports fewer pixels than it was allotted. | |
| this->UpdateProgress(1.0f); | |
| // The border tracking stage does not report exactly its allotted pixels. |
Reasoned from the code, not run.
| // The border tracking stage reports fewer pixels than it was allotted. | ||
| this->UpdateProgress(1.0f); |
There was a problem hiding this comment.
nit: same as on BinaryDilate: the comment wording, and the second ProgressEvent at 1.0 from the reporter's destructor.
| // The border tracking stage reports fewer pixels than it was allotted. | |
| this->UpdateProgress(1.0f); | |
| // The border tracking stage does not report exactly its allotted pixels. |
|
Nice! |
|
I somehow merged this without seeing your second round of reviews, Hans. I will address them in a follow-up PR. |
No problem :). |
Switch per-pixel loops in nine filters to
ImageScanlineIterator, and report BinaryDilate/BinaryErode copy-loop progress per line or stage. Output is unchanged; on six compilers no filter is consistently slower.Which loops, and why these
ImageScanlineIterator's inner loop advances a plain offset, withoutImageRegionIterator's per-pixel end-of-span check and call toIncrement(). Iteration order is unchanged, so results are bit-identical.Round 1 converted 18 headers. An iterator change was kept only if it was at least 5% faster on some compiler and not consistently 5% slower on any:
The last commit replaces
ProgressReporter::CompletedPixel()in the simple copy loops of BinaryDilate and BinaryErode withTotalProgressReporter::Completed()once per line or per stage. Other loops in those filters still report per pixel. The old includes are kept next to the new ones, because client code can depend on them transitively.GCC 13 and Clang 18–23 vectorize the Gaussian derivative, FFTW and FrameDifference inner loops; on main they vectorize none of these loops. MSVC vectorizes none of them in either version.
Benchmark results
Whole-filter wall time of the final branch against its base, median of 3 rounds alternating the builds. Ranges cover all-thread and one-thread runs.
Machines: Windows (24 threads; MSVC and clang-cl), Linux i7-14700 (28 threads; GCC and Clang 18,
-O3without-march), Apple M1 (8 threads; AppleClang and Homebrew LLVM Clang). Sizes: 256³unsigned charfor the morphology and label map filters, 128³–256³floatfor the Gaussian filters, 256³ complex for FFTW, 32 frames of 1024² for the video filters.These numbers were measured with the branch on top of #7256's anchor morphology fixes, which do not touch these filters. Some filters whose source is unchanged moved by up to ±50% depending on what else was compiled in the same translation unit (inlining). Every such case was checked with mixed-header builds and is not caused by these changes.
Testing
On this base, MSVC: all 433 tests of ITKImageFeature, BoneEnhancement, ITKImageGradient, ITKVideoFiltering, ITKBinaryMathematicalMorphology, ITKMathematicalMorphology, ITKLabelMap and ITKFFT (with FFTW) pass, with no new warnings. On the previous base, the same modules also passed with GCC 13.3 and AppleClang 21.
AI assistance
🤖 Generated with Claude Code