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 performance of Enum.CompareTo #37845

Merged
merged 1 commit into from
Jun 15, 2020
Merged

Conversation

jkotas
Copy link
Member

@jkotas jkotas commented Jun 13, 2020

Enum.CompareTo was rewritten in C# recently. This set of fixes gets the performance on par with the previous C++ implementation

  • Add optimization for obj1.GetType() == obj2.GetType() pattern the JIT
  • Optimize FCall used by Enum.CompareTo

Fixes DrewScoggins/performance-2#525

@jkotas jkotas added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI and removed area-VM-coreclr labels Jun 13, 2020
@jkotas jkotas requested a review from AndyAyersMS June 13, 2020 03:33
Enum.CompareTo was rewritten in C# recently. This set of fixes gets the performance on par with the previous C++ implementation

- Add optimization for obj1.GetType() == obj2.GetType() pattern the JIT
- Optimize FCall used by Enum.CompareTo

Fixes DrewScoggins/performance-2#525
@jkotas jkotas added the tenet-performance Performance related issue label Jun 15, 2020
Copy link
Member

@AndyAyersMS AndyAyersMS left a comment

Choose a reason for hiding this comment

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

LGTM.

Did you try running diffs?

@jkotas
Copy link
Member Author

jkotas commented Jun 15, 2020

Total bytes of diff: -2299 (-0.007% of base)
    diff is an improvement.
Top file improvements (bytes):
       -1747 : System.Private.CoreLib.dasm (-0.054% of base)
         -97 : System.Security.Permissions.dasm (-0.595% of base)
         -80 : System.Configuration.ConfigurationManager.dasm (-0.026% of base)
         -78 : Microsoft.CodeAnalysis.dasm (-0.010% of base)
         -74 : Microsoft.CSharp.dasm (-0.023% of base)
         -50 : System.Data.Common.dasm (-0.005% of base)
         -41 : xunit.assert.dasm (-0.083% of base)
         -30 : Microsoft.VisualBasic.Core.dasm (-0.007% of base)
         -27 : Microsoft.Diagnostics.NETCore.Client.dasm (-0.322% of base)
         -21 : System.ComponentModel.TypeConverter.dasm (-0.009% of base)
         -18 : Newtonsoft.Json.dasm (-0.003% of base)
         -18 : System.Private.Xml.dasm (-0.001% of base)
          -9 : xunit.core.dasm (-0.039% of base)
          -9 : xunit.execution.dotnet.dasm (-0.005% of base)
