Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Improve RGB Min Max evaluation performance by using 2 or 3 comparison… #50622

Merged
merged 5 commits into from
Apr 6, 2021

Conversation

L2
Copy link
Contributor

@L2 L2 commented Apr 1, 2021

Fixes: #46153

…s instead of 4

  • Replaces 2 calls each to Math.Min and Math.Max (a total of 4 required
    comparisons) with a single call to MinMaxRgb (at least 2 required
    comparisons and at worst 3).

…s instead of 4

* Replaces 2 calls each to Math.Min and Math.Max (a total of 4 required
  comparisons) with a single call to MinMaxRgb (at least 2 required
  comparisons and at worst 3).
@ghost
Copy link

ghost commented Apr 1, 2021

Tagging subscribers to this area: @safern, @tarekgh
See info in area-owners.md if you want to be subscribed.

Issue Details

…s instead of 4

  • Replaces 2 calls each to Math.Min and Math.Max (a total of 4 required
    comparisons) with a single call to MinMaxRgb (at least 2 required
    comparisons and at worst 3).
Author: L2
Assignees: -
Labels:

area-System.Drawing

Milestone: -

@L2
Copy link
Contributor Author

L2 commented Apr 1, 2021

dotnet/performance microbenchmarks filtered with System.Drawing.Tests.Perf_Color*:

Base = .NET6 Before this changeset
Diff = .NET6 After this changeset

No Slower results for the provided threshold = 2% and noise filter = 25ns.

