Skip to content

Optimize Half.Equals #41329

Closed
Closed

Description

@jkotas

See #41296 (comment) for context.

We should add this test case too: Assert.True(Half.NaN != Half.NaN);

Activity

  1. Dotnet-GitSync-Bot commented on Aug 25, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

  2. added
    untriaged
    on Aug 25, 2020
  3. jkotas commented on Aug 25, 2020

    @jkotas
    MemberAuthor

    @huoyaoyuan Are you interested in working on this?

  4. huoyaoyuan commented on Aug 25, 2020

    @huoyaoyuan
    Member

    I do have a quick idea about it.
    Does it require an entry in performance repo?
    Or, just let me practice to follow the workflow of comparing performance between branches?

  5. jkotas commented on Aug 25, 2020

    @jkotas
    MemberAuthor

    Does it require an entry in performance repo?

    Submitting performance tests to performance repo is encouraged, but not required.

  6. huoyaoyuan commented on Aug 29, 2020

    @huoyaoyuan
    Member

    @jkotas question: what's the correct way to run benchmark against locally built clr and corelib? I cannot find a documentation that's confidentially up-to-date.

    Here's my steps:

    1. Create a BenchmarkDotNet project. Half is included in latest shipped preview SDK, so no additional step is required here.
    2. Modify implementation
    3. build.cmd -c release -rc release
    4. dotnet run -c Release --corerun D:\Code\dotnet\runtime\artifacts\bin\testhost\net5.0-Windows_NT-Release-x64\shared\Microsoft.NETCore.App\6.0.0\CoreRun.exe

    But I'm afraid I've got something wrong:

    • The benchmark result is very close after implementation change.
    • After adding [DisassemblyDiagnoser], the asm looks like master branch, instead of changed one (contains call System.Half.op_Equality that's removed)
  7. huoyaoyuan commented on Aug 29, 2020

    @huoyaoyuan
    Member

    It seems to be influenced by step3:

    • If I do a clean build by deleting the whole artifact folder and build.cmd -c release -rc release, the benchmark will run against latest bits.
    • If I just delete the testhost folder and build.cmd clr.corelib+libs+libs.pretest -c Release -rc Release, the benchmark will run against last clean build.
  8. jkotas commented on Aug 29, 2020

    @jkotas
    MemberAuthor

    @adamsitnik What is the best way to run performance tests on CoreLib updates?

    Also, I have noticed that the local instructions we have for running performance tests are outdated: https://github.com/dotnet/runtime/blob/master/docs/project/performance-guidelines.md#running-the-coreclr-performance-tests-on-windows

  9. huoyaoyuan commented on Aug 29, 2020

    @huoyaoyuan
    Member

    Here are benchmark and runs:

    MaxValue() => MaxValue.Equals(MaxValue)
    Zeros() => PositiveZero.Equals(NegativeZero)
    Nan() => NegativeQNaN.Equals(NegativeQNaN)
    NaNDifferentBits() => NegativeQNaN.Equals(PositiveQNaN)
    

    Runs on master:

    Method Mean Error StdDev
    MaxValue 1.941 ns 0.0151 ns 0.0134 ns
    Zeros 1.857 ns 0.0196 ns 0.0193 ns
    NaN 2.150 ns 0.0167 ns 0.0148 ns
    NaNDifferentBits 1.917 ns 0.0109 ns 0.0097 ns

    Change1:

    Method Mean Error StdDev
    MaxValue 0.8438 ns 0.0067 ns 0.0062 ns
    Zeros 1.0809 ns 0.0055 ns 0.0048 ns
    NaN 0.8593 ns 0.0109 ns 0.0102 ns
    NaNDifferentBits 0.9401 ns 0.0072 ns 0.0064 ns
    ZeroAndNaN 1.1246 ns 0.0102 ns 0.0095 ns

    Change2:

    Method Mean Error StdDev
    MaxValue 0.3995 ns 0.0106 ns 0.0099 ns
    Zeros 0.6956 ns 0.0063 ns 0.0055 ns
    NaN 0.4726 ns 0.0280 ns 0.0262 ns
    NaNDifferentBits 1.1515 ns 0.0081 ns 0.0076 ns
    ZeroAndNaN 1.3466 ns 0.0140 ns 0.0117 ns

    Change1 is:

            public bool Equals(Half other)
            {
                return _value == other._value
                    || AreZero(this, other)
                    || (IsNaN(this) && IsNaN(other));
            }

    Change2 is:

            public bool Equals(Half other)
            {
                if (_value == other._value) return true;
                if (AreZero(this, other)) return true;
                return IsNaN(this) && IsNaN(other);
            }

    Change 2 is faster for bitwise equals scenarios, but a bit slower for unequals.
    And also, there should be measurable error for change2, because the MaxValue and NaN tests are both bitwise equals scenario, but there measured performance differs.

    Do you have a preference between equality performance and inequality performance?

    Base on the pattern used in operator ==, I will choose change2 for PR.

  10. jkotas commented on Aug 29, 2020

    @jkotas
    MemberAuthor

    MaxValue() => MaxValue.Equals(MaxValue)

    Using constants like this for benchmarking may not be representative. If the JIT was perfect, it would optimize this into just returning true. You may want to double check that optimizations like this are not affecting your measurements. It is better to use arguments passed into the benchmarking method to avoid optimizations skewing the numbers.

    We generally prefer the style in change1.

    The performance difference between these two variants may not be be durable. Both variants can lead to equally well performing code if the JIT was perfect.

  11. huoyaoyuan commented on Aug 29, 2020

    @huoyaoyuan
    Member

    A full benchmark

    Method Job Toolchain left right Mean Error StdDev Ratio RatioSD Code Size
    Equal Job-YIWPNM \V1-Inline\CoreRun.exe 0 -0 0.4526 ns 0.0127 ns 0.0112 ns 1.00 0.00 75 B
    Equal Job-FELNJL \V2-Inline\CoreRun.exe 0 -0 0.4420 ns 0.0069 ns 0.0064 ns 0.98 0.03 82 B
    Equal Job-QGOSKR \V3-Inline\CoreRun.exe 0 -0 0.4560 ns 0.0092 ns 0.0082 ns 1.01 0.04 82 B
    Equal Job-ULCNJF \V3-NoInline\CoreRun.exe 0 -0 1.4427 ns 0.0022 ns 0.0020 ns 3.19 0.08 107 B
    Equal Job-QLNFVM \master-Inline\CoreRun.exe 0 -0 1.8886 ns 0.0142 ns 0.0133 ns 4.17 0.11 156 B
    Equal Job-LVVTAN \master\CoreRun.exe 0 -0 1.8901 ns 0.0135 ns 0.0126 ns 4.18 0.12 156 B
    Equal Job-YIWPNM \V1-Inline\CoreRun.exe 65500 -65500 0.6604 ns 0.0088 ns 0.0082 ns 1.00 0.00 75 B
    Equal Job-FELNJL \V2-Inline\CoreRun.exe 65500 -65500 0.8505 ns 0.0151 ns 0.0134 ns 1.29 0.03 82 B
    Equal Job-QGOSKR \V3-Inline\CoreRun.exe 65500 -65500 0.6626 ns 0.0115 ns 0.0108 ns 1.00 0.02 82 B
    Equal Job-ULCNJF \V3-NoInline\CoreRun.exe 65500 -65500 1.8565 ns 0.0117 ns 0.0109 ns 2.81 0.04 107 B
    Equal Job-QLNFVM \master-Inline\CoreRun.exe 65500 -65500 1.6922 ns 0.0067 ns 0.0059 ns 2.57 0.04 156 B
    Equal Job-LVVTAN \master\CoreRun.exe 65500 -65500 1.6911 ns 0.0131 ns 0.0116 ns 2.56 0.03 156 B
    Equal Job-YIWPNM \V1-Inline\CoreRun.exe 65500 0 0.6683 ns 0.0042 ns 0.0038 ns 1.00 0.00 75 B
    Equal Job-FELNJL \V2-Inline\CoreRun.exe 65500 0 0.8500 ns 0.0120 ns 0.0112 ns 1.27 0.02 82 B
    Equal Job-QGOSKR \V3-Inline\CoreRun.exe 65500 0 0.6608 ns 0.0099 ns 0.0092 ns 0.99 0.01 82 B
    Equal Job-ULCNJF \V3-NoInline\CoreRun.exe 65500 0 1.8666 ns 0.0108 ns 0.0096 ns 2.79 0.02 107 B
    Equal Job-QLNFVM \master-Inline\CoreRun.exe 65500 0 1.6872 ns 0.0115 ns 0.0107 ns 2.53 0.02 156 B
    Equal Job-LVVTAN \master\CoreRun.exe 65500 0 1.6832 ns 0.0076 ns 0.0068 ns 2.52 0.02 156 B
    Equal Job-YIWPNM \V1-Inline\CoreRun.exe 65500 65500 0.2071 ns 0.0095 ns 0.0089 ns 1.00 0.00 75 B
    Equal Job-FELNJL \V2-Inline\CoreRun.exe 65500 65500 0.2199 ns 0.0057 ns 0.0053 ns 1.06 0.06 82 B
    Equal Job-QGOSKR \V3-Inline\CoreRun.exe 65500 65500 0.2096 ns 0.0024 ns 0.0022 ns 1.01 0.05 82 B
    Equal Job-ULCNJF \V3-NoInline\CoreRun.exe 65500 65500 0.9801 ns 0.0015 ns 0.0013 ns 4.74 0.21 107 B
    Equal Job-QLNFVM \master-Inline\CoreRun.exe 65500 65500 2.3541 ns 0.0407 ns 0.0361 ns 11.38 0.54 156 B
    Equal Job-LVVTAN \master\CoreRun.exe 65500 65500 2.2191 ns 0.0173 ns 0.0162 ns 10.73 0.43 156 B
    Equal Job-YIWPNM \V1-Inline\CoreRun.exe 65500 NaN 0.6647 ns 0.0081 ns 0.0076 ns 1.00 0.00 75 B
    Equal Job-FELNJL \V2-Inline\CoreRun.exe 65500 NaN 0.8462 ns 0.0106 ns 0.0099 ns 1.27 0.02 82 B
    Equal Job-QGOSKR \V3-Inline\CoreRun.exe 65500 NaN 0.6605 ns 0.0084 ns 0.0078 ns 0.99 0.02 82 B
    Equal Job-ULCNJF \V3-NoInline\CoreRun.exe 65500 NaN 1.8542 ns 0.0046 ns 0.0043 ns 2.79 0.03 107 B
    Equal Job-QLNFVM \master-Inline\CoreRun.exe 65500 NaN 1.4733 ns 0.0052 ns 0.0049 ns 2.22 0.02 156 B
    Equal Job-LVVTAN \master\CoreRun.exe 65500 NaN 1.4638 ns 0.0015 ns 0.0014 ns 2.20 0.02 156 B
    Equal Job-YIWPNM \V1-Inline\CoreRun.exe NaN 0 0.4603 ns 0.0009 ns 0.0008 ns 1.00 0.00 75 B
    Equal Job-FELNJL \V2-Inline\CoreRun.exe NaN 0 0.8589 ns 0.0015 ns 0.0014 ns 1.87 0.00 82 B
    Equal Job-QGOSKR \V3-Inline\CoreRun.exe NaN 0 0.6514 ns 0.0015 ns 0.0013 ns 1.42 0.00 82 B
    Equal Job-ULCNJF \V3-NoInline\CoreRun.exe NaN 0 1.2361 ns 0.0022 ns 0.0021 ns 2.69 0.01 107 B
    Equal Job-QLNFVM \master-Inline\CoreRun.exe NaN 0 2.4197 ns 0.0034 ns 0.0030 ns 5.26 0.01 156 B
    Equal Job-LVVTAN \master\CoreRun.exe NaN 0 2.4231 ns 0.0039 ns 0.0036 ns 5.26 0.01 156 B
    Equal Job-YIWPNM \V1-Inline\CoreRun.exe NaN NaN 0.2098 ns 0.0017 ns 0.0016 ns 1.00 0.00 75 B
    Equal Job-YIWPNM \V1-Inline\CoreRun.exe NaN NaN 0.4320 ns 0.0041 ns 0.0038 ns 2.06 0.02 75 B
    Equal Job-FELNJL \V2-Inline\CoreRun.exe NaN NaN 0.2186 ns 0.0024 ns 0.0023 ns 1.04 0.01 82 B
    Equal Job-FELNJL \V2-Inline\CoreRun.exe NaN NaN 0.8602 ns 0.0053 ns 0.0047 ns 4.10 0.03 82 B
    Equal Job-QGOSKR \V3-Inline\CoreRun.exe NaN NaN 0.2264 ns 0.0228 ns 0.0297 ns 1.11 0.17 82 B
    Equal Job-QGOSKR \V3-Inline\CoreRun.exe NaN NaN 0.6599 ns 0.0029 ns 0.0028 ns 3.15 0.02 82 B
    Equal Job-ULCNJF \V3-NoInline\CoreRun.exe NaN NaN 1.2409 ns 0.0064 ns 0.0060 ns 5.92 0.06 107 B
    Equal Job-ULCNJF \V3-NoInline\CoreRun.exe NaN NaN 1.0072 ns 0.0031 ns 0.0028 ns 4.80 0.04 107 B
    Equal Job-QLNFVM \master-Inline\CoreRun.exe NaN NaN 2.4152 ns 0.0141 ns 0.0132 ns 11.51 0.09 156 B
    Equal Job-QLNFVM \master-Inline\CoreRun.exe NaN NaN 2.4151 ns 0.0255 ns 0.0226 ns 11.52 0.16 156 B
    Equal Job-LVVTAN \master\CoreRun.exe NaN NaN 2.4281 ns 0.0078 ns 0.0069 ns 11.58 0.09 156 B
    Equal Job-LVVTAN \master\CoreRun.exe NaN NaN 2.0738 ns 0.0086 ns 0.0076 ns 9.89 0.09 156 B

    Code:

        public unsafe class Program
        {
            static void Main(string[] args)
            {
                BenchmarkSwitcher
                    .FromAssembly(typeof(Program).Assembly)
                    .Run(args);
            }
    
            private static Half GetPositiveQNan()
            {
                ushort PositiveQNaNBits = 0x7E00;
                return *(Half*)&PositiveQNaNBits;
            }
    
            public IEnumerable<object[]> ValueSource
            {
                get
                {
                    // Equality
                    yield return new object[] { Half.MaxValue, Half.MaxValue };
                    yield return new object[] { (Half)0.0, (Half)(-0.0) };
                    yield return new object[] { Half.NaN, Half.NaN };
                    yield return new object[] { Half.NaN, GetPositiveQNan() };
    
                    // Inequality
                    yield return new object[] { Half.MaxValue, Half.MinValue };
                    yield return new object[] { Half.MaxValue, (Half)0.0 };
                    yield return new object[] { Half.NaN, (Half)0.0 };
                    yield return new object[] { Half.MaxValue, Half.NaN };
                }
            }
    
            [Benchmark]
            [ArgumentsSource(nameof(ValueSource))]
            public bool Equal(Half left, Half right) => left.Equals(right);
        }

    Implementations:

    V1:

            public bool Equals(Half other)
            {
                return _value == other._value
                    || AreZero(this, other)
                    || (IsNaN(this) && IsNaN(other));
            }

    V2:

            public bool Equals(Half other)
            {
                if (_value == other._value) return true;
                if (AreZero(this, other)) return true;
                return IsNaN(this) && IsNaN(other);
            }

    V3:

            public bool Equals(Half other)
            {
                if (_value == other._value)
                {
                    return true;
                }
    
                return AreZero(this, other)
                    || (IsNaN(this) && IsNaN(other));
            }

    Inline:

    Adding [MethodImpl(MethodImplOptions.AggressiveInlining)]

  12. huoyaoyuan commented on Aug 29, 2020

    @huoyaoyuan
    Member

    Test instructions I've used:

    • Do a full release build, and copy the testhost folder for multiple times.
    • Change the implementation
    • Build only corelib by Build.cmd clr.corelib -c release. This is quite fast.
    • Copy System.Private.CoreLib.dll from artifacts\bin\coreclr\Windows_NT.x64.Release\IL, hinted by console output.
    • Run benchmark against every folder by dotnet run -c Release --corerun change1\CoreRun.exe change2\CoreRun.exe ...

    BenchmarkDotNet is unable to distinguish to different NaNs. Others are just finely grouped.

  13. huoyaoyuan commented on Aug 29, 2020

    @huoyaoyuan
    Member

    Using constants like this for benchmarking may not be representative. If the JIT was perfect, it would optimize this into just returning true. You may want to double check that optimizations like this are not affecting your measurements. It is better to use arguments passed into the benchmarking method to avoid optimizations skewing the numbers.

    We generally prefer the style in change1.

    After changing to ArgumentsSource, style 1 is getting better performance. PR opened with style 1.

    The open question is about usage of AggressiveInlining.

  14. adamsitnik commented on Aug 31, 2020

    @adamsitnik
    Member

    What is the best way to run performance tests on CoreLib updates?

    https://github.com/dotnet/performance/blob/master/docs/benchmarking-workflow-dotnet-runtime.md#Preventing-Regressions

    Also, I have noticed that the local instructions we have for running performance tests are outdated

    Thanks for pointing this out, I've sent a PR with an update: #41583

  15. removed
    untriaged
    on Sep 14, 2020
  16. ghost locked as resolved and limited conversation to collaborators on Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions