diff --git a/src/FSharp.Data.Adaptive/Datastructures/Index.fs b/src/FSharp.Data.Adaptive/Datastructures/Index.fs index 77720cf..60715d2 100644 --- a/src/FSharp.Data.Adaptive/Datastructures/Index.fs +++ b/src/FSharp.Data.Adaptive/Datastructures/Index.fs @@ -3,6 +3,7 @@ open System open System.Threading open System.Collections.Generic +open System.Runtime.CompilerServices /// the internal implementation of our order-maintenance structure. [] @@ -138,6 +139,10 @@ type IndexNode = | false, false -> x.CompareTo o + [] + member x.Equals (o : IndexNode) = + System.Object.ReferenceEquals(x,o) + interface IComparable with member x.CompareTo (o : obj) = match o with @@ -270,6 +275,10 @@ type Index private(real : IndexNode) = override x.ToString() = real.ToString() member private x.AsString = x.ToString() + interface IEquatable with + member x.Equals (o: Index): bool = + real.Equals o.Value + interface IComparable with member x.CompareTo(o : obj) = match o with diff --git a/src/FSharp.Data.Adaptive/Datastructures/Index.fsi b/src/FSharp.Data.Adaptive/Datastructures/Index.fsi index 49f38bb..11d60b3 100644 --- a/src/FSharp.Data.Adaptive/Datastructures/Index.fsi +++ b/src/FSharp.Data.Adaptive/Datastructures/Index.fsi @@ -9,6 +9,7 @@ open System type Index = interface IComparable #if !FABLE_COMPILER + interface IEquatable interface IComparable #endif diff --git a/src/Test/FSharp.Data.Adaptive.Tests/Benchmarks/IndexBenchmarks.fs b/src/Test/FSharp.Data.Adaptive.Tests/Benchmarks/IndexBenchmarks.fs new file mode 100644 index 0000000..27a1fe1 --- /dev/null +++ b/src/Test/FSharp.Data.Adaptive.Tests/Benchmarks/IndexBenchmarks.fs @@ -0,0 +1,59 @@ +namespace Benchmarks + +open BenchmarkDotNet.Attributes +open FSharp.Data.Adaptive + +//BenchmarkDotNet v0.14.0, Windows 10 (10.0.19045.4894/22H2/2022Update) +//Intel Core i7-8700K CPU 3.70GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores +//.NET SDK 8.0.400 +// [Host] : .NET 8.0.8 (8.0.824.36612), X64 RyuJIT AVX2 DEBUG +// DefaultJob : .NET 8.0.8 (8.0.824.36612), X64 RyuJIT AVX2 + +//| Method | CreateCount | Mean | Error | StdDev | Allocated | +//|-------------- |------------ |---------:|--------:|--------:|----------:| +//| HashSetAdd | 10000 | 221.6 μs | 2.46 μs | 2.05 μs | - | +//| DictionaryAdd | 10000 | 230.2 μs | 0.59 μs | 0.55 μs | - | + +// with Index IEquatable +//| Method | CreateCount | Mean | Error | StdDev | Allocated | +//|-------------- |------------ |---------:|--------:|--------:|----------:| +//| HashSetAdd | 10000 | 204.5 us | 0.37 us | 0.34 us | - | +//| DictionaryAdd | 10000 | 215.2 us | 2.46 us | 2.18 us | - | + +// + inlined IndexNode Equals +//| Method | CreateCount | Mean | Error | StdDev | Allocated | +//|-------------- |------------ |---------:|--------:|--------:|----------:| +//| HashSetAdd | 10000 | 199.1 us | 3.85 us | 4.12 us | - | +//| DictionaryAdd | 10000 | 205.4 us | 1.43 us | 1.33 us | - | + +[] +type IndexEqualsBenchmarks() = + + [] + val mutable public Count : int + + let mutable indexSet = Unchecked.defaultof<_> + let mutable indexDict = Unchecked.defaultof<_> + let mutable indices = Array.empty + + [] + member x.Setup() = + indexSet <- System.Collections.Generic.HashSet(x.Count) + indexDict <- System.Collections.Generic.Dictionary(x.Count) + indices <- Array.zeroCreate x.Count + let mutable h = Index.zero + for i in 0..x.Count-1 do + h <- Index.after h + indices.[i] <- h + + [] + member x.HashSetAdd() = + for i in indices do + indexSet.Add(i) |> ignore + + [] + member x.DictionaryAdd() = + let mutable ii = 0 + for i in indices do + indexDict.[i] <- ii + ii <- ii + 1 diff --git a/src/Test/FSharp.Data.Adaptive.Tests/FSharp.Data.Adaptive.Tests.fsproj b/src/Test/FSharp.Data.Adaptive.Tests/FSharp.Data.Adaptive.Tests.fsproj index 11c51a5..c06707e 100644 --- a/src/Test/FSharp.Data.Adaptive.Tests/FSharp.Data.Adaptive.Tests.fsproj +++ b/src/Test/FSharp.Data.Adaptive.Tests/FSharp.Data.Adaptive.Tests.fsproj @@ -26,6 +26,7 @@ + diff --git a/src/Test/FSharp.Data.Adaptive.Tests/Program.fs b/src/Test/FSharp.Data.Adaptive.Tests/Program.fs index 28173dc..12fdc68 100644 --- a/src/Test/FSharp.Data.Adaptive.Tests/Program.fs +++ b/src/Test/FSharp.Data.Adaptive.Tests/Program.fs @@ -50,7 +50,7 @@ let main _args = //ASet.``[ASet] mapA/flattenA/chooseA async``() //ASet.``[ASet] union constant``() - AList.``[AList] toAset``() + //AList.``[AList] toAset``() //``[AList] sub``(); @@ -91,5 +91,6 @@ let main _args = //BenchmarkRunner.Run() |> ignore //BenchmarkRunner.Run() |> ignore //BenchmarkRunner.Run() |> ignore + BenchmarkRunner.Run() |> ignore 0