Skip to content

Commit

Permalink
added IEquatable interface to Index
Browse files Browse the repository at this point in the history
  • Loading branch information
luithefirst committed Sep 14, 2024
1 parent 5f9c7f8 commit efd55a8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/FSharp.Data.Adaptive/Datastructures/Index.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
[<StructuredFormatDisplay("{AsString}")>]
Expand Down Expand Up @@ -138,6 +139,10 @@ type IndexNode =
| false, false ->
x.CompareTo o

[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
member x.Equals (o : IndexNode) =
System.Object.ReferenceEquals(x,o)

interface IComparable with
member x.CompareTo (o : obj) =
match o with
Expand Down Expand Up @@ -270,6 +275,10 @@ type Index private(real : IndexNode) =
override x.ToString() = real.ToString()
member private x.AsString = x.ToString()

interface IEquatable<Index> with
member x.Equals (o: Index): bool =
real.Equals o.Value

interface IComparable with
member x.CompareTo(o : obj) =
match o with
Expand Down
1 change: 1 addition & 0 deletions src/FSharp.Data.Adaptive/Datastructures/Index.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ open System
type Index =
interface IComparable
#if !FABLE_COMPILER
interface IEquatable<Index>
interface IComparable<Index>
#endif

Expand Down
59 changes: 59 additions & 0 deletions src/Test/FSharp.Data.Adaptive.Tests/Benchmarks/IndexBenchmarks.fs
Original file line number Diff line number Diff line change
@@ -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 | - |

[<MemoryDiagnoser>]
type IndexEqualsBenchmarks() =

[<Params(10000); DefaultValue>]
val mutable public Count : int

let mutable indexSet = Unchecked.defaultof<_>
let mutable indexDict = Unchecked.defaultof<_>
let mutable indices = Array.empty

[<GlobalSetup>]
member x.Setup() =
indexSet <- System.Collections.Generic.HashSet<Index>(x.Count)
indexDict <- System.Collections.Generic.Dictionary<Index, int>(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

[<Benchmark>]
member x.HashSetAdd() =
for i in indices do
indexSet.Add(i) |> ignore

[<Benchmark>]
member x.DictionaryAdd() =
let mutable ii = 0
for i in indices do
indexDict.[i] <- ii
ii <- ii + 1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Compile Include="Benchmarks\HashSetDeltaBench.fs" />
<Compile Include="Benchmarks\ListDeltaBenchmarks.fs" />
<Compile Include="Benchmarks\IndexListBenchmarks.fs" />
<Compile Include="Benchmarks\IndexBenchmarks.fs" />
<Compile Include="Utilities\Helpers.fs" />
<Compile Include="Utilities\Generators.fs" />
<Compile Include="Callbacks.fs" />
Expand Down
3 changes: 2 additions & 1 deletion src/Test/FSharp.Data.Adaptive.Tests/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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``();

Expand Down Expand Up @@ -91,5 +91,6 @@ let main _args =
//BenchmarkRunner.Run<Benchmarks.MapExtEnumeratorBenchmark>() |> ignore
//BenchmarkRunner.Run<Benchmarks.HashSetDeltaBench>() |> ignore
//BenchmarkRunner.Run<Benchmarks.IndexListBenchmarks>() |> ignore
BenchmarkRunner.Run<Benchmarks.IndexEqualsBenchmarks>() |> ignore

0

0 comments on commit efd55a8

Please sign in to comment.