-
Notifications
You must be signed in to change notification settings - Fork 691
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Use field aliasing to improve Color.GetHashCode and Color.Equals
- Loading branch information
1 parent
81e9ce8
commit 429d367
Showing
4 changed files
with
146 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...p/Benchmarks.Shared/Suite/Windows_UI/ColorBench/XamlControlsResourcesCreationBenchmark.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.UI.Xaml.Controls; | ||
using Windows.UI; | ||
|
||
namespace SamplesApp.Benchmarks.Suite.Windows_UI.ColorBench | ||
{ | ||
public class ColorBenchmark | ||
{ | ||
Color _color1 = Color.FromArgb(0xFF, 0xFF, 0x00, 0x00), _color2 = Color.FromArgb(0xFF, 0x00, 0xFF, 0x00); | ||
|
||
[Benchmark] | ||
public void Create_Color() | ||
{ | ||
var c = new Color(); | ||
} | ||
|
||
[Benchmark] | ||
public void Create_Color_WithParams() | ||
{ | ||
var c = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF); | ||
} | ||
|
||
[Benchmark] | ||
public void Create_Color_GetHashCode() | ||
{ | ||
var c = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF).GetHashCode(); | ||
} | ||
|
||
[Benchmark] | ||
public void Create_Color_op_Equals() | ||
{ | ||
var b = _color1 == _color2; | ||
} | ||
|
||
[Benchmark] | ||
public void Create_Color_Equals() | ||
{ | ||
var b = _color1.Equals(_color2); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
| ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Windows.Foundation; | ||
using Windows.System; | ||
using Windows.UI; | ||
using Windows.UI.Core; | ||
|
||
namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Core | ||
{ | ||
[TestClass] | ||
public class Given_Color | ||
{ | ||
#if HAS_UNO | ||
[TestMethod] | ||
[DynamicData(nameof(GetData), DynamicDataSourceType.Method)] | ||
public async Task When_FromArgb(byte a, byte r, byte g, byte b, uint result) | ||
=> Assert.AreEqual(result, Color.FromArgb(a, r, g, b).AsUInt32()); | ||
|
||
[TestMethod] | ||
[DynamicData(nameof(GetData), DynamicDataSourceType.Method)] | ||
public async Task When_GetHashCode(byte a, byte r, byte g, byte b, uint result) | ||
=> Assert.AreEqual(result, (uint)Color.FromArgb(a, r, g, b).GetHashCode()); | ||
|
||
public static IEnumerable<object[]> GetData() | ||
{ | ||
yield return new object[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (uint)0x00000000 }; | ||
yield return new object[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00, (uint)0xFF000000 }; | ||
yield return new object[] { (byte)0x00, (byte)0xFF, (byte)0x00, (byte)0x00, (uint)0x00FF0000 }; | ||
yield return new object[] { (byte)0xFF, (byte)0x00, (byte)0xFF, (byte)0x00, (uint)0xFF00FF00 }; | ||
yield return new object[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0xFF, (uint)0xFF0000FF }; | ||
yield return new object[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x00, (uint)0x7F000000 }; | ||
yield return new object[] { (byte)0xFF, (byte)0x7F, (byte)0x00, (byte)0x00, (uint)0xFF7F0000 }; | ||
yield return new object[] { (byte)0xFF, (byte)0x00, (byte)0x7F, (byte)0x00, (uint)0xFF007F00 }; | ||
yield return new object[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x7F, (uint)0xFF00007F }; | ||
yield return new object[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (uint)0xFFFFFFFF }; | ||
} | ||
#endif | ||
|
||
[TestMethod] | ||
[DynamicData(nameof(GetCompare), DynamicDataSourceType.Method)] | ||
public async Task When_Equals(Color left, Color right, bool result) | ||
=> Assert.AreEqual(result, left.Equals(right)); | ||
|
||
[TestMethod] | ||
[DynamicData(nameof(GetCompare), DynamicDataSourceType.Method)] | ||
public async Task When_op_Equals(Color left, Color right, bool result) | ||
=> Assert.AreEqual(result, left == right); | ||
|
||
public static IEnumerable<object[]> GetCompare() | ||
{ | ||
yield return new object[] { ColorHelper.FromArgb(0xFF, 0xFF, 0xFF, 0xFF), ColorHelper.FromArgb(0xFF, 0xFF, 0xFF, 0xFF), true }; | ||
yield return new object[] { ColorHelper.FromArgb(0xFF, 0x7F, 0xFF, 0xFF), ColorHelper.FromArgb(0xFF, 0xFF, 0xFF, 0xFF), false }; | ||
yield return new object[] { ColorHelper.FromArgb(0xFF, 0xFF, 0x7F, 0xFF), ColorHelper.FromArgb(0xFF, 0xFF, 0xFF, 0xFF), false }; | ||
yield return new object[] { ColorHelper.FromArgb(0xFF, 0xFF, 0xFF, 0x7F), ColorHelper.FromArgb(0xFF, 0xFF, 0xFF, 0xFF), false }; | ||
yield return new object[] { ColorHelper.FromArgb(0x00, 0x00, 0x00, 0x00), ColorHelper.FromArgb(0xFF, 0xFF, 0xFF, 0xFF), false }; | ||
yield return new object[] { ColorHelper.FromArgb(0x00, 0x00, 0x00, 0x00), ColorHelper.FromArgb(0x00, 0x00, 0x00, 0x00), true }; | ||
yield return new object[] { ColorHelper.FromArgb(0x7F, 0xFF, 0xFF, 0xFF), ColorHelper.FromArgb(0x7F, 0xFF, 0xFF, 0xFF), true }; | ||
yield return new object[] { ColorHelper.FromArgb(0xFF, 0x7F, 0xFF, 0xFF), ColorHelper.FromArgb(0xFF, 0x7F, 0xFF, 0xFF), true }; | ||
yield return new object[] { ColorHelper.FromArgb(0xFF, 0xFF, 0x7F, 0xFF), ColorHelper.FromArgb(0xFF, 0xFF, 0x7F, 0xFF), true }; | ||
yield return new object[] { ColorHelper.FromArgb(0xFF, 0xFF, 0xFF, 0x7F), ColorHelper.FromArgb(0xFF, 0xFF, 0xFF, 0x7F), true }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters