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

When calculating xxhash, net7.0 is slower than net6.0 #90090

Closed
Cricle opened this issue Aug 7, 2023 · 5 comments · Fixed by #90142
Closed

When calculating xxhash, net7.0 is slower than net6.0 #90090

Cricle opened this issue Aug 7, 2023 · 5 comments · Fixed by #90142
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Milestone

Comments

@Cricle
Copy link

Cricle commented Aug 7, 2023

Description

    internal unsafe class Program
    {
        public static void Main(string[] args)
        {
            var buffer = Encoding.UTF8.GetBytes("aadwejkadjgb8c27tr874c3/./[}|P{OP&^&$%^^TGERfgea");
            var sw = Stopwatch.GetTimestamp();
            for (int i = 0; i < 1_000_000; i++)
            {
                XxHash64.Hash(buffer);
            }
            Console.WriteLine(new TimeSpan(Stopwatch.GetTimestamp() - sw));

        }
    }

The csproj

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
	</PropertyGroup>

	<ItemGroup>
	  <PackageReference Include="System.IO.Hashing" Version="7.0.0" />
	</ItemGroup>

</Project>
dotnet run -c Release -f net6.0 

Result 00:00:00.1590156

dotnet run -c Release -f net7.0

Result 00:00:00.2138358

Configuration

OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win10-x64

Regression?

I don't know.

Data

None.

@Cricle Cricle added the tenet-performance Performance related issue label Aug 7, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Aug 7, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 7, 2023
@ghost
Copy link

ghost commented Aug 7, 2023

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

Issue Details

Description

    internal unsafe class Program
    {
        public static void Main(string[] args)
        {
            var buffer = Encoding.UTF8.GetBytes("aadwejkadjgb8c27tr874c3/./[}|P{OP&^&$%^^TGERfgea");
            var sw = Stopwatch.GetTimestamp();
            for (int i = 0; i < 1_000_000; i++)
            {
                XxHash64.Hash(buffer);
            }
            Console.WriteLine(new TimeSpan(Stopwatch.GetTimestamp() - sw));

        }
    }

The csproj

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
	</PropertyGroup>

	<ItemGroup>
	  <PackageReference Include="System.IO.Hashing" Version="7.0.0" />
	</ItemGroup>

</Project>
dotnet run -c Release -f net6.0 

Result 00:00:00.1590156

dotnet run -c Release -f net7.0

Result 00:00:00.2138358

Configuration

OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win10-x64

Regression?

I don't know.

Data

None.

Author: Cricle
Assignees: -
Labels:

tenet-performance, area-CodeGen-coreclr, untriaged

Milestone: -

@KeterSCP
Copy link

KeterSCP commented Aug 7, 2023

The difference between net6.0 and net7.0 is not that big, but net8.0 shows 2x+ regression:

Benchmark code
using System.IO.Hashing;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;

BenchmarkRunner.Run<Benchs>();

[SimpleJob(RuntimeMoniker.Net60)]
[SimpleJob(RuntimeMoniker.Net70)]
[SimpleJob(RuntimeMoniker.Net80)]
public class Benchs
{
    private readonly byte[] buffer = Encoding.UTF8.GetBytes("aadwejkadjgb8c27tr874c3/./[}|P{OP&^&$%^^TGERfgea");

    [Benchmark]
    public byte[] BenchXxhash()
    {
        return XxHash64.Hash(buffer);
    }
}
BenchmarkDotNet v0.13.7, Windows 11 (10.0.22621.1992/22H2/2022Update/SunValley2)
AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
.NET SDK 8.0.100-preview.6.23330.14
  [Host]   : .NET 7.0.9 (7.0.923.32018), X64 RyuJIT AVX2
  .NET 6.0 : .NET 6.0.20 (6.0.2023.32017), X64 RyuJIT AVX2
  .NET 7.0 : .NET 7.0.9 (7.0.923.32018), X64 RyuJIT AVX2
  .NET 8.0 : .NET 8.0.0 (8.0.23.32907), X64 RyuJIT AVX2