Faster base/diff Base Median (ns) Diff Median (ns) Modality
System.Drawing.Tests.Perf_Color.GetHue 1.23 1756.53 1432.47
System.Drawing.Tests.Perf_Color.GetBrightness 1.09 1544.74 1415.17
System.Drawing.Tests.Perf_Color.GetSaturation 1.09 1602.51 1476.70

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void MinMaxRgb(out int min, out int max, int r, int g, int b)
{
if (b < g) {

Choose a reason for hiding this comment

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

Move trailing braces to their own lines for consistency with existing .NET code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, updated.

@danmoseley
Copy link
Member

Do we have sufficient functional test coverage?

@L2
Copy link
Contributor Author

L2 commented Apr 2, 2021

Do we have sufficient functional test coverage?

Thanks @danmoseley . There were a couple of test cases missing which I've just added:

All Possible Cases:

label for existing test case
(1) r == g == b
(2) r == g < b
(3) r == g > b
(4) r < g == b
(missing) r < g < b
r < g > b
(5) where [r == b]
(missing) where [r > b]
(missing) where [r < b]
(7) r > g == b
r > g < b
(6) where [r == b]
(missing) where [r > b]
(missing) where [r < b]
(missing) r > g > b

Existing Test Coverage:

test label case
(1) [InlineData(0, 0, 0, 0f)]
[InlineData(255, 255, 255, 1f)]
(7) [InlineData(255, 0, 0, 0.5f)]
(5) [InlineData(0, 255, 0, 0.5f)]
(2) [InlineData(0, 0, 255, 0.5f)]
(3) [InlineData(255, 255, 0, 0.5f)]
(6) [InlineData(255, 0, 255, 0.5f)]
(4) [InlineData(0, 255, 255, 0.5f)]
[InlineData(51, 255, 255, 0.6f)]
[InlineData(255, 51, 255, 0.6f)]
[InlineData(255, 255, 51, 0.6f)]
[InlineData(255, 51, 51, 0.6f)]
[InlineData(51, 255, 51, 0.6f)]
[InlineData(51, 51, 255, 0.6f)]
[InlineData(51, 51, 51, 0.2f)]

For:

public void GetBrightness(int r, int g, int b, float expected)
{
    Assert.Equal(expected, Color.FromArgb(r, g, b).GetBrightness());
}

@L2
Copy link
Contributor Author

L2 commented Apr 2, 2021

Also I wasn't sure if I should put this new method in Math.cs and make it a general use case to find the min and max of three values so I've left it inside Color.cs for now.

@danmoseley
Copy link
Member

wasn't sure if I should put this new method in Math.cs

Drawing wouldn't be able to use anything from Math unless it was public: the libraries can only use each other's public surface. And to make new public API involves a review process, which would include consideration of how broadly useful the API would be. So I think it is in the right place.

Comment on lines 498 to 501
if (r < g)
max = g;
else
max = r;
Copy link
Member

Choose a reason for hiding this comment

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

We could simplify this to use ? assignment?

Suggested change
if (r < g)
max = g;
else
max = r;
max = r < g ? g : r;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, updated.

Comment on lines 514 to 517
if (g < r)
min = g;
else
min = r;
Copy link
Member

Choose a reason for hiding this comment

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

Ditto:

Suggested change
if (g < r)
min = g;
else
min = r;
min = g < r ? g : r;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, updated.

Comment on lines 531 to 532
int min, max;
MinMaxRgb(out min, out max, r, g, b);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
int min, max;
MinMaxRgb(out min, out max, r, g, b);
MinMaxRgb(out int min, out int max, r, g, b);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, updated.

Comment on lines 544 to 545
int min, max;
MinMaxRgb(out min, out max, r, g, b);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
int min, max;
MinMaxRgb(out min, out max, r, g, b);
MinMaxRgb(out int min, out int max, r, g, b);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, updated.

Comment on lines 571 to 572
int min, max;
MinMaxRgb(out min, out max, r, g, b);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
int min, max;
MinMaxRgb(out min, out max, r, g, b);
MinMaxRgb(out int min, out int max, r, g, b);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, updated.

Copy link
Member

@safern safern left a comment

Choose a reason for hiding this comment

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

Other than small style improvements, looks good. Thanks!

@L2
Copy link
Contributor Author

L2 commented Apr 2, 2021

Other than small style improvements, looks good. Thanks!

Thanks @safern , I've updated it with the requested changes.

@ghost
Copy link

ghost commented Apr 2, 2021

Hello @safern!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

Comment on lines 491 to 519
private void MinMaxRgb(out int min, out int max, int r, int g, int b)
{
if (b < g)
{
if (b < r)
{
min = b;
max = r < g ? g : r;
}
else
{
min = r;
max = g;
}
}
else
{
if (r < b)
{
max = b;
min = g < r ? g : r;
}
else
{
max = r;
min = g;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This can be simplified to instead be:

Suggested change
private void MinMaxRgb(out int min, out int max, int r, int g, int b)
{
if (b < g)
{
if (b < r)
{
min = b;
max = r < g ? g : r;
}
else
{
min = r;
max = g;
}
}
else
{
if (r < b)
{
max = b;
min = g < r ? g : r;
}
else
{
max = r;
min = g;
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void MinMaxRgb(out int min, out int max, int r, int g, int b)
{
if (r > g)
{
max = r;
min = g;
}
else
{
max = g;
min = r;
}
if (b > max)
{
max = b;
}
else if (b < min)
{
min = b;
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @stephentoub , I ran the perf_color microbenchmarks against 1) before this PR changeset, 2) the current PR's changeset, 3) proposed simplification

**.NET6 Before VS .NET6 with current PR changeset: Run1** summary: better: 3, geomean: 1.151 total diff: 3

No Slower results for the provided threshold = 2% and noise filter = 25ns.

Faster base/diff Base Median (ns) Diff Median (ns) Modality
System.Drawing.Tests.Perf_Color.GetBrightness 1.23 1549.96 1259.50
System.Drawing.Tests.Perf_Color.GetHue 1.18 1753.24 1484.57
System.Drawing.Tests.Perf_Color.GetSaturation 1.05 1451.59 1383.79
**.NET6 Before VS .NET6 with current PR changeset: Run2** summary: better: 3, geomean: 1.208 total diff: 3

No Slower results for the provided threshold = 2% and noise filter = 25ns.

Faster base/diff Base Median (ns) Diff Median (ns) Modality
System.Drawing.Tests.Perf_Color.GetBrightness 1.23 1543.19 1259.71
System.Drawing.Tests.Perf_Color.GetSaturation 1.22 1682.03 1384.17
System.Drawing.Tests.Perf_Color.GetHue 1.18 1758.68 1485.53
**.NET6 Before VS .NET6 with current PR changeset: Run3** summary: better: 3, geomean: 1.189 total diff: 3

No Slower results for the provided threshold = 2% and noise filter = 25ns.

Faster base/diff Base Median (ns) Diff Median (ns) Modality
System.Drawing.Tests.Perf_Color.GetSaturation 1.22 1682.42 1384.03
System.Drawing.Tests.Perf_Color.GetBrightness 1.19 1497.18 1259.28
System.Drawing.Tests.Perf_Color.GetHue 1.16 1725.25 1483.41


**.NET6 Before VS .NET6 with proposed simplification: Run1** summary: better: 2, geomean: 1.123 total diff: 2

No Slower results for the provided threshold = 2% and noise filter = 25ns.

Faster base/diff Base Median (ns) Diff Median (ns) Modality
System.Drawing.Tests.Perf_Color.GetBrightness 1.13 1549.96 1375.52
System.Drawing.Tests.Perf_Color.GetHue 1.12 1753.24 1565.75
**.NET6 Before VS .NET6 with proposed simplification: Run2** summary: better: 3, geomean: 1.135 total diff: 3

No Slower results for the provided threshold = 2% and noise filter = 25ns.

Faster base/diff Base Median (ns) Diff Median (ns) Modality
System.Drawing.Tests.Perf_Color.GetSaturation 1.16 1682.03 1448.45
System.Drawing.Tests.Perf_Color.GetHue 1.12 1758.68 1566.17
System.Drawing.Tests.Perf_Color.GetBrightness 1.12 1543.19 1375.17
**.NET6 Before VS .NET6 with proposed simplification: Run3** summary: better: 2, geomean: 1.132 worse: 1, geomean: 1.050 total diff: 3
Slower diff/base Base Median (ns) Diff Median (ns) Modality
System.Drawing.Tests.Perf_Color.GetBrightness 1.05 1497.18 1572.36
Faster base/diff Base Median (ns) Diff Median (ns) Modality
System.Drawing.Tests.Perf_Color.GetSaturation 1.16 1682.42 1446.59
System.Drawing.Tests.Perf_Color.GetHue 1.10 1725.25 1565.42
---------------------- ----------------------

Some of the benchmarks seem to get a bit slower on average after the simplification. I think what is happening may be two causes:

  1. There are paths with an extra store (we store max & min in the first conditional and then if the second conditional is taken one of these is overwritten).
  2. Just my guess, but processor pipelining with branch prediction may find it easier to go through each of the conditionals in the current implementation as there are no dependencies. The simplification introduces a dependency on the second conditional as it needs the values from the first? Just a guess here.

I will follow yours and @safern 's recommendation on which to keep. Thanks again.

Choose a reason for hiding this comment

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

I didn't look at the disassembly but there might be an aliasing complication also, if the JIT compiler has to account for the possibility that min and max refer to the same location. Although perhaps it can disprove that after inlining.

Copy link
Member

Choose a reason for hiding this comment

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

I actually see them get faster on my machine. Regardless, though, I'd rather keep the code smaller, especially with it triplicated due to the aggressive inlining.

Choose a reason for hiding this comment

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

One more possible improvement - using local valiables instead of out parameters:

		private static void MinMaxRgb(out int min, out int max, int r, int g, int b)
		{
			int minLoc, maxLoc;
			if (r > g)
			{
				maxLoc = r;
				minLoc = g;
			}
			else
			{
				maxLoc = g;
				minLoc = r;
			}

			if (b > maxLoc)
			{
				maxLoc = b;
			}
			else if (b < minLoc)
			{
				minLoc = b;
			}

			max = maxLoc;
			min = minLoc;
		}

Copy link
Contributor Author

@L2 L2 Apr 5, 2021

Choose a reason for hiding this comment

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

I actually see them get faster on my machine. Regardless, though, I'd rather keep the code smaller, especially with it triplicated due to the aggressive inlining.

Sounds good @stephentoub , I've updated it with the requested changes. (I'm not sure how crediting works in this project as this is my second pull request, so I've added your username to the commit details as the originator of the simplification).

One more possible improvement - using local valiables instead of out parameters:

Thanks @hypeartist, I'm wondering if these local variables will be eliminated due to inlining, so performance will be about the same?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how crediting works in this project as this is my second pull request, so I've added your username to the commit details as the originator of the simplification

Thanks, but not necessary... I comment on a lot of PRs :)

* Credit for this simplification: stephentoub
@ghost ghost removed the auto-merge label Apr 5, 2021
@danmoseley
Copy link
Member

Thanks for the contribution @L2 - perhaps you'd like to pick up something else?

thaystg added a commit to thaystg/runtime that referenced this pull request Apr 6, 2021
…shim_mono

# By Aaron Robinson (10) and others
# Via GitHub
* upstream/main: (108 commits)
  [mbr] Add Apple sample (dotnet#50740)
  make EstablishProxyTunnelAsync throw on failure status code from proxy (dotnet#50763)
  Improve RGB Min Max evaluation performance by using 2 or 3 comparison… (dotnet#50622)
  [mono] More domain cleanups (dotnet#50479)
  Fix Crossgen2 of PlatformDefaultMemberFunction methods and calls. (dotnet#50754)
  Disable EventSource generator in design-time builds (dotnet#50741)
  Fix X509 test failures on Android (dotnet#50301)
  Do not confuse fgDispBasicBlocks in fgMorphBlocks (dotnet#50703)
  Enforce 64KB event payload size limit on EventPipe  (dotnet#50600)
  Reorganize CoreCLR native build to reduce CMake reconfigures when the build system is untouched (dotnet#49906)
  [mbr] Turn on hot reload for iOS, tvOS and MacCatalyst (dotnet#50458)
  improve connection scavenge logic by doing zero-byte read (dotnet#50545)
  Resolve call mdtokens when making tier 1 inline observations (dotnet#50675)
  Annotate APIs in System.Private.Xml (dotnet#49682)
  Support compiling against OpenSSL 3 headers
  Change Configuration.Json to use a regular Dictionary. (dotnet#50611)
  Remove unused BigNumFromBinary P/Invoke (dotnet#50670)
  Make Ninja the default CMake generator on Windows for the repo (dotnet#49715)
  [AppleAppBuilder] Entitlements to run tests on catalyst using the JIT (dotnet#50637)
  [mono] Fix delegate invokes to dynamic methods in mixed mode. (dotnet#50547)
  ...

# Conflicts:
#	src/mono/dlls/mscordbi/CMakeLists.txt
@L2 L2 deleted the rgbMinMax branch April 6, 2021 15:05
@ghost ghost locked as resolved and limited conversation to collaborators May 9, 2021
@karelz karelz added this to the 6.0.0 milestone May 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Perf] Changes at 12/9/2020 1:20:52 PM
8 participants