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

Added Benchmark for SpanHelpers.Contains {byte, char} #2347

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/benchmarks/micro/MicroBenchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' Or ('$(TargetFrameworkIdentifier)' == '.NETCoreApp' And '$(_TargetFrameworkVersionWithoutV)' &lt; '6.0')">
<Compile Remove="libraries\System.Collections\PriorityQueue\Perf_PriorityQueue.cs" />
<Compile Remove="libraries\Common\AlignedMemory.cs" />
<Compile Remove="libraries\System.Memory\SpanHelpers.cs" />
<Compile Remove="libraries\System.IO.FileSystem\Perf.RandomAccess.cs" />
<Compile Remove="libraries\System.IO.FileSystem\Perf.RandomAccess.NoBuffering.cs" />
</ItemGroup>
Expand Down
81 changes: 81 additions & 0 deletions src/benchmarks/micro/libraries/System.Memory/SpanHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;

namespace System.Memory
{
[GenericTypeArguments(typeof(byte))]
[GenericTypeArguments(typeof(char))]
[BenchmarkCategory(Categories.Runtime, Categories.Libraries, Categories.Span)]
[ShortRunJob]
public unsafe class SpanHelpers<T>
where T : unmanaged, IComparable<T>, IEquatable<T>
{
private T* _searchSpace;
private T _value;

[ParamsSource(nameof(LengthValues))]
public int Length { get; set; }

public static IEnumerable<object> LengthValues()
{
// The values for the length take into account the different cut-offs
// in the vectorized pathes.

if (typeof(T) == typeof(byte))
{
// Vectorization is done on 2 * Vector<byte>.Count => 64 byte elements
yield return 63; // one less the vectorization threshould
Copy link
Member Author

Choose a reason for hiding this comment

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

At the moment these values are based on Vector<T>.Count on x64 with AVX2 enabled.
So for e.g. ARM where Vector<byte>.Count is 16 (instead of 32 as on x64) the values should be halfed.

Via ParamsSource this could be taken into account. Shall I update to this?
(Didn't do now, because of the discussion in the PR's description -- i.e. I'd like to an outcome from this first).

Copy link
Member

Choose a reason for hiding this comment

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

@adamsitnik might suggest.

Copy link
Member

Choose a reason for hiding this comment

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

@adamsitnik could you advise, so we can get this merged?

yield return 64; // exactly two vectorized operations
yield return 65; // one element more than standard vectorized loop
yield return 95; // one element less than another iteration of the standard vectorized loop
yield return 100;
}
else if (typeof(T) == typeof(char))
{
// Vectorization is done on 2 * Vector<char>.Count => 32 char elements
// Values analogous to the byte values above
yield return 31;
yield return 32;
yield return 33;
yield return 47;
yield return 100;
}
else
{
throw new NotSupportedException();
}
}

[GlobalSetup]
public void Setup()
{
_searchSpace = (T*)NativeMemory.AlignedAlloc((uint)Length, 32);
Debug.Assert((nint)_searchSpace % Vector<T>.Count == 0);
Unsafe.InitBlock(_searchSpace, 0x00, (uint)Length);

_value = ValuesGenerator.GetNonDefaultValue<T>();
}

[GlobalCleanup]
public void Cleanup()
{
if (_searchSpace != null)
{
NativeMemory.AlignedFree(_searchSpace);
_searchSpace = null;
}
}

[Benchmark]
public bool Contains()
{
return new System.Span<T>(_searchSpace, Length).Contains(_value);
}
}
}