-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Intrinsify Enum.Equals to avoid boxing #122779
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
Open
EgorBo
wants to merge
8
commits into
dotnet:main
Choose a base branch
from
EgorBo:intrinsify-enum-equals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−2
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
64d0870
Intrinsify Enum.Equals
EgorBo e70c8ea
clean up
EgorBo a5e6e3d
replace isEnum with an assert
EgorBo a05f4a2
add tests
EgorBo 64bbe63
clean up
EgorBo 51312b5
fix build
EgorBo 7bb020c
Merge branch 'main' of https://github.com/dotnet/runtime into intrins…
EgorBo e6ed7a9
move to gtFoldCallExpr
EgorBo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,108 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // | ||
|
|
||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
| using Xunit; | ||
|
|
||
| public class EnumIntrinsics | ||
| { | ||
| public enum SByteEnum : sbyte { Min = sbyte.MinValue, Neg = -1, Zero = 0, Max = sbyte.MaxValue } | ||
| public enum ByteEnum : byte { Min = 0, Max = 255 } | ||
| public enum ShortEnum : short { Min = short.MinValue, Max = short.MaxValue } | ||
| public enum UShortEnum : ushort { Min = 0, Max = ushort.MaxValue } | ||
| public enum IntEnum : int { Min = int.MinValue, Zero = 0, Max = int.MaxValue } | ||
| public enum UIntEnum : uint { Min = 0, Max = uint.MaxValue } | ||
| public enum LongEnum : long { Min = long.MinValue, Max = long.MaxValue } | ||
| public enum ULongEnum : ulong { Min = 0, Max = ulong.MaxValue } | ||
| [Flags] public enum FlagsEnum { None = 0, A = 1, B = 2, All = 3 } | ||
|
|
||
| [Fact] | ||
| public static void TestEntryPoint() | ||
| { | ||
| TestSimpleEnums(); | ||
| TestGenericEnums(); | ||
| TestDifferentUnderlyingTypes(); | ||
| TestCornerCases(); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static void TestSimpleEnums() | ||
| { | ||
| // Testing bit-pattern identities | ||
| Assert.True(SByteEnum.Neg.Equals((SByteEnum)(-1))); | ||
| Assert.True(ByteEnum.Max.Equals((ByteEnum)255)); | ||
| Assert.False(SByteEnum.Max.Equals(SByteEnum.Min)); | ||
|
|
||
| // Flags behavior (bitwise equality) | ||
| FlagsEnum flags = FlagsEnum.A | FlagsEnum.B; | ||
| Assert.True(flags.Equals(FlagsEnum.All)); | ||
| Assert.False(flags.Equals(FlagsEnum.A)); | ||
|
|
||
| // 64-bit boundaries | ||
| Assert.True(ULongEnum.Max.Equals((ULongEnum)ulong.MaxValue)); | ||
| Assert.True(LongEnum.Min.Equals((LongEnum)long.MinValue)); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static void TestGenericEnums() | ||
| { | ||
| // Testing generic instantiation for every width | ||
| Assert.True(CheckGenericEquals(SByteEnum.Max, SByteEnum.Max)); | ||
| Assert.True(CheckGenericEquals(UShortEnum.Max, UShortEnum.Max)); | ||
| Assert.True(CheckGenericEquals(UIntEnum.Max, UIntEnum.Max)); | ||
| Assert.True(CheckGenericEquals(ULongEnum.Max, ULongEnum.Max)); | ||
|
|
||
| var container = new GenericEnumClass<IntEnum> { field = IntEnum.Min }; | ||
| Assert.True(CheckGenericEquals(container.field, IntEnum.Min)); | ||
| Assert.False(CheckGenericEquals(container.field, IntEnum.Max)); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static bool CheckGenericEquals<T>(T left, T right) where T : Enum => left.Equals(right); | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static void TestDifferentUnderlyingTypes() | ||
| { | ||
| // 0xFF pattern | ||
| Assert.False(((SByteEnum)(-1)).Equals((ByteEnum)255)); | ||
|
|
||
| // 0x00 pattern | ||
| Assert.False(SByteEnum.Zero.Equals(ByteEnum.Min)); | ||
|
|
||
| // 0xFFFF pattern | ||
| Assert.False(((ShortEnum)(-1)).Equals((UShortEnum)ushort.MaxValue)); | ||
|
|
||
| // 0xFFFFFFFF pattern | ||
| Assert.False(((IntEnum)(-1)).Equals((UIntEnum)uint.MaxValue)); | ||
|
|
||
| // Signed vs Unsigned same width | ||
| Assert.False(IntEnum.Max.Equals((UIntEnum)int.MaxValue)); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static void TestCornerCases() | ||
| { | ||
| // Ensure no false positives with primitive types (boxing checks) | ||
| Assert.False(IntEnum.Zero.Equals(0)); | ||
| Assert.False(LongEnum.Max.Equals(long.MaxValue)); | ||
|
|
||
| // Different enum types entirely | ||
| Assert.False(SimpleEnum.A.Equals(FlagsEnum.A)); | ||
|
|
||
| // Null and Object references | ||
| object obj = new object(); | ||
| Assert.False(SimpleEnum.B.Equals(obj)); | ||
| Assert.False(SimpleEnum.C.Equals(null)); | ||
|
|
||
| // Double boxing scenarios | ||
| object boxedA = SimpleEnum.A; | ||
| object boxedB = SimpleEnum.A; | ||
| Assert.True(boxedA.Equals(boxedB)); | ||
| Assert.True(SimpleEnum.A.Equals(boxedB)); | ||
| } | ||
|
|
||
| public class GenericEnumClass<T> where T : Enum { public T field; } | ||
| public enum SimpleEnum { A, B, C } | ||
| } | ||
This file contains hidden or 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,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <DebugType>None</DebugType> | ||
| <Optimize>True</Optimize> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="EnumIntrinsics.cs" /> | ||
| </ItemGroup> | ||
| </Project> | ||
EgorBo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.