Skip to content

PERF: FUTURE: Default default-constructors of RGBPixel and RGBAPixel#4469

Merged
dzenanz merged 1 commit intoInsightSoftwareConsortium:masterfrom
N-Dekker:Default-RGBPixel-default-constructors
Feb 21, 2024
Merged

PERF: FUTURE: Default default-constructors of RGBPixel and RGBAPixel#4469
dzenanz merged 1 commit intoInsightSoftwareConsortium:masterfrom
N-Dekker:Default-RGBPixel-default-constructors

Conversation

@N-Dekker
Copy link
Copy Markdown
Contributor

@N-Dekker N-Dekker commented Feb 19, 2024

Will make image.Allocate() and image.Allocate(false) calls much, much
faster, for RGB and RGBA images, when ITK_FUTURE_LEGACY_REMOVE is enabled.
An Allocate() call on a 1024x1024x1024 RGB image was observed to take more than
1.3 sec. before this commit, and less than 0.001 sec. after this commit, having
ITK_FUTURE_LEGACY_REMOVE=ON, using Visual C++ 2019 Release. Even
image.Allocate(true) (initializePixels = true) appears slightly faster now
with ITK_FUTURE_LEGACY_REMOVE.

Adjusted unit tests tests from itkCommonTypeTraitsGTest, because RGBPixel and
RGBAPixel become "trivial" types when they get defaulted default-constructors.

@github-actions github-actions Bot added type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module labels Feb 19, 2024
Will make `image.Allocate()` and `image.Allocate(false)` calls much, much
faster, for RGB and RGBA images, when `ITK_FUTURE_LEGACY_REMOVE` is enabled.
An `Allocate()` call on a 1024x1024x1024 RGB image was observed to take more than
1.3 sec. before this commit, and less than 0.001 sec. after this commit, having
`ITK_FUTURE_LEGACY_REMOVE=ON`, using Visual C++ 2019 Release. Even
`image.Allocate(true)` (`initializePixels` = true) appears slightly faster now
with `ITK_FUTURE_LEGACY_REMOVE`.

Adjusted unit tests tests from itkCommonTypeTraitsGTest, because `RGBPixel` and
`RGBAPixel` become "trivial" types when they get defaulted default-constructors.
@N-Dekker N-Dekker force-pushed the Default-RGBPixel-default-constructors branch from 688a0f8 to 8d77af6 Compare February 20, 2024 09:08
@N-Dekker N-Dekker changed the title WIP: PERF: Default default-constructors of RGBPixel and RGBAPixel PERF: FUTURE: Default default-constructors of RGBPixel and RGBAPixel Feb 20, 2024
@N-Dekker N-Dekker marked this pull request as ready for review February 20, 2024 12:20
/** Default-constructor.
* \note The other five "special member functions" are defaulted implicitly, following the C++ "Rule of Zero". */
#ifdef ITK_FUTURE_LEGACY_REMOVE
RGBAPixel() = default;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why protect this by legacy ifdef? Does this change introduce backwards compatibility issues? Which ones?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I agree with @dzenanz. I don't immediately see a backward-compatible problem with the code change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for asking! With the original code, even rgbImage.Allocate(false) (with initializePixels parameter = false) zero-fills the RGB pixels. Whereas when having a defaulted default-constructor for RGBPixel, only rgbImage.Allocate(true) zero-initializes the pixels. So with this change, users have to explicitly specify the parameter initializePixels = true, if that is what they want.

Also, a declared local variable of an RGB pixel type may not be initialized, with this change. So in the code below, the behavior may change for pixel1. It will not change for pixel2, pixel3, and pixel4.

RGBPixel<> pixel1; // May not be initialized, after this change 
RGBPixel<> pixel2{}; // Properly zero-initialized.
RGBPixel<> pixel3 = RGBPixel<>{}; // Properly initialized.
RGBPixel<> pixel4 = RGBPixel<>(); // Properly initialized.

Obviously it has been like that for other pixel types already, including int, float, itk::Vector<int> etc. But it's a behavior change for users who expect RGB pixels to always be initialized by default.

Anyway, I can also directly make it LEGACY_REMOVE, instead of just FUTURE_LEGACY_REMOVE. Would you like that better?

Copy link
Copy Markdown
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

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

Thanks for explaining. I am on the fence of legacy vs future legacy.

@N-Dekker
Copy link
Copy Markdown
Contributor Author

I am on the fence of legacy vs future legacy.

Thanks @dzenanz. I am also on the fence of legacy vs future legacy. 😸 But only if someone is strongly in favor of LEGACY_REMOVE, instead of FUTURE_LEGACY_REMOVE, I would still adjust the PR. @hjmjohnson Is FUTURE_LEGACY_REMOVE now OK to you?

@dzenanz dzenanz merged commit 755cd10 into InsightSoftwareConsortium:master Feb 21, 2024
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 21, 2024
Follow-up to pull request InsightSoftwareConsortium#4469
commit 755cd10
"PERF: FUTURE: Default default-constructors of `RGBPixel` and `RGBAPixel`"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 21, 2024
Follow-up to pull request InsightSoftwareConsortium#4469
commit 755cd10
"PERF: FUTURE: Default default-constructors of `RGBPixel` and `RGBAPixel`"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 21, 2024
Follow-up to pull request InsightSoftwareConsortium#4469
commit 755cd10
"PERF: FUTURE: Default default-constructors of `RGBPixel` and `RGBAPixel`"
dzenanz pushed a commit that referenced this pull request Feb 26, 2024
Follow-up to pull request #4469
commit 755cd10
"PERF: FUTURE: Default default-constructors of `RGBPixel` and `RGBAPixel`"
hjmjohnson pushed a commit to hjmjohnson/ITK that referenced this pull request May 6, 2026
Follow-up to pull request InsightSoftwareConsortium#4469
commit 77faea3
"PERF: FUTURE: Default default-constructors of `RGBPixel` and `RGBAPixel`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module 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