-
Notifications
You must be signed in to change notification settings - Fork 272
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
gfoidl
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
gfoidl:span_helpers_benchmark
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.
Open
Changes from all commits
Commits
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 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
81 changes: 81 additions & 0 deletions
81
src/benchmarks/micro/libraries/System.Memory/SpanHelpers.cs
This file contains 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,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 | ||
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); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamsitnik might suggest.
There was a problem hiding this comment.
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?