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
Fix Support for Collection Expressions #1323
Fix Support for Collection Expressions #1323
Conversation
Benchmarkusing System;
using BenchmarkDotNet.Attributes;
namespace LanguageExt.Benchmarks
{
[RPlotExporter, RankColumn]
[GenericTypeArguments(typeof(int))]
[GenericTypeArguments(typeof(string))]
public class SetBenchmarks<T>
{
[Params(0, 1, 2, 3, 10, 100, 1000, 10000, 100000)]
public int N;
T[] values;
[GlobalSetup]
public void Setup()
{
values = ValuesGenerator.Default.GenerateUniqueValues<T>(N);
}
[Benchmark]
public Set<T> CreateLoopSpan()
{
var span = new ReadOnlySpan<T>(values);
return Set.create(span);
}
[Benchmark]
public Set<T> CreateCopySpanToArray()
{
var span = new ReadOnlySpan<T>(values);
var array = span.ToArray();
return new Set<T>(array);
}
}
} Note that here I changed [Pure]
public static Set<T> create<T>(
ReadOnlySpan<T> items)
{
var set = empty<T>();
foreach (var item in items)
{
set = set.TryAdd(item);
}
} |
@louthy Thank you! |
I'm really sorry, but I don't have the bandwidth to maintain language-ext has eaten several months of my time recently, which I really should have been putting into building my new startup project (I have meetings lined up with VCs in September, so I'm pushing pretty hard on my project and the stability of This is an MIT licensed project though, so you're well within your rights to fork it and maintain your own version until you're ready to come over to
Sorry I can't be more help. |
@louthy Good luck + success with your new startup! |
This change fixes support for collection expressions like:
for
Arr<T>
Lst<T>
Seq<T>
Set<T>
I've chosen these types because the compiler thinks the collection expressions are already supported for these (it compiles), but the resulting collections are always empty.
Collection Initializers Unaffected
This does not fix collection initializers like:
This still results in an empty arr.
Implementation
Implemented create methods according to https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/collection-expressions#create-methods
see commit 9fff3cc
There's also tests for this, see 4505936
Performance Considerations for
Set<T>
The
ReadOnlySpan<T>
passed to the create methods does not implementIEnumerable<T>
and thus cannot be passed to existing constructors ofSet<T>
.I've compared two options:
ReadOnlySpan<T>.ToArray()
foreach
loop overReadOnlySpan<T>
which performsSet.TryAdd
Benchmarking showed that the loop is faster for 0 or 1 elements. After that,
ToArray()
is significantly faster.Implementation for
ReadOnlySpan<T>.ToArray()
Implementation with
foreach
loopImplementation of Benchmark
See #1323 (comment)