Skip to content

Commit e6bd5b6

Browse files
committed
not sure
1 parent 9bd7981 commit e6bd5b6

File tree

3 files changed

+93
-217
lines changed

3 files changed

+93
-217
lines changed

benchmark/Benchmark.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ public Config()
152152
}
153153
}
154154
// Parameters and variables for real data
155-
[Params(
156-
//@"data/email/" //,
155+
[Params(
156+
//@"data/email/" //,
157157
@"data/dns/swedenzonebase.txt"
158158
)]
159159
#pragma warning disable CA1051

src/Base64Scalar.cs

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ public static int MaximalBinaryLengthFromBase64Scalar<T>(ReadOnlySpan<T> input)
6464
public unsafe static OperationStatus DecodeFromBase64Scalar(ReadOnlySpan<byte> source, Span<byte> dest, out int bytesConsumed, out int bytesWritten, bool isUrl = false)
6565
{
6666

67-
uint[] d0 = isUrl ? Base64Url.d0 : Base64Default.d0;
68-
uint[] d1 = isUrl ? Base64Url.d1 : Base64Default.d1;
69-
uint[] d2 = isUrl ? Base64Url.d2 : Base64Default.d2;
70-
uint[] d3 = isUrl ? Base64Url.d3 : Base64Default.d3;
71-
7267
int length = source.Length;
7368

7469
fixed (byte* srcInit = source)
@@ -89,7 +84,7 @@ public unsafe static OperationStatus DecodeFromBase64Scalar(ReadOnlySpan<byte> s
8984
{
9085
// fastpath
9186
while (src + 4 <= srcEnd &&
92-
(x = d0[*src] | d1[src[1]] | d2[src[2]] | d3[src[3]]) < 0x01FFFFFF)
87+
(x = isUrl ? Base64Url.GetD(src) : Base64Default.GetD(src)) < 0x01FFFFFF)
9388
{
9489
if (MatchSystem(Endianness.BIG))
9590
{
@@ -208,11 +203,6 @@ public static bool IsValidBase64Index(char b)
208203
public unsafe static OperationStatus DecodeFromBase64Scalar(ReadOnlySpan<char> source, Span<byte> dest, out int bytesConsumed, out int bytesWritten, bool isUrl = false)
209204
{
210205

211-
uint[] d0 = isUrl ? Base64Url.d0 : Base64Default.d0;
212-
uint[] d1 = isUrl ? Base64Url.d1 : Base64Default.d1;
213-
uint[] d2 = isUrl ? Base64Url.d2 : Base64Default.d2;
214-
uint[] d3 = isUrl ? Base64Url.d3 : Base64Default.d3;
215-
216206
int length = source.Length;
217207

218208
fixed (char* srcInit = source)
@@ -234,9 +224,7 @@ public unsafe static OperationStatus DecodeFromBase64Scalar(ReadOnlySpan<char> s
234224
{
235225
// fastpath
236226
while (src + 4 <= srcEnd &&
237-
IsValidBase64Index(*src) && IsValidBase64Index(src[1]) &&
238-
IsValidBase64Index(src[2]) && IsValidBase64Index(src[3]) &&
239-
(x = d0[*src] | d1[src[1]] | d2[src[2]] | d3[src[3]]) < 0x01FFFFFF)
227+
(x = isUrl ? Base64Url.GetD(src) : Base64Default.GetD(src)) < 0x01FFFFFF)
240228
{
241229
if (MatchSystem(Endianness.BIG))
242230
{
@@ -358,18 +346,13 @@ public unsafe static OperationStatus DecodeFromBase64Scalar(ReadOnlySpan<char> s
358346
// like DecodeFromBase64Scalar, but it will not write past the end of the ouput buffer.
359347
public unsafe static OperationStatus SafeDecodeFromBase64Scalar(ReadOnlySpan<byte> source, Span<byte> dest, out int bytesConsumed, out int bytesWritten, bool isUrl = false)
360348
{
361-
362-
363-
uint[] d0 = isUrl ? Base64Url.d0 : Base64Default.d0;
364-
uint[] d1 = isUrl ? Base64Url.d1 : Base64Default.d1;
365-
uint[] d2 = isUrl ? Base64Url.d2 : Base64Default.d2;
366-
uint[] d3 = isUrl ? Base64Url.d3 : Base64Default.d3;
367-
368349
int length = source.Length;
350+
Span<byte> buffer = [0, 0, 0, 0];
369351

370352
// Define pointers within the fixed blocks
371353
fixed (byte* srcInit = source)
372354
fixed (byte* dstInit = dest)
355+
fixed (byte* bufferPtr = buffer)
373356
{
374357
byte* srcEnd = srcInit + length;
375358
byte* src = srcInit;
@@ -382,13 +365,12 @@ public unsafe static OperationStatus SafeDecodeFromBase64Scalar(ReadOnlySpan<byt
382365
int idx;
383366
// Should be
384367
// Span<byte> buffer = stackalloc byte[4];
385-
Span<byte> buffer = [0, 0, 0, 0];
386368

387369
while (true)
388370
{
389371
// fastpath
390372
while (src + 4 <= srcEnd &&
391-
(x = d0[*src] | d1[src[1]] | d2[src[2]] | d3[src[3]]) < 0x01FFFFFF)
373+
(x = isUrl ? Base64Url.GetD(src) : Base64Default.GetD(src)) < 0x01FFFFFF)
392374
{
393375

394376

@@ -402,7 +384,7 @@ public unsafe static OperationStatus SafeDecodeFromBase64Scalar(ReadOnlySpan<byt
402384
bytesWritten = (int)(dst - dstInit);
403385
return OperationStatus.DestinationTooSmall;
404386
}
405-
Marshal.Copy(buffer, 0, (IntPtr)dst, 3); // optimization opportunity: copy 4 bytes
387+
Buffer.MemoryCopy(bufferPtr, dst, 3, 3);
406388
dst += 3;
407389
src += 4;
408390
}
@@ -528,16 +510,15 @@ public unsafe static OperationStatus SafeDecodeFromBase64Scalar(ReadOnlySpan<byt
528510
public unsafe static OperationStatus SafeDecodeFromBase64Scalar(ReadOnlySpan<char> source, Span<byte> dest, out int bytesConsumed, out int bytesWritten, bool isUrl = false)
529511
{
530512

531-
uint[] d0 = isUrl ? Base64Url.d0 : Base64Default.d0;
532-
uint[] d1 = isUrl ? Base64Url.d1 : Base64Default.d1;
533-
uint[] d2 = isUrl ? Base64Url.d2 : Base64Default.d2;
534-
uint[] d3 = isUrl ? Base64Url.d3 : Base64Default.d3;
535-
536513
int length = source.Length;
537514

515+
// Should be
516+
// Span<byte> buffer = stackalloc byte[4];
517+
Span<byte> buffer = [0, 0, 0, 0];
538518
// Define pointers within the fixed blocks
539519
fixed (char* srcInit = source)
540520
fixed (byte* dstInit = dest)
521+
fixed (byte* bufferPtr = buffer)
541522

542523
{
543524
char* srcEnd = srcInit + length;
@@ -549,18 +530,13 @@ public unsafe static OperationStatus SafeDecodeFromBase64Scalar(ReadOnlySpan<cha
549530
uint x;
550531
uint triple;
551532
int idx;
552-
// Should be
553-
// Span<byte> buffer = stackalloc byte[4];
554-
Span<byte> buffer = [0, 0, 0, 0];
555533

556534
while (true)
557535
{
558536
// fastpath
559537
while (src + 4 <= srcEnd &&
560-
(x = d0[*src] | d1[src[1]] | d2[src[2]] | d3[src[3]]) < 0x01FFFFFF)
538+
(x = isUrl ? Base64Url.GetD(src) : Base64Default.GetD(src)) < 0x01FFFFFF)
561539
{
562-
563-
564540
if (MatchSystem(Endianness.BIG))
565541
{
566542
x = BinaryPrimitives.ReverseEndianness(x);
@@ -571,7 +547,8 @@ public unsafe static OperationStatus SafeDecodeFromBase64Scalar(ReadOnlySpan<cha
571547
bytesWritten = (int)(dst - dstInit);
572548
return OperationStatus.DestinationTooSmall;
573549
}
574-
Marshal.Copy(buffer, 0, (IntPtr)dst, 3); // optimization opportunity: copy 4 bytes
550+
Buffer.MemoryCopy(bufferPtr, dst, 3, 3);
551+
//Marshal.Copy(buffer, 0, (IntPtr)dst, 3); // optimization opportunity: copy 4 bytes
575552
dst += 3;
576553
src += 4;
577554
}

0 commit comments

Comments
 (0)