GeezerVison is showing github.com — refactor: read the span tables in place in tests and benchmarks · unsafePtr/Base58Encoding@e4a8b46
File treeExpand file treeCollapse file tree Open diff view settings Base58Encoding.Benchmarks
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/Base58Encoding.Benchmarks/EndToEndDecodeBenchmark.cs +5-7Lines changed: 5 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,7 +18,6 @@ namespace Base58Encoding.Benchmarks; | 18 | 18 | public class EndToEndDecodeBenchmark | 19 | 19 | { | 20 | 20 | private string _sig64 = default!; | 21 | | - private ulong[] _transposed64 = default!; | 22 | 21 | private uint[][] _jagged64 = default!; | 23 | 22 |
| 24 | 23 | [GlobalSetup] | @@ -28,10 +27,9 @@ public void Setup() | 28 | 27 | Random.Shared.NextBytes(bytes); | 29 | 28 | _sig64 = Base58.Bitcoin.Encode(bytes); | 30 | 29 |
| 31 | | - // Production tables on this branch are transposed column-major ulong. | 32 | | - _transposed64 = Base58BitcoinTables.DecodeTable64.ToArray(); | 33 | | - | | 30 | + // The production table is transposed column-major ulong. | 34 | 31 | // Rebuild the original jagged row-major layout: jagged[i][j] = transposed[j*IntermediateSz64 + i]. | | 32 | + ReadOnlySpan<ulong> transposed = Base58BitcoinTables.DecodeTable64; | 35 | 33 | int rows = Base58BitcoinTables.IntermediateSz64; | 36 | 34 | int cols = Base58BitcoinTables.BinarySz64; | 37 | 35 | _jagged64 = new uint[rows][]; | @@ -40,7 +38,7 @@ public void Setup() | 40 | 38 | _jagged64[i] = new uint[cols]; | 41 | 39 | for (int j = 0; j < cols; j++) | 42 | 40 | { | 43 | | - _jagged64[i][j] = (uint)_transposed64[j * rows + i]; | | 41 | + _jagged64[i][j] = (uint)transposed[j * rows + i]; | 44 | 42 | } | 45 | 43 | } | 46 | 44 |
| @@ -110,11 +108,11 @@ private int DecodeFull64(ReadOnlySpan<char> encoded, Span<byte> destination, boo | 110 | 108 | Span<ulong> binary = stackalloc ulong[Base58BitcoinTables.BinarySz64]; | 111 | 109 | if (useSimd) | 112 | 110 | { | 113 | | - var table = _transposed64; | | 111 | + ReadOnlySpan<ulong> table = Base58BitcoinTables.DecodeTable64; | 114 | 112 | int rows = Base58BitcoinTables.IntermediateSz64; | 115 | 113 | for (int j = 0; j < Base58BitcoinTables.BinarySz64; j++) | 116 | 114 | { | 117 | | - binary[j] = TensorDot(intermediate, table.AsSpan(j * rows, rows)); | | 115 | + binary[j] = TensorDot(intermediate, table.Slice(j * rows, rows)); | 118 | 116 | } | 119 | 117 | } | 120 | 118 | else | |
Collapse file Copy file name to clipboardExpand all lines: src/Base58Encoding.Benchmarks/EndToEndEncodeBenchmark.cs +8-6Lines changed: 8 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,7 +32,6 @@ private interface IMatmulMode { } | 32 | 32 |
| 33 | 33 | private byte[] _data = default!; | 34 | 34 | private uint[][] _jagged = default!; | 35 | | - private ulong[] _rowMajor = default!; | 36 | 35 |
| 37 | 36 | [GlobalSetup] | 38 | 37 | public void Setup() | @@ -47,14 +46,14 @@ public void Setup() | 47 | 46 | int binarySz = Size == 32 ? Base58BitcoinTables.BinarySz32 : Base58BitcoinTables.BinarySz64; | 48 | 47 | int rowLen = (Size == 32 ? Base58BitcoinTables.IntermediateSz32 : Base58BitcoinTables.IntermediateSz64) - 1; | 49 | 48 |
| 50 | | - _rowMajor = (Size == 32 ? Base58BitcoinTables.EncodeTable32RowMajor : Base58BitcoinTables.EncodeTable64RowMajor).ToArray(); | | 49 | + ReadOnlySpan<ulong> rowMajor = RowMajor(Size); | 51 | 50 | _jagged = new uint[binarySz][]; | 52 | 51 | for (int i = 0; i < binarySz; i++) | 53 | 52 | { | 54 | 53 | _jagged[i] = new uint[rowLen]; | 55 | 54 | for (int j = 0; j < rowLen; j++) | 56 | 55 | { | 57 | | - _jagged[i][j] = (uint)_rowMajor[i * rowLen + j]; | | 56 | + _jagged[i][j] = (uint)rowMajor[i * rowLen + j]; | 58 | 57 | } | 59 | 58 | } | 60 | 59 |
| @@ -184,14 +183,14 @@ private void AccumulateRows<TMode>( | 184 | 183 | return; | 185 | 184 | } | 186 | 185 |
| 187 | | - ulong[] rowMajor = _rowMajor; | | 186 | + ReadOnlySpan<ulong> rowMajor = RowMajor(Size); | 188 | 187 | Span<ulong> target = intermediate.Slice(1, rowLen); | 189 | 188 |
| 190 | 189 | if (typeof(TMode) == typeof(TensorMode)) | 191 | 190 | { | 192 | 191 | for (int i = startRow; i < endRow; i++) | 193 | 192 | { | 194 | | - ReadOnlySpan<ulong> row = rowMajor.AsSpan(i * rowLen, rowLen); | | 193 | + ReadOnlySpan<ulong> row = rowMajor.Slice(i * rowLen, rowLen); | 195 | 194 | // destination[k] = (row[k] * binary[i]) + target[k]; addend and destination are the | 196 | 195 | // same span (allowed: they begin at the same location). | 197 | 196 | TensorPrimitives.MultiplyAdd(row, (ulong)binary[i], target, target); | @@ -203,11 +202,14 @@ private void AccumulateRows<TMode>( | 203 | 202 | // SimdMode | 204 | 203 | for (int i = startRow; i < endRow; i++) | 205 | 204 | { | 206 | | - ReadOnlySpan<ulong> row = rowMajor.AsSpan(i * rowLen, rowLen); | | 205 | + ReadOnlySpan<ulong> row = rowMajor.Slice(i * rowLen, rowLen); | 207 | 206 | MultiplyAddSimd(row, binary[i], target); | 208 | 207 | } | 209 | 208 | } | 210 | 209 |
| | 210 | + private static ReadOnlySpan<ulong> RowMajor(int size) => | | 211 | + size == 32 ? Base58BitcoinTables.EncodeTable32RowMajor : Base58BitcoinTables.EncodeTable64RowMajor; | | 212 | + | 211 | 213 | // acc[k] += x[k] * scale, widest available width first, then a scalar tail / fallback. | 212 | 214 | private static void MultiplyAddSimd(ReadOnlySpan<ulong> x, ulong scale, Span<ulong> acc) | 213 | 215 | { | |
Collapse file Copy file name to clipboardExpand all lines: src/Base58Encoding.Tests/VectorMathTests.cs +13-14Lines changed: 13 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -147,24 +147,23 @@ public void Kernels_HandleBoundaryOperands(ulong a, ulong b) | 147 | 147 | public void MultiplyWidening32Precondition_HoldsForAllProductionOperands() | 148 | 148 | { | 149 | 149 | // Table side. | 150 | | - foreach ((string name, ulong[] table) in new (string, ulong[])[] | 151 | | - { | 152 | | - (nameof(Base58BitcoinTables.EncodeTable32RowMajor), Base58BitcoinTables.EncodeTable32RowMajor.ToArray()), | 153 | | - (nameof(Base58BitcoinTables.EncodeTable64RowMajor), Base58BitcoinTables.EncodeTable64RowMajor.ToArray()), | 154 | | - (nameof(Base58BitcoinTables.DecodeTable32), Base58BitcoinTables.DecodeTable32.ToArray()), | 155 | | - (nameof(Base58BitcoinTables.DecodeTable64), Base58BitcoinTables.DecodeTable64.ToArray()), | 156 | | - }) | 157 | | - { | 158 | | - for (int i = 0; i < table.Length; i++) | 159 | | - { | 160 | | - Assert.True(table[i] < (ulong)OperandLimit, | 161 | | - $"{name}[{i}] = {table[i]} exceeds 2^32; MultiplyWidening32 would truncate it."); | 162 | | - } | 163 | | - } | | 150 | + AssertOperandsFit(nameof(Base58BitcoinTables.EncodeTable32RowMajor), Base58BitcoinTables.EncodeTable32RowMajor); | | 151 | + AssertOperandsFit(nameof(Base58BitcoinTables.EncodeTable64RowMajor), Base58BitcoinTables.EncodeTable64RowMajor); | | 152 | + AssertOperandsFit(nameof(Base58BitcoinTables.DecodeTable32), Base58BitcoinTables.DecodeTable32); | | 153 | + AssertOperandsFit(nameof(Base58BitcoinTables.DecodeTable64), Base58BitcoinTables.DecodeTable64); | 164 | 154 |
| 165 | 155 | // Limb side: an intermediate limb is at most 58^5 - 1, which is below 2^32. | 166 | 156 | ulong maxLimb = (57UL * 11316496UL) + (57UL * 195112UL) + (57UL * 3364UL) + (57UL * 58UL) + 57UL; | 167 | 157 | Assert.Equal(Base58BitcoinTables.R1Div - 1UL, maxLimb); | 168 | 158 | Assert.True(maxLimb < (ulong)OperandLimit); | 169 | 159 | } | | 160 | + | | 161 | + private static void AssertOperandsFit(string name, ReadOnlySpan<ulong> table) | | 162 | + { | | 163 | + for (int i = 0; i < table.Length; i++) | | 164 | + { | | 165 | + Assert.True(table[i] < (ulong)OperandLimit, | | 166 | + $"{name}[{i}] = {table[i]} exceeds 2^32; MultiplyWidening32 would truncate it."); | | 167 | + } | | 168 | + } | 170 | 169 | } |
|