Skip to content

Commit

Permalink
Moving to a push model
Browse files Browse the repository at this point in the history
  • Loading branch information
manofstick committed Oct 28, 2020
1 parent bca3595 commit c29bc04
Show file tree
Hide file tree
Showing 22 changed files with 871 additions and 333 deletions.
2 changes: 1 addition & 1 deletion Benchmark/Cistern.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>Cistern.Benchmarks</RootNamespace>
</PropertyGroup>

Expand Down
91 changes: 91 additions & 0 deletions Benchmark/Double/SelectSelectToList/.netcore5.0-rc2.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Benchmark/Double/SelectSelectToList/Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class Benchmark
#if true
[Params(0, 1, 10, 100, 1000, 1000000)]
#else
[Params(0, 1, 10, 100)]
[Params(100)]
#endif
public int Length { get; set; } = 0;

Expand Down Expand Up @@ -50,7 +50,7 @@ internal static void SanityCheck()
var check = new Benchmark();

check.Length = 100;
check.ContainerType = ContainerTypes.Enumerable;
check.ContainerType = ContainerTypes.Array;
check.SetupData();

var baseline = check.Linq();
Expand Down
272 changes: 145 additions & 127 deletions Benchmark/Double/SelectSum/.netcore5.0-rc2.md

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions Benchmark/Double/SelectSum/Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ public partial class Benchmark
{
IEnumerable<double> _double;

#if trueX
[Params(0, 1, 10, 100, 1000, 1000000)]
//[Params(1000000)]
#else
[Params(100)]
#endif
public int Length { get; set; } = 0;

#if trueX
[Params(ContainerTypes.Array, ContainerTypes.Enumerable, ContainerTypes.List)]
//[Params(ContainerTypes.Enumerable)]
#else
[Params(ContainerTypes.Array)]
#endif
public ContainerTypes ContainerType { get; set; } = ContainerTypes.Enumerable;

[GlobalSetup]
Expand Down
91 changes: 91 additions & 0 deletions Benchmark/Double/SelectToList/.netcore5.0-rc2

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Benchmark/Double/SelectToList/Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public partial class Benchmark
{
IEnumerable<double> _double;

#if LONG
#if true
[Params(0, 1, 10, 100, 1000, 1000000)]
#else
[Params(0, 1, 10, 100)]
Expand Down
91 changes: 91 additions & 0 deletions Benchmark/Double/SelectWhereToList/.netcore5.0-rc2

Large diffs are not rendered by default.

232 changes: 125 additions & 107 deletions Benchmark/Double/Sum/.netcore5.0-rc2.md

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions Benchmark/Double/Sum/Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ public partial class Benchmark
{
IEnumerable<double> _double;

#if trueX
[Params(0, 1, 10, 100, 1000, 1000000)]
//[Params(1000000)]
#else
[Params(100)]
#endif
public int Length { get; set; } = 0;

#if trueX
[Params(ContainerTypes.Array, ContainerTypes.Enumerable, ContainerTypes.List)]
//[Params(ContainerTypes.Enumerable)]
#else
[Params(ContainerTypes.Array)]
#endif
public ContainerTypes ContainerType { get; set; } = ContainerTypes.Enumerable;

[GlobalSetup]
Expand Down Expand Up @@ -44,6 +50,8 @@ internal static void SanityCheck()
var check = new Benchmark();

check.Length = 100;
check.ContainerType = ContainerTypes.Array;

check.SetupData();

var baseline = check.Linq();
Expand Down
160 changes: 89 additions & 71 deletions Benchmark/Double/WhereSelectToList/.netcore5.0-rc2.md

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions Benchmark/Double/WhereToList/.netcore5.0-rc2.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void Main(string[] args)
Double.Any.Benchmark.SanityCheck();
Double.SelectSum.Benchmark.SanityCheck();

var summary = BenchmarkRunner.Run<DoubleDoubleDouble.SelectWhereAggregate.Benchmark>();
var summary = BenchmarkRunner.Run<Double.SelectSum.Benchmark>();
}
}
}
37 changes: 37 additions & 0 deletions ValueLinq/Aggregation/Sum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cistern.ValueLinq.Aggregation
{
#if OLD_WAY
struct SumInt
: INode
{
Expand Down Expand Up @@ -61,4 +62,40 @@ CreationType INode.CreateObjectAscent<CreationType, EnumeratorElement, Enumerato
TResult INode.CreateObjectViaFastEnumerator<TIn, TResult, FEnumerator>(in FEnumerator fenum)
=> Impl.CreateObjectViaFastEnumerator<TResult>();
}
#endif
struct SumIntForward
: IForwardEnumerator<int>
{
private int _sum;

public void Init(int? size) { }

public TResult GetResult<TResult>() => (TResult)(object)_sum;

public bool ProcessNext(int input)
{
checked
{
_sum += input;
return true;
}
}
}

struct SumDoubleForward
: IForwardEnumerator<double>
{
private double _sum;

public TResult GetResult<TResult>() => (TResult)(object)_sum;

public void Init(int? _) { }

public bool ProcessNext(double input)
{
_sum += input;
return true;
}
}

}
21 changes: 20 additions & 1 deletion ValueLinq/Aggregation/ToList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cistern.ValueLinq.Aggregation
{
#if OLD_WAY
struct ToList
: INode
{
Expand Down Expand Up @@ -48,4 +49,22 @@ static List<EnumeratorElement> DoToList(ref Enumerator enumerator)
}
}
}
}

