Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

[WIP] Add Sort(...) extension methods for Span<T> #26859

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
92702fe
add function signatures and minimal xml doc
nietras Dec 23, 2017
0c99da2
add helper stuff and scaffolding
nietras Dec 23, 2017
48f655d
copy code from ArraySortHelper in coreclr
nietras Dec 23, 2017
57eb72f
more porting work
nietras Dec 23, 2017
2b8b08f
compiles
nietras Dec 23, 2017
4272823
comparablespansorthelper
nietras Dec 23, 2017
25fc3c6
fix make generic sorter
nietras Dec 25, 2017
22d819b
fix porting bug
nietras Dec 25, 2017
ba1da6a
remove unnecessary swap checks, add reverse test
nietras Dec 29, 2017
ec77c64
fast middle via uint
nietras Dec 29, 2017
4f500a3
cleanup and a few comments
nietras Dec 29, 2017
4f2f822
optimize DownHeap
nietras Dec 29, 2017
758762b
add simple perf tests, with issues...
nietras Dec 29, 2017
fcb63b5
remove span sort perf for loop
nietras Dec 29, 2017
eff4ab4
show type specialization can make perf as good or better as array sort.
nietras Dec 30, 2017
0d99df1
make default sort helper static readonly
nietras Dec 30, 2017
03b1bef
expand perf tests
nietras Dec 30, 2017
84691a2
add new no sorting needed perf tests
nietras Dec 30, 2017
c8dff35
fix sort perf test
nietras Jan 1, 2018
5352890
Add implementation code from bb5c9f89 in DotNetCross.Sorting "span-so…
nietras Jan 28, 2018
c109f6a
Add KeysAndOrValues code from 4942ee74 in DotNetCross.Sorting "span-…
nietras Jan 29, 2018
48be72d
Update to a71460be with Sort tests including possible overflow test.
nietras Jan 29, 2018
7468485
Update to fdbb7bcf from DotNetCross/Sorting
nietras Feb 5, 2018
78aa18c
Add bogus comparer/comparable detection and exception throwing. Fix f…
nietras Feb 11, 2018
07b0105
Add TComparer, Comparison variants, remove bounds checks for TDirectC…
nietras Mar 13, 2018
04c5c0b
cleanup MemoryExtensions
nietras Mar 18, 2018
ced09bd
address ahsonkhan feedback, before sort test split up
nietras Mar 18, 2018
4259560
Split Sort test code into three files.
nietras Mar 18, 2018
9527a49
Remove uncommented Sort3 code
nietras Mar 18, 2018
146029f
Add below lines to comments
nietras Mar 18, 2018
e44b8b6
Remove AggressiveInlining attributes
nietras Mar 18, 2018
596df18
Add license to Common
nietras Mar 19, 2018
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
7 changes: 7 additions & 0 deletions src/System.Memory/ref/System.Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ public static class MemoryExtensions
public static int BinarySearch<T>(this Span<T> span, IComparable<T> comparable) { throw null; }
public static int BinarySearch<T, TComparable>(this Span<T> span, TComparable comparable) where TComparable : IComparable<T> { throw null; }
public static int BinarySearch<T, TComparer>(this Span<T> span, T value, TComparer comparer) where TComparer : IComparer<T> { throw null; }

public static void Sort<T>(this Span<T> span) { throw null; }
Copy link
Member

Choose a reason for hiding this comment

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

nice: new line.

Generate the reference using our tool.
Run msbuild /t:GenerateReferenceSource from the S.Memory source directory.

cc @weshaggard if you have any questions.

public static void Sort<T, TComparer>(this Span<T> span, TComparer comparer) where TComparer : IComparer<T> { throw null; }
public static void Sort<T>(this Span<T> span, Comparison<T> comparison) { throw null; }
public static void Sort<TKey, TValue>(this Span<TKey> keys, Span<TValue> items) { throw null; }
public static void Sort<TKey, TValue, TComparer>(this Span<TKey> keys, Span<TValue> items, TComparer comparer) where TComparer : IComparer<TKey> { throw null; }
public static void Sort<TKey, TValue>(this Span<TKey> keys, Span<TValue> items, Comparison<TKey> comparison) { throw null; }
}

