Skip to content
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

OutPoint: Use CompareTo in operator== #1191

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions NBitcoin.Bench/OutPointBench.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NBitcoin.Bench
{
public class OutPointBench
{
private const int Count = 10000;
private OutPoint[] _outPoints;

[GlobalSetup]
public void Setup()
{
Random random = new Random(Seed: 53454);

_outPoints = new OutPoint[Count];

for (int i = 0; i < Count; i++)
{
uint256 txid = new uint256(RandomUtils.GetBytes(32));
int n = random.Next(1_000_000) % 2;

_outPoints[i] = new OutPoint(txid, n);
}
}

[Benchmark]
public void Old()
{
_ = DoComparisons(newImplementation: false);
}

[Benchmark]
public void New()
{
_ = DoComparisons(newImplementation: true);
}

private int DoComparisons(bool newImplementation)
{
int count = 0;

foreach (OutPoint o1 in _outPoints)
{
foreach (OutPoint o2 in _outPoints)
{
if (OutPoint.OperatorEqualsEquals(newImplementation, o1, o2))
{
count++;
}
}
}

return count;
}
}
}
22 changes: 22 additions & 0 deletions NBitcoin/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,27 @@ void SetNull()
return (a.hash == b.hash && a.n == b.n);
}

public static bool OperatorEqualsEquals(bool newImpl, OutPoint? a, OutPoint? b)
{
if (Object.ReferenceEquals(a, null))
{
return Object.ReferenceEquals(b, null);
}
if (Object.ReferenceEquals(b, null))
{
return false;
}

if (newImpl)
{
return (a.hash == b.hash && a.n.CompareTo(b.n) == 0);
}
else
{
return (a.hash == b.hash && a.n == b.n);
}
}

public static bool operator !=(OutPoint? a, OutPoint? b)
{
return !(a == b);
Expand Down Expand Up @@ -206,6 +227,7 @@ public override string ToString()
return $"{Hash}-{N}";
}
}

#nullable disable
public class TxIn : IBitcoinSerializable
{
Expand Down
Loading