Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Jun 26, 2021
1 parent e78d541 commit 882ecfc
Show file tree
Hide file tree
Showing 27 changed files with 74 additions and 109 deletions.
28 changes: 14 additions & 14 deletions Benchmarks/ArrayIterationBenchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ BenchmarkDotNet=v0.13.0.1555-nightly, OS=Windows 10.0.19043.1081 (21H1/May2021Up
Intel Core i7-7567U CPU 3.50GHz (Kaby Lake), 1 CPU, 4 logical and 2 physical cores
.NET SDK=6.0.100-preview.5.21302.13
[Host] : .NET 6.0.0 (6.0.21.30105), X64 RyuJIT
Job-UGMGOQ : .NET 6.0.0 (6.0.21.30105), X64 RyuJIT
Job-SLIMHF : .NET 6.0.0 (6.0.21.30105), X64 RyuJIT

Runtime=.NET 6.0

```
| Method | Count | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|--------------------- |-------- |-----------:|---------:|--------:|-------------:|--------:|------:|------:|------:|----------:|
| Foreach | 1000000 | 411.6 μs | 3.58 μs | 2.99 μs | baseline | | - | - | - | - |
| For | 1000000 | 410.3 μs | 1.57 μs | 1.22 μs | 1.00x faster | 0.01x | - | - | - | - |
| For_Unsafe | 1000000 | 411.1 μs | 1.58 μs | 1.48 μs | 1.00x faster | 0.01x | - | - | - | - |
| ForAdamczewski | 1000000 | 404.1 μs | 1.83 μs | 1.53 μs | 1.02x faster | 0.01x | - | - | - | - |
| ForAdamczewskiUnsafe | 1000000 | 411.2 μs | 1.93 μs | 1.71 μs | 1.00x faster | 0.01x | - | - | - | - |
| Span | 1000000 | 411.1 μs | 1.74 μs | 1.36 μs | 1.00x faster | 0.01x | - | - | - | - |
| ArraySegment_Foreach | 1000000 | 2,790.2 μs | 10.03 μs | 7.83 μs | 6.78x slower | 0.06x | - | - | - | 1 B |
| ArraySegment_For | 1000000 | 801.6 μs | 2.79 μs | 2.33 μs | 1.95x slower | 0.02x | - | - | - | - |
| ArraySegment_AsSpan | 1000000 | 410.4 μs | 1.33 μs | 1.24 μs | 1.00x faster | 0.01x | - | - | - | - |
| ArraySegment_AsArray | 1000000 | 560.8 μs | 2.97 μs | 2.63 μs | 1.36x slower | 0.01x | - | - | - | - |
| Vector | 1000000 | 104.3 μs | 0.54 μs | 0.50 μs | 3.95x faster | 0.04x | - | - | - | - |
| Method | Count | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|--------------------- |-------- |-----------:|---------:|---------:|-------------:|--------:|------:|------:|------:|----------:|
| Foreach | 1000000 | 432.4 μs | 3.92 μs | 3.06 μs | baseline | | - | - | - | - |
| For | 1000000 | 435.1 μs | 8.19 μs | 7.26 μs | 1.01x slower | 0.02x | - | - | - | - |
| For_Unsafe | 1000000 | 434.8 μs | 8.67 μs | 8.11 μs | 1.00x slower | 0.02x | - | - | - | - |
| ForAdamczewski | 1000000 | 369.4 μs | 6.03 μs | 5.03 μs | 1.17x faster | 0.02x | - | - | - | - |
| ForAdamczewskiUnsafe | 1000000 | 436.1 μs | 4.05 μs | 3.38 μs | 1.01x slower | 0.01x | - | - | - | - |
| Span | 1000000 | 431.5 μs | 2.91 μs | 2.43 μs | 1.00x faster | 0.01x | - | - | - | - |
| ArraySegment_Foreach | 1000000 | 2,890.1 μs | 34.93 μs | 30.97 μs | 6.69x slower | 0.07x | - | - | - | 1 B |
| ArraySegment_For | 1000000 | 836.4 μs | 9.11 μs | 7.60 μs | 1.94x slower | 0.02x | - | - | - | - |
| ArraySegment_AsSpan | 1000000 | 430.0 μs | 4.37 μs | 3.65 μs | 1.01x faster | 0.01x | - | - | - | - |
| ArraySegment_AsArray | 1000000 | 575.1 μs | 3.28 μs | 2.91 μs | 1.33x slower | 0.01x | - | - | - | - |
| Vector | 1000000 | 118.6 μs | 1.91 μs | 2.12 μs | 3.63x faster | 0.08x | - | - | - | - |
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ public int ForAdamczewski()
var sum2 = 0;
for (var index = 0; index <= source.Length - 2; index += 2)
{
long i1 = index + 0;
long i2 = index + 1;
var c = source[i1];
var d = source[i2];
var c = source[index + 0];
var d = source[index + 1];

sum1 += c;
sum2 += d;
Expand All @@ -97,10 +95,8 @@ public unsafe int ForAdamczewskiUnsafe()
var sum2 = 0;
for (var index = 0; index <= len; index += 2)
{
long i1 = index + 0;
long i2 = index + 1;
var c = source[i1];
var d = source[i2];
var c = source[index + 0];
var d = source[index + 1];

sum1 += c;
sum2 += d;
Expand Down
31 changes: 8 additions & 23 deletions NetFabric.Hyperlinq/Aggregation/Sum/Sum.ReadOnlySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,11 @@ static TSource Sum<TSource>(this ReadOnlySpan<TSource> source)
for (var index = 0; index < Vector<TSource>.Count; index++)
sum = GenericsOperator.Add(vectorSum[index], sum);

for (var index = source.Length - (source.Length % Vector<TSource>.Count); index < source.Length; index++)
{
var item = source[index];
sum = GenericsOperator.Add(item, sum);
}
var count = source.Length % Vector<TSource>.Count;
source = source.Slice(source.Length - count, count);
}
else
{
foreach (var item in source)
sum = GenericsOperator.Add(item, sum);
}

foreach (var item in source)
sum = GenericsOperator.Add(item, sum);
return sum;
}

Expand All @@ -59,28 +52,20 @@ static TSource Sum<TSource>(this ReadOnlySpan<TSource> source)
for (var index = 0; index < Vector<TResult>.Count; index++)
sum = GenericsOperator.Add(vectorSum[index], sum);

for (var index = source.Length - (source.Length % Vector<TSource>.Count); index < source.Length; index++)
{
var item = source[index];
sum = GenericsOperator.Add(selector.Invoke(item), sum);
}
}
else
{
foreach (var item in source)
sum = GenericsOperator.Add(selector.Invoke(item), sum);
var count = source.Length % Vector<TSource>.Count;
source = source.Slice(source.Length - count, count);
}
foreach (var item in source)
sum = GenericsOperator.Add(selector.Invoke(item), sum);
return sum;
}

static TSum Sum<TSource, TSum>(this ReadOnlySpan<TSource> source)
where TSum : struct
{
var sum = default(TSum);

foreach (var item in source)
sum = GenericsOperator.AddNullable(item, sum);

return sum;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void CopyTo(Span<TSource> span)
using var enumerator = GetEnumerator();
checked
{
for (var index = 0; enumerator.MoveNext() && index < span.Length; index++)
for (var index = 0; enumerator.MoveNext(); index++)
span[index] = enumerator.Current;
}
}
Expand All @@ -65,7 +65,7 @@ public void CopyTo(TSource[] array, int arrayIndex)
collection.CopyTo(array, arrayIndex);
break;
default:
CopyTo(array.AsSpan().Slice(arrayIndex));
CopyTo(array.AsSpan(arrayIndex));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void CopyTo(TSource[] array, int arrayIndex)
collection.CopyTo(array, arrayIndex);
break;
default:
CopyTo(array.AsSpan().Slice(arrayIndex));
CopyTo(array.AsSpan(arrayIndex));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void CopyTo(Span<TSource> span)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(TSource[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));


[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void CopyTo(Span<TSource> span)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(TSource[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));


[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void CopyTo(TSource[] array, int arrayIndex)
collection.CopyTo(array, arrayIndex);
break;
default:
CopyTo(array.AsSpan().Slice(arrayIndex));
CopyTo(array.AsSpan(arrayIndex));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void CopyTo(TSource[] array, int arrayIndex)
collection.CopyTo(array, arrayIndex);
break;
default:
CopyTo(array.AsSpan().Slice(arrayIndex));
CopyTo(array.AsSpan(arrayIndex));
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion NetFabric.Hyperlinq/Generation/ValueEnumerable/Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void CopyTo(Span<int> span)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(int[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));


[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
4 changes: 2 additions & 2 deletions NetFabric.Hyperlinq/Generation/ValueEnumerable/Repeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void CopyTo(Span<TSource> span)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(TSource[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(TSource item)
Expand Down Expand Up @@ -217,7 +217,7 @@ public static void CopyToVector<TSource>(this RepeatEnumerable<TSource> source,
for (var index = 0; index < destinationVectors.Length; index++)
destinationVectors[index] = vector;

for (var index = count - (count % Vector<TSource>.Count); index < count && index < destination.Length; index++)
for (var index = count - (count % Vector<TSource>.Count); index < count; index++)
destination[index] = value;
}
else
Expand Down
2 changes: 1 addition & 1 deletion NetFabric.Hyperlinq/Generation/ValueEnumerable/Return.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void CopyTo(Span<TSource> span)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(TSource[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(TSource item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public void CopyTo(Span<TSource> span)

if (offset is 0)
{
for(var index = 0; index < Count && index < span.Length; index++)
for(var index = 0; index < Count; index++)
span[index] = source[index];
}
else
{
for(var index = 0; index < Count && index < span.Length; index++)
for(var index = 0; index < Count; index++)
span[index] = source[index + offset];
}
}
Expand All @@ -93,7 +93,7 @@ public void CopyTo(TSource[] array, int arrayIndex)
if (offset is 0 && Count == source.Count && source is ICollection<TSource> collection)
collection.CopyTo(array, arrayIndex);
else
CopyTo(array.AsSpan().Slice(arrayIndex));
CopyTo(array.AsSpan(arrayIndex));
}

public bool Contains(TSource item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void CopyTo(TSource[] array, int arrayIndex)
if (offset is 0 && Count == source.Count && source is ICollection<TSource> collection)
collection.CopyTo(array, arrayIndex);
else
CopyTo(array.AsSpan().Slice(arrayIndex));
CopyTo(array.AsSpan(arrayIndex));
}

public bool Contains(TSource item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void CopyTo(Span<TResult> span)
}

void ICollection<TResult>.CopyTo(TResult[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));

void ICollection<TResult>.Add(TResult item)
=> Throw.NotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void CopyTo(Span<TResult> span)
}

void ICollection<TResult>.CopyTo(TResult[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));

void ICollection<TResult>.Add(TResult item)
=> Throw.NotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void CopyTo(Span<TResult> span)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(TResult[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));

public bool Contains(TResult item)
=> ValueReadOnlyCollectionExtensions.Contains<TEnumerable, TEnumerator, TSource, TResult, TSelector>(source, item, default, selector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal SelectEnumerable(in TEnumerable source, TSelector selector)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void CopyTo(TResult[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));

public readonly bool Contains(TResult item)
=> ValueReadOnlyCollectionExtensions.Contains<TEnumerable, TEnumerator, TSource, TResult, TSelector>(source, item, default, selector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void CopyTo(Span<TResult> span)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(TResult[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));

public bool Contains(TResult item)
=> ContainsAt<TEnumerable, TEnumerator, TSource, TResult, TSelector>(source, item, default, selector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void CopyTo(Span<TResult> span)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(TResult[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));

public readonly bool Contains(TResult item)
=> ValueReadOnlyCollectionExtensions.ContainsAt<TEnumerable, TEnumerator, TSource, TResult, TSelector>(source, item, default, selector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,14 @@ static bool ContainsVector<TSource>(this ReadOnlySpan<TSource> source, TSource v
return true;
}

for (var index = source.Length - (source.Length % Vector<TSource>.Count); index < source.Length; index++)
{
var item = source[index];
if (GenericsOperator.Equals(item, value))
return true;
}
var count = source.Length % Vector<TSource>.Count;
source = source.Slice(source.Length - count, count);
}
else
foreach (var item in source)
{
foreach (var item in source)
{
if (GenericsOperator.Equals(item, value))
return true;
}
}

if (GenericsOperator.Equals(item, value))
return true;
}
return false;
}

Expand All @@ -63,22 +55,14 @@ static bool ContainsVector<TSource>(this ReadOnlySpan<TSource> source, TSource v
return true;
}

for (var index = source.Length - (source.Length % Vector<TSource>.Count); index < source.Length; index++)
{
var item = source[index];
if (GenericsOperator.Equals(selector.Invoke(item), value))
return true;
}
var count = source.Length % Vector<TSource>.Count;
source = source.Slice(source.Length - count, count);
}
else
foreach (var item in source)
{
foreach (var item in source)
{
if (GenericsOperator.Equals(selector.Invoke(item), value))
return true;
}
if (GenericsOperator.Equals(selector.Invoke(item), value))
return true;
}

return false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions NetFabric.Hyperlinq/Set/Distinct/Set.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void Resize()
Array.Clear(newSlots, Count, newSlots.Length - Count);

Array.Clear(newBuckets, 0, newBuckets.Length);
for (var index = 0; index < Count && index < newSlots.Length; index++)
for (var index = 0; index < Count; index++)
{
var bucket = newSlots[index].HashCode % newSize;
newSlots[index].Next = newBuckets[bucket] - 1;
Expand Down Expand Up @@ -202,16 +202,16 @@ void Resize()
if (Count is 0)
return;

var source = slots!.AsSpan().Slice(0, Count);
for (var index = 0; index < source.Length && index < span.Length; index++)
var source = slots!.AsSpan(0, Count);
for (var index = 0; index < source.Length; index++)
{
ref readonly var slot = ref source[index];
span[index] = slot.Value;
}
}

public readonly void CopyTo(TElement[] array, int arrayIndex)
=> CopyTo(array.AsSpan().Slice(arrayIndex));
=> CopyTo(array.AsSpan(arrayIndex));

[ExcludeFromCodeCoverage]
readonly void ICollection<TElement>.Add(TElement item)
Expand Down
2 changes: 1 addition & 1 deletion NetFabric.Hyperlinq/Utils/ArrayBuilder/ArrayBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public int Capacity
// }

public readonly Span<T> AsSpan()
=> buffer!.AsSpan().Slice(0, Count);
=> buffer!.AsSpan(0, Count);

/// <summary>
/// Adds an item to the backing array, resizing it if necessary.
Expand Down
Loading

0 comments on commit 882ecfc

Please sign in to comment.