PERF: Remove allocation on image casting
Summary
A lot of non necessary allocations are done almost every time an image is read.
This cost can be removed.
Rationale
No need to pay for computations that can be avoided.
On simple applications, a x2 speed up is easily observed. It was the case with https://gitlab.orfeo-toolbox.org/s1-tiling/normlim_sigma0 SARComputeLocalIncidenceAngle
that reads 2 images, and computes for each pixel 1 cross product, 2 norms, one arcsin
.
This MR relates to #2065 (closed).
Implementation Details
Classes and files
- A new class to iterate over pixel components is provided:
PixelComponentIterator
. For instance this iterator is able to convert aFixedArray<std::complex<float>>
into aVariableLengthArray<double>
-
ClampFilter
architecture is changed to efficiently handleVariableLengthVector
pixels: it's no longer anUnaryFunctorImageFilter
(that callsresult = f(in)
internally) but a plain filter that callsf(result, in)
on each pixel. This permits to avoid creating (and destroying) a new VLV for each pixel, and thus to avoid dynamic allocations. The filter has also been simplified and optimized. -
ConvertTypeFunctor
has been adapted for the new interface ofClampFilter
. Internally, it uses the newPixelComponentIterator
to copy components from the input pixel to the output pixel. Along the way, it has been simplified and optimized. -
CastImage
application has been simplified. Instead of converting any image into aVectorImage<double>
of the same number of components/bands, and then into the output image, it directly converts the input image into the output image -- thanks toClampFilter
. -
WrapperInputImageParameter
andWrapperOutputImageParameter
have been simplified
Also a few new helpers functions and classes have been provided:
- like a C++14 backport of C++17
std::clamp
- a
otb::is_array
traits that recognizesitk::VLV
and classes that inherits fromitk::FixedArray
-
GetNumberOfComponents()
that works around shortcomings ofitk::DefaultConvertPixelTraits<complex<>>::GetNumberOfComponents(pix)
(which doesn't supportstd::complex
and that copiesVLV
(see https://github.com/InsightSoftwareConsortium/ITK/issues/2312)) - a port of
gsl::not_null
into OTB.
Tests
Old tests for ClampFilter
(bfTvClampImageFilterConversionTest
) still runs.
A new test for PixelComponentIterator
has been added.
Copyright
The copyright owner is CSGroup FRANCE and has signed the ORFEO ToolBox Contributor License Agreement.
Check before merging:
- All discussions are resolved
- At least 2
👍 votes from core developers, no👎 vote. - The feature branch is (reasonably) up-to-date with the base branch
- Dashboard is green
- Copyright owner has signed the ORFEO ToolBox Contributor License Agreement
- Optionally, run
git diff develop... -U0 --no-color | clang-format-diff.py -p1 -i
on latest changes and commit