From ff09630d6d792dc57ac25142d1c566b6a714defd Mon Sep 17 00:00:00 2001 From: Kiminuo Date: Mon, 23 Oct 2023 13:32:32 +0200 Subject: [PATCH] WIP --- NBitcoin.Bench/OutPointBench.cs | 60 +++++++++++++++++++++++++++++++++ NBitcoin/Transaction.cs | 22 ++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 NBitcoin.Bench/OutPointBench.cs diff --git a/NBitcoin.Bench/OutPointBench.cs b/NBitcoin.Bench/OutPointBench.cs new file mode 100644 index 0000000000..44909ee541 --- /dev/null +++ b/NBitcoin.Bench/OutPointBench.cs @@ -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; + } + } +} diff --git a/NBitcoin/Transaction.cs b/NBitcoin/Transaction.cs index b31c907c3c..1985d01f5a 100644 --- a/NBitcoin/Transaction.cs +++ b/NBitcoin/Transaction.cs @@ -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); @@ -206,6 +227,7 @@ public override string ToString() return $"{Hash}-{N}"; } } + #nullable disable public class TxIn : IBitcoinSerializable {