Method Job Runtime Mean Error StdDev
BenchXxhash .NET 6.0 .NET 6.0 14.09 ns 0.060 ns 0.056 ns
BenchXxhash .NET 7.0 .NET 7.0 15.54 ns 0.062 ns 0.055 ns
BenchXxhash .NET 8.0 .NET 8.0 36.63 ns 0.090 ns 0.084 ns

@EgorBo
Copy link
Member

EgorBo commented Aug 7, 2023

I bet it's #87113 (comment)

@EgorBo
Copy link
Member

EgorBo commented Aug 7, 2023

Or it can be PGO actually... cc @AndyAyersMS

using System.Diagnostics;
using System.IO.Hashing;

internal class Program
{
    public static void Main()
    {
        byte[] buffer = "aadwejkadjgb8c27tr874c3/./[}|P{OP&^&$%^^TGERfgea"u8.ToArray();
        Stopwatch sw = Stopwatch.StartNew();
        while (true)
        {
            sw.Restart();
            for (int i = 0; i < 1_000_000; i++)
                XxHash64.Hash(buffer);
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
        }
    }
}

smells like the inliner budget problem - i see BinaryPrimitives.Read* apis non-inlined on hot path

@EgorBo
Copy link
Member

EgorBo commented Aug 7, 2023

