GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor: read the span tables in place in tests and benchmarks · unsafePtr/Base58Encoding@e4a8b46

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • Base58Encoding.Benchmarks
      • EndToEndDecodeBenchmark.cs
      • EndToEndEncodeBenchmark.cs
    • Base58Encoding.Tests
      • VectorMathTests.cs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/Base58Encoding.Benchmarks/EndToEndDecodeBenchmark.cs‎

Copy file name to clipboardExpand all lines: src/Base58Encoding.Benchmarks/EndToEndDecodeBenchmark.cs
+5-7Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace Base58Encoding.Benchmarks;
1818
public class EndToEndDecodeBenchmark
1919
{
2020
private string _sig64 = default!;
21-
private ulong[] _transposed64 = default!;
2221
private uint[][] _jagged64 = default!;
2322

2423
[GlobalSetup]
@@ -28,10 +27,9 @@ public void Setup()
2827
Random.Shared.NextBytes(bytes);
2928
_sig64 = Base58.Bitcoin.Encode(bytes);
3029

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.
3431
// Rebuild the original jagged row-major layout: jagged[i][j] = transposed[j*IntermediateSz64 + i].
32+
ReadOnlySpan<ulong> transposed = Base58BitcoinTables.DecodeTable64;
3533
int rows = Base58BitcoinTables.IntermediateSz64;
3634
int cols = Base58BitcoinTables.BinarySz64;
3735
_jagged64 = new uint[rows][];
@@ -40,7 +38,7 @@ public void Setup()
4038
_jagged64[i] = new uint[cols];
4139
for (int j = 0; j < cols; j++)
4240
{
43-
_jagged64[i][j] = (uint)_transposed64[j * rows + i];
41+
_jagged64[i][j] = (uint)transposed[j * rows + i];
4442
}
4543
}
4644

@@ -110,11 +108,11 @@ private int DecodeFull64(ReadOnlySpan<char> encoded, Span<byte> destination, boo
110108
Span<ulong> binary = stackalloc ulong[Base58BitcoinTables.BinarySz64];
111109
if (useSimd)
112110
{
113-
var table = _transposed64;
111+
ReadOnlySpan<ulong> table = Base58BitcoinTables.DecodeTable64;
114112
int rows = Base58BitcoinTables.IntermediateSz64;
115113
for (int j = 0; j < Base58BitcoinTables.BinarySz64; j++)
116114
{
117-
binary[j] = TensorDot(intermediate, table.AsSpan(j * rows, rows));
115+
binary[j] = TensorDot(intermediate, table.Slice(j * rows, rows));
118116
}
119117
}
120118
else
Collapse file

‎src/Base58Encoding.Benchmarks/EndToEndEncodeBenchmark.cs‎

Copy file name to clipboardExpand all lines: src/Base58Encoding.Benchmarks/EndToEndEncodeBenchmark.cs
+8-6Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ private interface IMatmulMode { }
3232

3333
private byte[] _data = default!;
3434
private uint[][] _jagged = default!;
35-
private ulong[] _rowMajor = default!;
3635

3736
[GlobalSetup]
3837
public void Setup()
@@ -47,14 +46,14 @@ public void Setup()
4746
int binarySz = Size == 32 ? Base58BitcoinTables.BinarySz32 : Base58BitcoinTables.BinarySz64;
4847
int rowLen = (Size == 32 ? Base58BitcoinTables.IntermediateSz32 : Base58BitcoinTables.IntermediateSz64) - 1;
4948

50-
_rowMajor = (Size == 32 ? Base58BitcoinTables.EncodeTable32RowMajor : Base58BitcoinTables.EncodeTable64RowMajor).ToArray();
49+
ReadOnlySpan<ulong> rowMajor = RowMajor(Size);
5150
_jagged = new uint[binarySz][];
5251
for (int i = 0; i < binarySz; i++)
5352
{
5453
_jagged[i] = new uint[rowLen];
5554
for (int j = 0; j < rowLen; j++)
5655
{
57-
_jagged[i][j] = (uint)_rowMajor[i * rowLen + j];
56+
_jagged[i][j] = (uint)rowMajor[i * rowLen + j];
5857
}
5958
}
6059

@@ -184,14 +183,14 @@ private void AccumulateRows<TMode>(
184183
return;
185184
}
186185

187-
ulong[] rowMajor = _rowMajor;
186+
ReadOnlySpan<ulong> rowMajor = RowMajor(Size);
188187
Span<ulong> target = intermediate.Slice(1, rowLen);
189188

190189
if (typeof(TMode) == typeof(TensorMode))
191190
{
192191
for (int i = startRow; i < endRow; i++)
193192
{
194-
ReadOnlySpan<ulong> row = rowMajor.AsSpan(i * rowLen, rowLen);
193+
ReadOnlySpan<ulong> row = rowMajor.Slice(i * rowLen, rowLen);
195194
// destination[k] = (row[k] * binary[i]) + target[k]; addend and destination are the
196195
// same span (allowed: they begin at the same location).
197196
TensorPrimitives.MultiplyAdd(row, (ulong)binary[i], target, target);
@@ -203,11 +202,14 @@ private void AccumulateRows<TMode>(
203202
// SimdMode
204203
for (int i = startRow; i < endRow; i++)
205204
{
206-
ReadOnlySpan<ulong> row = rowMajor.AsSpan(i * rowLen, rowLen);
205+
ReadOnlySpan<ulong> row = rowMajor.Slice(i * rowLen, rowLen);
207206
MultiplyAddSimd(row, binary[i], target);
208207
}
209208
}
210209

210+
private static ReadOnlySpan<ulong> RowMajor(int size) =>
211+
size == 32 ? Base58BitcoinTables.EncodeTable32RowMajor : Base58BitcoinTables.EncodeTable64RowMajor;
212+
211213
// acc[k] += x[k] * scale, widest available width first, then a scalar tail / fallback.
212214
private static void MultiplyAddSimd(ReadOnlySpan<ulong> x, ulong scale, Span<ulong> acc)
213215
{
Collapse file

‎src/Base58Encoding.Tests/VectorMathTests.cs‎

Copy file name to clipboardExpand all lines: src/Base58Encoding.Tests/VectorMathTests.cs
+13-14Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,23 @@ public void Kernels_HandleBoundaryOperands(ulong a, ulong b)
147147
public void MultiplyWidening32Precondition_HoldsForAllProductionOperands()
148148
{
149149
// 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);
164154

165155
// Limb side: an intermediate limb is at most 58^5 - 1, which is below 2^32.
166156
ulong maxLimb = (57UL * 11316496UL) + (57UL * 195112UL) + (57UL * 3364UL) + (57UL * 58UL) + 57UL;
167157
Assert.Equal(Base58BitcoinTables.R1Div - 1UL, maxLimb);
168158
Assert.True(maxLimb < (ulong)OperandLimit);
169159
}
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+
}
170169
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.