#endif

struct ToListForward<T>
: IForwardEnumerator<T>
{
private List<T> _list;

TResult IForwardEnumerator<T>.GetResult<TResult>() => (TResult)(object)_list;

void IForwardEnumerator<T>.Init(int? size) => _list = size.HasValue ? new List<T>(size.Value) : new List<T>();

bool IForwardEnumerator<T>.ProcessNext(T input)
{
_list.Add(input);
return true;
}
}
}
16 changes: 12 additions & 4 deletions ValueLinq/Containers/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,21 @@ public static List<T> ToList<T>(T[] array, Func<T, bool> map)
internal static TResult FastEnumerate<TIn, TResult, FEnumerator>(TIn[] array, FEnumerator fenum)
where FEnumerator : IForwardEnumerator<TIn>
{
fenum.Init(null);
foreach (var item in array)
fenum.Init(array.Length);

Loop(array, ref fenum);

return fenum.GetResult<TResult>();
}

private static void Loop<TIn, FEnumerator>(TIn[] array, ref FEnumerator fenum)
where FEnumerator : IForwardEnumerator<TIn>
{
for (var i = 0; i < array.Length; ++i)
{
if (!fenum.ProcessNext(item))
if (!fenum.ProcessNext(array[i]))
break;
}
return fenum.GetResult<TResult>();
}
}
}
7 changes: 6 additions & 1 deletion ValueLinq/Containers/IEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,17 @@ public static List<T> ToList<T>(IEnumerable<T> enumerable, Func<T, bool> map)
internal static TResult FastEnumerate<TIn, TResult, FEnumerator>(IEnumerable<TIn> e, FEnumerator fenum) where FEnumerator : IForwardEnumerator<TIn>
{
fenum.Init(null);
Loop(e, ref fenum);
return fenum.GetResult<TResult>();
}

private static void Loop<TIn, FEnumerator>(IEnumerable<TIn> e, ref FEnumerator fenum) where FEnumerator : IForwardEnumerator<TIn>
{
foreach (var item in e)
{
if (!fenum.ProcessNext(item))
break;
}
return fenum.GetResult<TResult>();
}
}
}
15 changes: 11 additions & 4 deletions ValueLinq/Containers/ListByIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,22 @@ public static List<T> ToList<T>(List<T> list, Func<T, bool> map)
return newList;
}

internal static TResult FastEnumerate<TIn, TResult, FEnumerator>(List<TIn> list, FEnumerator fenum) where FEnumerator : IForwardEnumerator<TIn>
internal static TResult FastEnumerate<TIn, TResult, FEnumerator>(List<TIn> list, FEnumerator fenum)
where FEnumerator : IForwardEnumerator<TIn>
{
fenum.Init(null);
for(var i=0; i < list.Count; ++i)
fenum.Init(list.Count);
DoLoop(list, ref fenum);
return fenum.GetResult<TResult>();
}

private static void DoLoop<TIn, FEnumerator>(List<TIn> list, ref FEnumerator fenum)
where FEnumerator : IForwardEnumerator<TIn>
{
for (var i = 0; i < list.Count; ++i)
{
if (!fenum.ProcessNext(list[i]))
break;
}
return fenum.GetResult<TResult>();
}
}