public static void Main()
{
    byte[] buffer = "aadwejkadjgb8c27tr874c3/./[}|P{OP&^&$%^^TGERfgea"u8.ToArray();
    for (int i = 0; i < 1_000_000; i++)
        XxHash64.Hash(buffer);
}
Inlines into Program:Main():
  [INLINED: aggressive inline attribute] System.ReadOnlySpan`1[ubyte]:.ctor(ulong,int):this
    [INLINED: below ALWAYS_INLINE size] System.Runtime.CompilerServices.RuntimeHelpers:IsReferenceOrContainsReferences[ubyte]():bool
    [FAILED: unprofitable inline] System.ThrowHelper:ThrowInvalidTypeWithPointersNotSupported(System.Type)
  [FAILED: unprofitable inline] System.ReadOnlySpan`1[ubyte]:ToArray():ubyte[]:this
  [INLINED: profitable inline] System.IO.Hashing.XxHash64:Hash(ubyte[]):ubyte[]
    [FAILED: too many il bytes] System.ArgumentNullException:.ctor(System.String):this
    [INLINED: aggressive inline attribute] System.ReadOnlySpan`1[ubyte]:.ctor(ubyte[]):this
    [INLINED: profitable inline] System.IO.Hashing.XxHash64:Hash(System.ReadOnlySpan`1[ubyte],long):ubyte[]
      [INLINED: profitable inline] System.IO.Hashing.XxHash64:HashToUInt64(System.ReadOnlySpan`1[ubyte],long):ulong
        [INLINED: profitable inline] System.IO.Hashing.XxHash64+State:.ctor(ulong):this
        [INLINED: profitable inline] System.IO.Hashing.XxHash64+State:ProcessStripe(System.ReadOnlySpan`1[ubyte]):this
          [INLINED: aggressive inline attribute] System.ReadOnlySpan`1[ubyte]:Slice(int,int):System.ReadOnlySpan`1[ubyte]:this
            [FAILED: does not return] System.ThrowHelper:ThrowArgumentOutOfRangeException()
            [INLINED: aggressive inline attribute] System.ReadOnlySpan`1[ubyte]:.ctor(byref,int):this
              [INLINED: profitable inline] System.Diagnostics.Debug:Assert(bool)
                [INLINED: below ALWAYS_INLINE size] System.Diagnostics.Debug:Assert(bool,System.String,System.String)
          [INLINED: below ALWAYS_INLINE size] System.IO.Hashing.XxHash64+State:ApplyRound(ulong,System.ReadOnlySpan`1[ubyte]):ulong
            [INLINED: aggressive inline attribute] System.Buffers.Binary.BinaryPrimitives:ReadUInt64LittleEndian(System.ReadOnlySpan`1[ubyte]):ulong
              [INLINED: aggressive inline attribute] System.Runtime.InteropServices.MemoryMarshal:Read[ulong](System.ReadOnlySpan`1[ubyte]):ulong
                [INLINED: below ALWAYS_INLINE size] System.Runtime.CompilerServices.RuntimeHelpers:IsReferenceOrContainsReferences[ulong]():bool
                [FAILED: unprofitable inline] System.ThrowHelper:ThrowInvalidTypeWithPointersNotSupported(System.Type)
                [FAILED: does not return] System.ThrowHelper:ThrowArgumentOutOfRangeException(int)
                [INLINED: below ALWAYS_INLINE size] System.Runtime.InteropServices.MemoryMarshal:GetReference[ubyte](System.ReadOnlySpan`1[ubyte]):byref
            [INLINED: profitable inline] System.IO.Hashing.XxHash64+State:ApplyRound(ulong,ulong):ulong
          [INLINED: aggressive inline attribute] System.ReadOnlySpan`1[ubyte]:Slice(int):System.ReadOnlySpan`1[ubyte]:this
            [FAILED: does not return] System.ThrowHelper:ThrowArgumentOutOfRangeException()
            [INLINED: aggressive inline attribute] System.ReadOnlySpan`1[ubyte]:.ctor(byref,int):this
              [INLINED: profitable inline] System.Diagnostics.Debug:Assert(bool)
                [INLINED: below ALWAYS_INLINE size] System.Diagnostics.Debug:Assert(bool,System.String,System.String)
                  [FAILED: noinline per IL/cached result] System.Diagnostics.Debug:Fail(System.String,System.String)
          [INLINED: below ALWAYS_INLINE size] System.IO.Hashing.XxHash64+State:ApplyRound(ulong,System.ReadOnlySpan`1[ubyte]):ulong
            [INLINED: aggressive inline attribute] System.Buffers.Binary.BinaryPrimitives:ReadUInt64LittleEndian(System.ReadOnlySpan`1[ubyte]):ulong
              [INLINED: aggressive inline attribute] System.Runtime.InteropServices.MemoryMarshal:Read[ulong](System.ReadOnlySpan`1[ubyte]):ulong
                [INLINED: below ALWAYS_INLINE size] System.Runtime.CompilerServices.RuntimeHelpers:IsReferenceOrContainsReferences[ulong]():bool
                [FAILED: unprofitable inline] System.ThrowHelper:ThrowInvalidTypeWithPointersNotSupported(System.Type)
                [FAILED: does not return] System.ThrowHelper:ThrowArgumentOutOfRangeException(int)
                [INLINED: below ALWAYS_INLINE size] System.Runtime.InteropServices.MemoryMarshal:GetReference[ubyte](System.ReadOnlySpan`1[ubyte]):byref
            [INLINED: profitable inline] System.IO.Hashing.XxHash64+State:ApplyRound(ulong,ulong):ulong
          [INLINED: aggressive inline attribute] System.ReadOnlySpan`1[ubyte]:Slice(int):System.ReadOnlySpan`1[ubyte]:this
            [FAILED: does not return] System.ThrowHelper:ThrowArgumentOutOfRangeException()
            [INLINED: aggressive inline attribute] System.ReadOnlySpan`1[ubyte]:.ctor(byref,int):this
              [INLINED: profitable inline] System.Diagnostics.Debug:Assert(bool)
                [INLINED: below ALWAYS_INLINE size] System.Diagnostics.Debug:Assert(bool,System.String,System.String)
                  [FAILED: noinline per IL/cached result] System.Diagnostics.Debug:Fail(System.String,System.String)
          [INLINED: below ALWAYS_INLINE size] System.IO.Hashing.XxHash64+State:ApplyRound(ulong,System.ReadOnlySpan`1[ubyte]):ulong
            [INLINED: aggressive inline attribute] System.Buffers.Binary.BinaryPrimitives:ReadUInt64LittleEndian(System.ReadOnlySpan`1[ubyte]):ulong
              [INLINED: aggressive inline attribute] System.Runtime.InteropServices.MemoryMarshal:Read[ulong](System.ReadOnlySpan`1[ubyte]):ulong
                [INLINED: below ALWAYS_INLINE size] System.Runtime.CompilerServices.RuntimeHelpers:IsReferenceOrContainsReferences[ulong]():bool
                [FAILED: unprofitable inline] System.ThrowHelper:ThrowInvalidTypeWithPointersNotSupported(System.Type)
                [FAILED: does not return] System.ThrowHelper:ThrowArgumentOutOfRangeException(int)
                [INLINED: below ALWAYS_INLINE size] System.Runtime.InteropServices.MemoryMarshal:GetReference[ubyte](System.ReadOnlySpan`1[ubyte]):byref
            [INLINED: profitable inline] System.IO.Hashing.XxHash64+State:ApplyRound(ulong,ulong):ulong
          [INLINED: aggressive inline attribute] System.ReadOnlySpan`1[ubyte]:Slice(int):System.ReadOnlySpan`1[ubyte]:this
            [FAILED: does not return] System.ThrowHelper:ThrowArgumentOutOfRangeException()
            [INLINED: aggressive inline attribute] System.ReadOnlySpan`1[ubyte]:.ctor(byref,int):this
              [INLINED: profitable inline] System.Diagnostics.Debug:Assert(bool)
                [INLINED: below ALWAYS_INLINE size] System.Diagnostics.Debug:Assert(bool,System.String,System.String)
                  [FAILED: noinline per IL/cached result] System.Diagnostics.Debug:Fail(System.String,System.String)
          [INLINED: below ALWAYS_INLINE size] System.IO.Hashing.XxHash64+State:ApplyRound(ulong,System.ReadOnlySpan`1[ubyte]):ulong
            [INLINED: aggressive inline attribute] System.Buffers.Binary.BinaryPrimitives:ReadUInt64LittleEndian(System.ReadOnlySpan`1[ubyte]):ulong
              [INLINED: aggressive inline attribute] System.Runtime.InteropServices.MemoryMarshal:Read[ulong](System.ReadOnlySpan`1[ubyte]):ulong
                [INLINED: below ALWAYS_INLINE size] System.Runtime.CompilerServices.RuntimeHelpers:IsReferenceOrContainsReferences[ulong]():bool
                [FAILED: unprofitable inline] System.ThrowHelper:ThrowInvalidTypeWithPointersNotSupported(System.Type)
                [FAILED: does not return] System.ThrowHelper:ThrowArgumentOutOfRangeException(int)
                [INLINED: below ALWAYS_INLINE size] System.Runtime.InteropServices.MemoryMarshal:GetReference[ubyte](System.ReadOnlySpan`1[ubyte]):byref
            [INLINED: profitable inline] System.IO.Hashing.XxHash64+State:ApplyRound(ulong,ulong):ulong
        [FAILED: inline exceeds budget] System.ReadOnlySpan`1[ubyte]:Slice(int):System.ReadOnlySpan`1[ubyte]:this
        [FAILED: inline exceeds budget] System.IO.Hashing.XxHash64+State:Complete(long,System.ReadOnlySpan`1[ubyte]):ulong:this
      [INLINED: below ALWAYS_INLINE size] System.Span`1[ubyte]:op_Implicit(ubyte[]):System.Span`1[ubyte]
        [FAILED: inline exceeds budget] System.Span`1[ubyte]:.ctor(ubyte[]):this
      [FAILED: inline exceeds budget] System.Buffers.Binary.BinaryPrimitives:WriteUInt64BigEndian(System.Span`1[ubyte],ulong)

Note a few FAILED: inline exceeds budget

@EgorBo EgorBo added this to the 8.0.0 milestone Aug 7, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Aug 7, 2023
@EgorBo EgorBo self-assigned this Aug 7, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 8, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 8, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 7, 2023
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 a pull request may close this issue.

3 participants