14 total files with Code Size differences (14 improved, 0 regressed), 249 unchanged.
Top method improvements (bytes):
       -1044 (-63.158% of base) : System.Private.CoreLib.dasm - GenericComparer`1:Equals(Object):bool:this (29 methods)
        -216 (-63.158% of base) : System.Private.CoreLib.dasm - ObjectEqualityComparer`1:Equals(Object):bool:this (6 methods)
        -108 (-63.158% of base) : System.Private.CoreLib.dasm - ObjectComparer`1:Equals(Object):bool:this (3 methods)
         -72 (-63.158% of base) : System.Private.CoreLib.dasm - NullableEqualityComparer`1:Equals(Object):bool:this (2 methods)
         -72 (-63.158% of base) : System.Private.CoreLib.dasm - NullableComparer`1:Equals(Object):bool:this (2 methods)
         -44 (-11.828% of base) : Microsoft.CSharp.dasm - ComEventsMethod:RemoveDelegate(Delegate,bool):this
         -44 (-11.733% of base) : System.Private.CoreLib.dasm - ComEventsMethod:RemoveDelegate(Delegate,bool):this
         -39 (-7.276% of base) : System.Configuration.ConfigurationManager.dasm - ConfigurationElement:SetLocked():this
         -39 (-65.000% of base) : System.Security.Permissions.dasm - PrincipalPermission:VerifyType(IPermission):bool:this
         -36 (-63.158% of base) : System.Private.CoreLib.dasm - EnumComparer`1:Equals(Object):bool:this
         -36 (-63.158% of base) : System.Private.CoreLib.dasm - ByteEqualityComparer:Equals(Object):bool:this
         -36 (-63.158% of base) : System.Private.CoreLib.dasm - EnumEqualityComparer`1:Equals(Object):bool:this
         -34 (-3.469% of base) : System.Private.CoreLib.dasm - Array:Copy(Array,int,Array,int,int,bool)
         -30 (-6.928% of base) : Microsoft.CSharp.dasm - ComEventsMethod:AddDelegate(Delegate,bool):this
         -30 (-38.462% of base) : Microsoft.VisualBasic.Core.dasm - FastList:DataIsEqual(Object,Object):bool:this
         -27 (-28.723% of base) : Microsoft.CodeAnalysis.dasm - XmlFileResolver:Equals(Object):bool:this
         -27 (-31.395% of base) : Microsoft.CodeAnalysis.dasm - SourceFileResolver:Equals(Object):bool:this
         -27 (-31.395% of base) : Microsoft.Diagnostics.NETCore.Client.dasm - EventPipeProvider:Equals(Object):bool:this
         -24 (-21.818% of base) : Microsoft.CodeAnalysis.dasm - DesktopStrongNameProvider:Equals(Object):bool:this
         -23 (-10.798% of base) : System.Data.Common.dasm - ObjectStorage:CompareValueTo(int,Object):int:this
Top method improvements (percentages):
         -39 (-65.000% of base) : System.Security.Permissions.dasm - PrincipalPermission:VerifyType(IPermission):bool:this
         -36 (-63.158% of base) : System.Private.CoreLib.dasm - EnumComparer`1:Equals(Object):bool:this
         -36 (-63.158% of base) : System.Private.CoreLib.dasm - ByteEqualityComparer:Equals(Object):bool:this
         -36 (-63.158% of base) : System.Private.CoreLib.dasm - EnumEqualityComparer`1:Equals(Object):bool:this
         -72 (-63.158% of base) : System.Private.CoreLib.dasm - NullableEqualityComparer`1:Equals(Object):bool:this (2 methods)
         -72 (-63.158% of base) : System.Private.CoreLib.dasm - NullableComparer`1:Equals(Object):bool:this (2 methods)
        -108 (-63.158% of base) : System.Private.CoreLib.dasm - ObjectComparer`1:Equals(Object):bool:this (3 methods)
        -216 (-63.158% of base) : System.Private.CoreLib.dasm - ObjectEqualityComparer`1:Equals(Object):bool:this (6 methods)
       -1044 (-63.158% of base) : System.Private.CoreLib.dasm - GenericComparer`1:Equals(Object):bool:this (29 methods)
         -30 (-38.462% of base) : Microsoft.VisualBasic.Core.dasm - FastList:DataIsEqual(Object,Object):bool:this
         -27 (-31.395% of base) : Microsoft.CodeAnalysis.dasm - SourceFileResolver:Equals(Object):bool:this
         -27 (-31.395% of base) : Microsoft.Diagnostics.NETCore.Client.dasm - EventPipeProvider:Equals(Object):bool:this
         -27 (-28.723% of base) : Microsoft.CodeAnalysis.dasm - XmlFileResolver:Equals(Object):bool:this
         -24 (-21.818% of base) : Microsoft.CodeAnalysis.dasm - DesktopStrongNameProvider:Equals(Object):bool:this
         -44 (-11.828% of base) : Microsoft.CSharp.dasm - ComEventsMethod:RemoveDelegate(Delegate,bool):this
         -44 (-11.733% of base) : System.Private.CoreLib.dasm - ComEventsMethod:RemoveDelegate(Delegate,bool):this
         -23 (-10.798% of base) : System.Data.Common.dasm - ObjectStorage:CompareValueTo(int,Object):int:this
         -39 (-7.276% of base) : System.Configuration.ConfigurationManager.dasm - ConfigurationElement:SetLocked():this
         -30 (-6.928% of base) : Microsoft.CSharp.dasm - ComEventsMethod:AddDelegate(Delegate,bool):this
         -23 (-5.569% of base) : System.Private.CoreLib.dasm - ComEventsMethod:AddDelegate(Delegate,bool):this
38 total methods with Code Size differences (38 improved, 0 regressed), 186728 unchanged.

@jkotas jkotas merged commit f05ebc8 into dotnet:master Jun 15, 2020
@jkotas jkotas deleted the enum-compareto branch June 15, 2020 21:09
@DrewScoggins
Copy link
Member

DrewScoggins commented Jul 21, 2020

Moved this issue to the runtime repo for posterity: #39740

@Saibamen
Copy link

Do you have benchmark times before and after this PR?

@jkotas
Copy link
Member Author

jkotas commented Jul 21, 2020

Sorry, I forgot the paste the throughput numbers into the PR. It was within noise range (<5%) of the baseline. @DrewScoggins Are there official perf numbers with this fix available?

@DrewScoggins
Copy link
Member

You can see the improvement after @jkotas checkin below.

image

@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Perf -31%] PerfLabTests.EnumPerf (2)
5 participants