|
| 1 | +using System.Buffers.Binary; |
| 2 | +using System.Numerics; |
| 3 | +using System.Runtime.CompilerServices; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Runtime.Intrinsics; |
| 6 | + |
| 7 | +using BenchmarkDotNet.Attributes; |
| 8 | +using BenchmarkDotNet.Jobs; |
| 9 | + |
| 10 | +namespace Base58Encoding.Benchmarks; |
| 11 | + |
| 12 | +// End-to-end 64-byte decode A/B. Both arms run the identical full decode path (validate, |
| 13 | +// intermediate build, matmul, carry-reduce, leading-zero check, byte output) and differ ONLY |
| 14 | +// in the matmul step (SIMD TensorDot over transposed ulong columns vs jagged scalar). Measured |
| 15 | +// in a single run, so the ratio is the real end-to-end effect of the matmul change. |
| 16 | +[SimpleJob(RuntimeMoniker.Net10_0)] |
| 17 | +[HideColumns("Error", "StdDev", "RatioSD")] |
| 18 | +public class EndToEndDecodeBenchmark |
| 19 | +{ |
| 20 | + private string _sig64 = default!; |
| 21 | + private ulong[] _transposed64 = default!; |
| 22 | + private uint[][] _jagged64 = default!; |
| 23 | + |
| 24 | + [GlobalSetup] |
| 25 | + public void Setup() |
| 26 | + { |
| 27 | + var bytes = new byte[64]; |
| 28 | + Random.Shared.NextBytes(bytes); |
| 29 | + _sig64 = Base58.Bitcoin.Encode(bytes); |
| 30 | + |
| 31 | + // Production tables on this branch are transposed column-major ulong. |
| 32 | + _transposed64 = Base58BitcoinTables.DecodeTable64; |
| 33 | + |
| 34 | + // Rebuild the original jagged row-major layout: jagged[i][j] = transposed[j*IntermediateSz64 + i]. |
| 35 | + int rows = Base58BitcoinTables.IntermediateSz64; |
| 36 | + int cols = Base58BitcoinTables.BinarySz64; |
| 37 | + _jagged64 = new uint[rows][]; |
| 38 | + for (int i = 0; i < rows; i++) |
| 39 | + { |
| 40 | + _jagged64[i] = new uint[cols]; |
| 41 | + for (int j = 0; j < cols; j++) |
| 42 | + { |
| 43 | + _jagged64[i][j] = (uint)_transposed64[j * rows + i]; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Correctness: both matmul variants must reproduce the production decode exactly. |
| 48 | + Span<byte> a = stackalloc byte[64]; |
| 49 | + Span<byte> b = stackalloc byte[64]; |
| 50 | + DecodeFull64(_sig64, a, useSimd: true); |
| 51 | + DecodeFull64(_sig64, b, useSimd: false); |
| 52 | + byte[] expected = Base58.Bitcoin.Decode(_sig64); |
| 53 | + if (!a.SequenceEqual(expected) || !b.SequenceEqual(expected)) |
| 54 | + { |
| 55 | + throw new InvalidOperationException("DecodeFull64 does not match production Decode"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + [Benchmark(Baseline = true)] |
| 60 | + public int Full_Jagged() |
| 61 | + { |
| 62 | + Span<byte> dest = stackalloc byte[64]; |
| 63 | + return DecodeFull64(_sig64, dest, useSimd: false); |
| 64 | + } |
| 65 | + |
| 66 | + [Benchmark] |
| 67 | + public int Full_Simd() |
| 68 | + { |
| 69 | + Span<byte> dest = stackalloc byte[64]; |
| 70 | + return DecodeFull64(_sig64, dest, useSimd: true); |
| 71 | + } |
| 72 | + |
| 73 | + [SkipLocalsInit] |
| 74 | + private int DecodeFull64(ReadOnlySpan<char> encoded, Span<byte> destination, bool useSimd) |
| 75 | + { |
| 76 | + int charCount = encoded.Length; |
| 77 | + |
| 78 | + Span<byte> rawBase58 = stackalloc byte[Base58BitcoinTables.Raw58Sz64]; |
| 79 | + ReadOnlySpan<byte> decodeTable = BitcoinAlphabet.DecodeTable; |
| 80 | + |
| 81 | + int prepend0 = Base58BitcoinTables.Raw58Sz64 - charCount; |
| 82 | + for (int j = 0; j < Base58BitcoinTables.Raw58Sz64; j++) |
| 83 | + { |
| 84 | + if (j < prepend0) |
| 85 | + { |
| 86 | + rawBase58[j] = 0; |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + int c = encoded[j - prepend0]; |
| 91 | + if ((uint)c >= 128 || decodeTable[c] == 255) |
| 92 | + { |
| 93 | + ThrowHelper.ThrowInvalidCharacter((char)c); |
| 94 | + } |
| 95 | + |
| 96 | + rawBase58[j] = decodeTable[c]; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + Span<ulong> intermediate = stackalloc ulong[Base58BitcoinTables.IntermediateSz64]; |
| 101 | + for (int i = 0; i < Base58BitcoinTables.IntermediateSz64; i++) |
| 102 | + { |
| 103 | + intermediate[i] = (ulong)rawBase58[5 * i + 0] * 11316496UL + |
| 104 | + (ulong)rawBase58[5 * i + 1] * 195112UL + |
| 105 | + (ulong)rawBase58[5 * i + 2] * 3364UL + |
| 106 | + (ulong)rawBase58[5 * i + 3] * 58UL + |
| 107 | + (ulong)rawBase58[5 * i + 4] * 1UL; |
| 108 | + } |
| 109 | + |
| 110 | + Span<ulong> binary = stackalloc ulong[Base58BitcoinTables.BinarySz64]; |
| 111 | + if (useSimd) |
| 112 | + { |
| 113 | + var table = _transposed64; |
| 114 | + int rows = Base58BitcoinTables.IntermediateSz64; |
| 115 | + for (int j = 0; j < Base58BitcoinTables.BinarySz64; j++) |
| 116 | + { |
| 117 | + binary[j] = TensorDot(intermediate, table.AsSpan(j * rows, rows)); |
| 118 | + } |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + var table = _jagged64; |
| 123 | + for (int j = 0; j < Base58BitcoinTables.BinarySz64; j++) |
| 124 | + { |
| 125 | + ulong acc = 0UL; |
| 126 | + for (int i = 0; i < Base58BitcoinTables.IntermediateSz64; i++) |
| 127 | + { |
| 128 | + acc += intermediate[i] * table[i][j]; |
| 129 | + } |
| 130 | + |
| 131 | + binary[j] = acc; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + for (int i = Base58BitcoinTables.BinarySz64 - 1; i > 0; i--) |
| 136 | + { |
| 137 | + binary[i - 1] += binary[i] >> 32; |
| 138 | + binary[i] &= 0xFFFFFFFFUL; |
| 139 | + } |
| 140 | + |
| 141 | + if (binary[0] > 0xFFFFFFFFUL) |
| 142 | + { |
| 143 | + return -1; |
| 144 | + } |
| 145 | + |
| 146 | + int outputLeadingZeros = 0; |
| 147 | + for (int i = 0; i < Base58BitcoinTables.BinarySz64; i++) |
| 148 | + { |
| 149 | + uint v = (uint)binary[i]; |
| 150 | + if (v != 0) |
| 151 | + { |
| 152 | + outputLeadingZeros += BitOperations.LeadingZeroCount(v) / 8; |
| 153 | + break; |
| 154 | + } |
| 155 | + outputLeadingZeros += 4; |
| 156 | + } |
| 157 | + |
| 158 | + int inputLeadingOnes = Base58.CountLeadingCharacters(encoded, '1'); |
| 159 | + if (outputLeadingZeros != inputLeadingOnes) |
| 160 | + { |
| 161 | + return -1; |
| 162 | + } |
| 163 | + |
| 164 | + for (int i = 0; i < Base58BitcoinTables.BinarySz64; i++) |
| 165 | + { |
| 166 | + BinaryPrimitives.WriteUInt32BigEndian(destination.Slice(i * sizeof(uint), sizeof(uint)), (uint)binary[i]); |
| 167 | + } |
| 168 | + |
| 169 | + return 64; |
| 170 | + } |
| 171 | + |
| 172 | + private static ulong TensorDot(ReadOnlySpan<ulong> x, ReadOnlySpan<ulong> y) |
| 173 | + { |
| 174 | + ref ulong xr = ref MemoryMarshal.GetReference(x); |
| 175 | + ref ulong yr = ref MemoryMarshal.GetReference(y); |
| 176 | + int len = x.Length; |
| 177 | + int i = 0; |
| 178 | + ulong sum = 0UL; |
| 179 | + |
| 180 | + if (Vector256.IsHardwareAccelerated && len >= Vector256<ulong>.Count) |
| 181 | + { |
| 182 | + Vector256<ulong> acc = Vector256<ulong>.Zero; |
| 183 | + int upper = len - Vector256<ulong>.Count; |
| 184 | + for (; i <= upper; i += Vector256<ulong>.Count) |
| 185 | + { |
| 186 | + acc += Vector256.LoadUnsafe(ref xr, (nuint)i) * Vector256.LoadUnsafe(ref yr, (nuint)i); |
| 187 | + } |
| 188 | + |
| 189 | + sum += Vector256.Sum(acc); |
| 190 | + } |
| 191 | + else if (Vector128.IsHardwareAccelerated && len >= Vector128<ulong>.Count) |
| 192 | + { |
| 193 | + Vector128<ulong> acc = Vector128<ulong>.Zero; |
| 194 | + int upper = len - Vector128<ulong>.Count; |
| 195 | + for (; i <= upper; i += Vector128<ulong>.Count) |
| 196 | + { |
| 197 | + acc += Vector128.LoadUnsafe(ref xr, (nuint)i) * Vector128.LoadUnsafe(ref yr, (nuint)i); |
| 198 | + } |
| 199 | + |
| 200 | + sum += Vector128.Sum(acc); |
| 201 | + } |
| 202 | + |
| 203 | + for (; i < len; i++) |
| 204 | + { |
| 205 | + sum += Unsafe.Add(ref xr, i) * Unsafe.Add(ref yr, i); |
| 206 | + } |
| 207 | + |
| 208 | + return sum; |
| 209 | + } |
| 210 | +} |
0 commit comments