public readonly struct ReadOnlyMemory<T>
Expand Down
13 changes: 13 additions & 0 deletions src/System.Memory/src/System.Memory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
<Compile Include="System\SpanHelpers.BinarySearch.cs" />
<Compile Include="System\SpanHelpers.T.cs" />
<Compile Include="System\SpanHelpers.byte.cs" />
<Compile Include="System\SpanSortHelpers.Common.cs" />
<Compile Include="System\SpanSortHelpers.Keys.cs" />
<Compile Include="System\SpanSortHelpers.Keys.Comparison.cs" />
<Compile Include="System\SpanSortHelpers.Keys.IComparable.cs" />
<Compile Include="System\SpanSortHelpers.Keys.Specialized.cs" />
<Compile Include="System\SpanSortHelpers.Keys.TComparer.cs" />
<Compile Include="System\SpanSortHelpers.Keys.TDirectComparer.cs" />
<Compile Include="System\SpanSortHelpers.KeysValues.cs" />
<Compile Include="System\SpanSortHelpers.KeysValues.Comparison.cs" />
<Compile Include="System\SpanSortHelpers.KeysValues.IComparable.cs" />
<Compile Include="System\SpanSortHelpers.KeysValues.Specialized.cs" />
<Compile Include="System\SpanSortHelpers.KeysValues.TComparer.cs" />
<Compile Include="System\SpanSortHelpers.KeysValues.TDirectComparer.cs" />
<Compile Include="System\ThrowHelper.cs" />
<Compile Include="System\Buffers\OperationStatus.cs" />
<Compile Include="System\Buffers\Binary\Reader.cs" />
Expand Down
77 changes: 77 additions & 0 deletions src/System.Memory/src/System/MemoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -922,5 +922,82 @@ public static int BinarySearch<T, TComparer>(
value, comparer);
return BinarySearch(span, comparable);
}


/// <summary>
/// Sorts the elements in the entire <see cref="Span{T}" />
/// using the <see cref="IComparable" /> implementation of each
/// element of the <see cref= "Span{T}" />
/// </summary>
/// <param name="span">The <see cref="Span{T}"/> to sort.</param>
/// <exception cref = "InvalidOperationException">
/// One or more elements do not implement the <see cref="IComparable" /> interface.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Sort<T>(this Span<T> span) => SpanSortHelpersKeys.Sort(span);

/// <summary>
/// Sorts the elements in the entire <see cref="Span{T}" />
/// using the <typeparamref name="TComparer" />.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Sort<T, TComparer>(this Span<T> span, TComparer comparer)
where TComparer : IComparer<T> =>
SpanSortHelpersKeys.Sort(span, comparer);

/// <summary>
/// Sorts the elements in the entire <see cref="Span{T}" />
/// using the <see cref="Comparison{T}" />.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Sort<T>(this Span<T> span, Comparison<T> comparison)
{
if (comparison == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparison);

SpanSortHelpersKeys.Sort(span, comparison);
}

/// <summary>
/// Sorts a pair of spans
/// (one contains the keys <see cref="Span{TKey}"/>
/// and the other contains the corresponding items <see cref="Span{TValue}"/>)
/// based on the keys in the first <see cref= "Span{TKey}" />
/// using the <see cref="IComparable" /> implementation of each
/// element of the <see cref= "Span{TKey}"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Sort<TKey, TValue>(this Span<TKey> keys, Span<TValue> items) =>
SpanSortHelpersKeysValues.Sort(keys, items);

/// <summary>
/// Sorts a pair of spans
/// (one contains the keys <see cref="Span{TKey}"/>
/// and the other contains the corresponding items <see cref="Span{TValue}"/>)
/// based on the keys in the first <see cref= "Span{TKey}" />
/// using the <typeparamref name="TComparer" />.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Sort<TKey, TValue, TComparer>(this Span<TKey> keys,
Span<TValue> items, TComparer comparer)
where TComparer : IComparer<TKey> =>
SpanSortHelpersKeysValues.Sort(keys, items, comparer);

/// <summary>
/// Sorts a pair of spans
/// (one contains the keys <see cref="Span{TKey}"/>
/// and the other contains the corresponding items <see cref="Span{TValue}"/>)
/// based on the keys in the first <see cref= "Span{TKey}" />
/// using the <see cref="Comparison{TKey}" />.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Sort<TKey, TValue>(this Span<TKey> keys,
Span<TValue> items, Comparison<TKey> comparison)
{
if (comparison == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparison);

SpanSortHelpersKeysValues.Sort(keys, items, comparison);
}
}
}
125 changes: 125 additions & 0 deletions src/System.Memory/src/System/SpanSortHelpers.Common.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Runtime.CompilerServices;

#if !netstandard
using Internal.Runtime.CompilerServices;
#endif