Expand Down
22 changes: 18 additions & 4 deletions ValueLinq/Enumerable.ValueEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,16 @@ public static ValueEnumerable<T, WhereNode<T, TPrior>> Where<T, TPrior>(in this
return new ValueEnumerable<T, WhereNode<T, TPrior>>(new WhereNode<T, TPrior>(in prior.Node, predicate));
}
public static ValueEnumerable<T, Where_InNode<T, TPrior>> Where<T, TPrior>(in this ValueEnumerable<T, TPrior> prior, InFunc<T, bool> f) where TPrior : INode => new ValueEnumerable<T, Where_InNode<T, TPrior>>(new Where_InNode<T, TPrior>(in prior.Node, f));
public static List<T> ToList<T, Inner>(in this ValueEnumerable<T, Inner> inner) where Inner : INode =>
inner.Node.CheckForOptimization<T, Optimizations.ToList_XXX, List<T>>(new Optimizations.ToList_XXX(), out var list) switch
public static List<T> ToList<T, Inner>(in this ValueEnumerable<T, Inner> inner) where Inner : INode
#if OLD_WAY
=> inner.Node.CheckForOptimization<T, Optimizations.ToList_XXX, List<T>>(new Optimizations.ToList_XXX(), out var list) switch
{
false => Nodes<List<T>>.Aggregation<Inner, ToList>(in inner.Node),
true => list,
};
#else
=> inner.Node.CreateObjectViaFastEnumerator<T, List<T>, ToListForward<T>>(new ToListForward<T>());
#endif

public static T Last<T, Inner>(in this ValueEnumerable<T, Inner> inner) where Inner : INode =>
(inner.Node.CheckForOptimization<T, Optimizations.TryLast, (bool, T)>(new Optimizations.TryLast(), out var maybeLast), maybeLast) switch
Expand All @@ -123,8 +127,18 @@ public static T LastOrDefault<T, Inner>(in this ValueEnumerable<T, Inner> inner)
};


public static int Sum<Inner>(in this ValueEnumerable<int, Inner> inner) where Inner : INode => Nodes<int>.Aggregation<Inner, SumInt>(in inner.Node);
public static double Sum<Inner>(in this ValueEnumerable<double, Inner> inner) where Inner : INode => Nodes<double>.Aggregation<Inner, SumDouble>(in inner.Node);
public static int Sum<Inner>(in this ValueEnumerable<int, Inner> inner) where Inner : INode
#if OLD_WAY
=> Nodes<int>.Aggregation<Inner, SumInt>(in inner.Node);
#else
=> inner.Node.CreateObjectViaFastEnumerator<int, int, SumIntForward>(new SumIntForward());
#endif
public static double Sum<Inner>(in this ValueEnumerable<double, Inner> inner) where Inner : INode
#if OLD_WAY
=> Nodes<double>.Aggregation<Inner, SumDouble>(in inner.Node);
#else
=> inner.Node.CreateObjectViaFastEnumerator<double, double, SumDoubleForward>(new SumDoubleForward());
#endif

public static int Count<T, Inner>(in this ValueEnumerable<T, Inner> inner) where Inner : INode => Enumerable.Count<T, Inner>(in inner.Node);

Expand Down
22 changes: 19 additions & 3 deletions ValueLinq/Nodes/Selecti.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,24 @@ CreationType INode.CreateObjectAscent<CreationType, EnumeratorElement, Enumerato
bool INode.CheckForOptimization<TOuter, TRequest, TResult>(in TRequest request, out TResult result) { result = default; return false;}

public TResult CreateObjectViaFastEnumerator<TIn, TResult, FEnumerator>(in FEnumerator fenum) where FEnumerator : IForwardEnumerator<TIn>
{
throw new NotImplementedException();
}
=> _nodeT.CreateObjectViaFastEnumerator<T, TResult, SelectiFoward<T, TIn, FEnumerator>>(new SelectiFoward<T, TIn, FEnumerator>(fenum, (Func<T, int, TIn>)(object) _map));
}

struct SelectiFoward<T, U, Next>
: IForwardEnumerator<T>
where Next : IForwardEnumerator<U>
{
Next _next;
Func<T, int, U> _selector;
int _idx;

public SelectiFoward(in Next prior, Func<T, int, U> predicate) => (_next, _selector, _idx) = (prior, predicate, 0);

public TResult GetResult<TResult>() => _next.GetResult<TResult>();

public void Init(int? size) => _next.Init(size);

public bool ProcessNext(T input) => _next.ProcessNext(_selector(input, _idx++));
}

}
2 changes: 1 addition & 1 deletion ValueLinq/Nodes/Where.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct WhereFoward<T, Next>

public TResult GetResult<TResult>() => _next.GetResult<TResult>();

public void Init(int? size) => _next.Init(size);
public void Init(int? size) => _next.Init(null);

public bool ProcessNext(T input)
{
Expand Down
2 changes: 1 addition & 1 deletion ValueLinq/Nodes/Where_In.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct Where_InFoward<T, Next>

public TResult GetResult<TResult>() => _next.GetResult<TResult>();

public void Init(int? size) => _next.Init(size);
public void Init(int? size) => _next.Init(null);

public bool ProcessNext(T input)
{
Expand Down

0 comments on commit c29bc04

Please sign in to comment.