Skip to content
Merged
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
10 changes: 8 additions & 2 deletions Benchmarks/Benchmarks/FirstOperatorBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ public void Setup()
[Benchmark(Baseline = true)]
public void FrozenSequenceFirst()
{
_ = _frozenSequence!.First(number => number is 50);
for (var i = 0; i < 3; i++)
{
_ = _frozenSequence!.First(number => number is 50);
}
}

[Benchmark]
public void ListFirst()
{
_ = _list!.First(number => number is 50);
for (var i = 0; i < 3; i++)
{
_ = _list!.First(number => number is 50);
}
}
}
38 changes: 38 additions & 0 deletions Benchmarks/Benchmarks/LastOperatorBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using BenchmarkDotNet.Attributes;
using Falko.Common.Extensions;
using Falko.Common.Sequences;

namespace Benchmarks;

public class LastOperatorBenchmark
{
private List<int>? _list;

private FrozenSequence<int>? _frozenSequence;

[GlobalSetup]
public void Setup()
{
_frozenSequence = Enumerable.Range(0, 100).ToFrozenSequence();

_list = Enumerable.Range(0, 100).ToList();
}

[Benchmark(Baseline = true)]
public void FrozenSequenceFirst()
{
for (var i = 0; i < 3; i++)
{
_ = _frozenSequence!.Last(number => number is 50);
}
}

[Benchmark]
public void ListFirst()
{
for (var i = 0; i < 3; i++)
{
_ = _list!.Last(number => number is 50);
}
}
}
5 changes: 1 addition & 4 deletions Benchmarks/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using BenchmarkDotNet.Running;
using Benchmarks;

var b = new FirstOperatorBenchmark();
b.Setup();
b.FrozenSequenceFirst();
b.ListFirst();
BenchmarkRunner.Run<FirstOperatorBenchmark>();
BenchmarkRunner.Run<LastOperatorBenchmark>();
2 changes: 1 addition & 1 deletion Falko.Common.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
<Project Path="Sources\Falko.Common.Sequences\Falko.Common.Sequences.csproj" Type="Classic C#" />
</Folder>
<Folder Name="/Tests/" />
</Solution>
</Solution>
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public static void ThrowIfNotSingle(int count)
}

[DoesNotReturn]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowNotMatchAny()
{
throw new InvalidOperationException("The source sequence does not match any of the specified conditions.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ public T First(Func<T, bool> predicate)
{
ArgumentNullException.ThrowIfNull(predicate);

foreach (ref readonly var item in this)
var itemsCount = _itemsCount;

scoped ref var itemsReference = ref MemoryMarshal.GetArrayDataReference(_items);

for (var itemIndex = 0; itemIndex < itemsCount; itemIndex++)
{
var item = Unsafe.Add(ref itemsReference, itemIndex);

if (predicate(item)) return item;
}

SequenceExceptions.ThrowNotMatchAny();
return default!; // This line is unreachable
return default; // This line is unreachable
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -41,8 +47,14 @@ public T First(Func<T, bool> predicate)
{
ArgumentNullException.ThrowIfNull(predicate);

foreach (ref readonly var item in this)
var itemsCount = _itemsCount;

scoped ref var itemsReference = ref MemoryMarshal.GetArrayDataReference(_items);

for (var itemIndex = 0; itemIndex < itemsCount; itemIndex++)
{
var item = Unsafe.Add(ref itemsReference, itemIndex);

if (predicate(item)) return item;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ public T Last()

public T Last(Func<T, bool> predicate)
{
var itemsCount = _itemsCount;

SequenceExceptions.ThrowIfEmpty(itemsCount);

ArgumentNullException.ThrowIfNull(predicate);

var itemsCount = _itemsCount;

scoped ref var itemsReference = ref MemoryMarshal.GetArrayDataReference(_items);

for (var itemIndex = itemsCount - 1; itemIndex >= 0; itemIndex--)
Expand All @@ -50,12 +48,10 @@ public T Last(Func<T, bool> predicate)

public T? LastOrDefault(Func<T, bool> predicate)
{
var itemsCount = _itemsCount;

if (itemsCount is 0) return default;

ArgumentNullException.ThrowIfNull(predicate);

var itemsCount = _itemsCount;

scoped ref var itemsReference = ref MemoryMarshal.GetArrayDataReference(_items);

for (var itemIndex = itemsCount - 1; itemIndex >= 0; itemIndex--)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public ref struct ValueEnumerator
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ValueEnumerator(T[] values, int valuesCount)
{
_currentIndex = -1;
_valuesReference = ref MemoryMarshal.GetArrayDataReference(values);
_valuesCount = valuesCount;
}
Expand All @@ -29,19 +30,18 @@ public ref readonly T Current
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
var currentIndex = _currentIndex;
var nextIndex = _currentIndex + 1;

if (currentIndex == _valuesCount) return false;

_currentIndex = currentIndex + 1;
if (nextIndex >= _valuesCount) return false;

_currentIndex = nextIndex;
return true;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset()
{
_currentIndex = 0;
_currentIndex = -1;
}
}
}