Skip to content

Commit 913b71e

Browse files
committed
refactor: slice to the fixed lengths in the bitcoin encode fast paths
1 parent 059b6d3 commit 913b71e

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

src/Base58Encoding/Base58.Encode.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ private static int EncodeBitcoin32FastToBytes(ReadOnlySpan<byte> data, Span<byte
243243
[SkipLocalsInit]
244244
private static int ComputeBitcoin32FastRaw(ReadOnlySpan<byte> data, Span<byte> rawBase58)
245245
{
246+
// Span params hide their length. Re-slicing to the fixed sizes folds away ~50 bounds checks
247+
// below: 30% less code, perf-neutral.
248+
data = data[..(Base58BitcoinTables.BinarySz32 * sizeof(uint))];
249+
rawBase58 = rawBase58[..Base58BitcoinTables.Raw58Sz32];
250+
246251
// Convert 32 bytes to 8 uint32 limbs (big-endian)
247252
Span<uint> binary = stackalloc uint[Base58BitcoinTables.BinarySz32];
248253
for (int i = 0; i < Base58BitcoinTables.BinarySz32; i++)
@@ -351,6 +356,10 @@ private static int EncodeBitcoin64FastToBytes(ReadOnlySpan<byte> data, Span<byte
351356
[SkipLocalsInit]
352357
private static int ComputeBitcoin64FastRaw(ReadOnlySpan<byte> data, Span<byte> rawBase58)
353358
{
359+
// See ComputeBitcoin32FastRaw.
360+
data = data[..(Base58BitcoinTables.BinarySz64 * sizeof(uint))];
361+
rawBase58 = rawBase58[..Base58BitcoinTables.Raw58Sz64];
362+
354363
// Convert 64 bytes to 16 uint32 limbs (big-endian)
355364
Span<uint> binary = stackalloc uint[Base58BitcoinTables.BinarySz64];
356365
for (int i = 0; i < Base58BitcoinTables.BinarySz64; i++)

0 commit comments

Comments
 (0)