namespace System
{
internal static partial class SpanSortHelpersCommon
{
// This is the threshold where Introspective sort switches to Insertion sort.
// Empirically, 16 seems to speed up most cases without slowing down others, at least for integers.
// Large value types may benefit from a smaller number.
internal const int IntrosortSizeThreshold = 16;

internal static int FloorLog2PlusOne(int n)
{
Debug.Assert(n >= 2);
int result = 2;
n >>= 2;
while (n > 0)
{
++result;
n >>= 1;
}
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Swap<T>(ref T items, int i, int j)
{
Debug.Assert(i != j);
Swap(ref Unsafe.Add(ref items, i), ref Unsafe.Add(ref items, j));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Swap<T>(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}

// This started out with just LessThan.
// However, due to bogus comparers, comparables etc.
// we need to preserve semantics completely to get same result.
internal interface IDirectComparer<in T>
{
bool GreaterThan(T x, T y);
bool LessThan(T x, T y);
}

//
// Type specific DirectComparer(s) to ensure optimal code-gen
Copy link
Member

@ahsonkhan ahsonkhan Mar 19, 2018

Choose a reason for hiding this comment

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

Are these type-specific structs really necessary for optimal code-gen?

cc @AndyAyersMS

Copy link
Member

Choose a reason for hiding this comment

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

@nietras can you compare perf with and without these extra comparers, and if it's quite different, show the key bits of generated code?

Also there still seems to be a healthy dose of AggressiveInlining. I realize it is time consuming to evaluate each of these on its own merits but we do need to be careful with framework code not to use this too broadly. I'm not asking you to remove them wholesale, but I would think they'd be less necessary at the outer edges of this and really only important in the innermost parts of sorting where we expect overhead costs to be magnified. So please think critically about where this attribute is needed. If I'm wrong and there is some benefit to having them higher up it would be good to see examples.

Also (not necessarily critiquing this change, just wondering aloud) it seems like the same algorithmic patterns are duplicated across different comparison contexts, and I wonder if there is some way to avoid this, or whether we are missing language features and/or concepts that would allow there to be one master version that gets appropriately specialized and optimized, or whether this is just how things are done for now. I know we haven't put the effort into optimizing general comparers the way we have for equality comparers -- maybe we should?

Copy link
Author

Choose a reason for hiding this comment

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

@AndyAyersMS perf is significantly lower if using IComparer compared to the direct comparers. Array.Sort does the same for this, but via C++ template. The problem of course is IComparer will do more than one compare. I saw differences up to 2x for some scenarios. In fact, many scenarios could be improved if direct comparer was used for say float keys and int items/values. But due to the inconsistencies in sorting in Array.Sort, this would mean differences in sorting output.

So yes, these are necessary, and I would really like for perf to be good on existing runtimes.

//
internal struct SByteDirectComparer : IDirectComparer<sbyte>
{
public bool GreaterThan(sbyte x, sbyte y) => x > y;
public bool LessThan(sbyte x, sbyte y) => x < y;
}
internal struct ByteDirectComparer : IDirectComparer<byte>
{
public bool GreaterThan(byte x, byte y) => x > y;
public bool LessThan(byte x, byte y) => x < y;
}
internal struct Int16DirectComparer : IDirectComparer<short>
{
public bool GreaterThan(short x, short y) => x > y;
public bool LessThan(short x, short y) => x < y;
}
internal struct UInt16DirectComparer : IDirectComparer<ushort>
{
public bool GreaterThan(ushort x, ushort y) => x > y;
public bool LessThan(ushort x, ushort y) => x < y;
}
internal struct Int32DirectComparer : IDirectComparer<int>
{
public bool GreaterThan(int x, int y) => x > y;
public bool LessThan(int x, int y) => x < y;
}
internal struct UInt32DirectComparer : IDirectComparer<uint>
{
public bool GreaterThan(uint x, uint y) => x > y;
public bool LessThan(uint x, uint y) => x < y;
}
internal struct Int64DirectComparer : IDirectComparer<long>
{
public bool GreaterThan(long x, long y) => x > y;
public bool LessThan(long x, long y) => x < y;
}
internal struct UInt64DirectComparer : IDirectComparer<ulong>
{
public bool GreaterThan(ulong x, ulong y) => x > y;
public bool LessThan(ulong x, ulong y) => x < y;
}
internal struct SingleDirectComparer : IDirectComparer<float>
{
public bool GreaterThan(float x, float y) => x > y;
public bool LessThan(float x, float y) => x < y;
}
internal struct DoubleDirectComparer : IDirectComparer<double>
{
public bool GreaterThan(double x, double y) => x > y;
public bool LessThan(double x, double y) => x < y;
}

internal interface IIsNaN<T>
{
bool IsNaN(T value);
}
internal struct SingleIsNaN : IIsNaN<float>
{
public bool IsNaN(float value) => float.IsNaN(value);
}
internal struct DoubleIsNaN : IIsNaN<double>
{
public bool IsNaN(double value) => double.IsNaN(value);
}
}
}
Loading