dadhi / ImTools

Fast and memory-efficient immutable collections and helper data structures
MIT License
232 stars 10 forks source link

Benchmark vs F# sum types #18

Closed dadhi closed 4 years ago

dadhi commented 5 years ago

For instance we can match over array of union vs F# sum-type.

Maybe try SOA UnionList as well.

dadhi commented 5 years ago

The first results are here: https://github.com/dadhi/ImTools/blob/master/playground/ImTools.UnionCsVsFsBenchmarks/Program.fs

Remarks regarding F#:

dadhi commented 5 years ago

New naming proposal:

public abstract class Case<TName, T>
{
   public readonly struct @case : I<T>, IEq<value>
   {
       public T Value { get; }
       //...
   }
}

public sealed class U2<A, B> : U<Empty, A, B> {}
public abstract class U<TName, A, B>
{
   public interface union
   {
       R Match<A, B, R>(Func<A, R> fa...);
   }
   public readonly struct case1 : union, I<A>, IEq<case1>
   {
      public A Value { get; }
      //...
   }
}
dadhi commented 4 years ago

Results:

BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18362
Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=3.0.100
  [Host]     : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), X64 RyuJIT DEBUG
  DefaultJob : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), X64 RyuJIT

|                                     Method |      Mean |    Error |   StdDev | Ratio | RatioSD |  Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------------------------------------- |----------:|---------:|---------:|------:|--------:|-------:|------:|------:|----------:|
|                                     FSharp |  12.49 ns | 0.031 ns | 0.029 ns |  1.00 |    0.00 |      - |     - |     - |         - |
|                   CSharp_named_case_struct |  15.43 ns | 0.085 ns | 0.080 ns |  1.24 |    0.01 |      - |     - |     - |         - |
|         CSharp_named_case_struct_tag_match |  15.00 ns | 0.162 ns | 0.152 ns |  1.20 |    0.01 |      - |     - |     - |         - |
| CSharp_named_case_struct_match_I_interface | 248.44 ns | 4.045 ns | 3.784 ns | 19.89 |    0.29 |      - |     - |     - |         - |
|      CSharp_named_case_struct_Match_method | 181.99 ns | 0.563 ns | 0.527 ns | 14.57 |    0.06 | 0.1733 |     - |     - |     816 B |