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
Merge request reports
Activity
added 2 commits
mentioned in merge request !799
mentioned in issue #2155 (closed)
Regarding the tests failing on Windows with vc14, I'm waiting to see how !799 evolves: hopefully the current MR should compile with a version of vc compliant with C++17.
Edited by Luc Hermitte@lhermitte : Ok, I've commented !799 before having a look at this MR. I understand the rationale of this MR and this sounds good to implement the corrections you propose, but maybe we have to discuss of the possible side effects...
@ytanguy Note that the code that doesn't compile on Windows is valid C++14 code (g++ and clang++ would have rejected it otherwise). It's just that vc14 is not fully compliant with C++14...
According to this table, we need to use at leastVisual Studio 2017 15.0 (1910, MSVC++14.1 ) to have full c++ 14 support. If msvc 14 does not fully support c++ 14, I think it makes sense to drop support for this compiler.
added 179 commits
-
c7bfc855...bc0cb8dd - 178 commits from branch
orfeotoolbox:develop
- 80bd885e - Merge branch 'develop' of https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb...
-
c7bfc855...bc0cb8dd - 178 commits from branch
mentioned in commit 807114a7
mentioned in merge request !827 (merged)