Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add_UTF16_support + fix/test array OutOfIndex error #25

Merged
merged 23 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 76 additions & 3 deletions benchmark/Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,27 @@ public unsafe void RunScalarDecodingBenchmarkUTF8(string[] data, int[] lengths)
throw new Exception("Error");
}
}
}

}

public unsafe void RunScalarDecodingBenchmarkUTF16(string[] data, int[] lengths)
{
for (int i = 0; i < FileContent.Length; i++)
{
string s = FileContent[i];
char[] base64 = s.ToCharArray();
byte[] dataoutput = output[i];
int bytesConsumed = 0;
int bytesWritten = 0;
SimdBase64.Base64.Base64WithWhiteSpaceToBinaryScalar(base64.AsSpan(), dataoutput, out bytesConsumed, out bytesWritten, false);
if (bytesWritten != lengths[i])
{
Console.WriteLine($"Error: {bytesWritten} != {lengths[i]}");
#pragma warning disable CA2201
throw new Exception("Error");
}
}
}

public unsafe void RunSSEDecodingBenchmarkUTF8(string[] data, int[] lengths)
{
for (int i = 0; i < FileContent.Length; i++)
Expand All @@ -275,13 +294,34 @@ public unsafe void RunSSEDecodingBenchmarkUTF8(string[] data, int[] lengths)
}
}

public unsafe void RunSSEDecodingBenchmarkUTF16(string[] data, int[] lengths)
{
for (int i = 0; i < FileContent.Length; i++)
{
string s = FileContent[i];
char[] base64 = s.ToCharArray();
Copy link
Member

@lemire lemire Aug 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect ToCharArray is allocating. I suggest trying...

        fixed (char* p = s)
        {
            // p now points to the first character of the string
        }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or possibly...

Span<char> span = s.AsSpan();

Copy link
Collaborator Author

@Nick-Nuon Nick-Nuon Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it:

The benchmarks show an improvement with using s.AsSpan() :

Eg Before:

Method FileName Mean Error StdDev Speed (GB/s)
SSEDecodingRealDataUTF8 data/dns/swedenzonebase.txt 13,676.2 us 4,527.54 us 248.17 us 2.57
SSEDecodingRealDataWithAllocUTF8 data/dns/swedenzonebase.txt 15,854.0 us 2,258.94 us 123.82 us 2.21
SSEDecodingRealDataUTF16 data/dns/swedenzonebase.txt 21,441.2 us 11,428.57 us 626.44 us 1.64
SSEDecodingRealDataWithAllocUTF16 data/dns/swedenzonebase.txt 23,101.2 us 1,199.81 us 65.77 us 1.52
SSEDecodingRealDataUTF8 data/email/ 303.5 us 94.11 us 5.16 us 6.51
SSEDecodingRealDataWithAllocUTF8 data/email/ 502.2 us 188.52 us 10.33 us 3.94
SSEDecodingRealDataUTF16 data/email/ 1,231.8 us 794.61 us 43.56 us 1.60
SSEDecodingRealDataWithAllocUTF16 data/email/ 1,807.2 us 398.33 us 21.83 us 1.09
DotnetRuntimeSIMDBase64RealDataUTF8 data/dns/swedenzonebase.txt 11,092.6 us 1,009.68 us 55.34 us 3.16
DotnetRuntimeSIMDBase64RealDataWithAllocUTF8 data/dns/swedenzonebase.txt 11,840.5 us 88.56 us 4.85 us 2.96
DotnetRuntimeBase64RealDataUTF16 data/dns/swedenzonebase.txt 47,221.1 us 9,269.31 us 508.08 us .74
DotnetRuntimeSIMDBase64RealDataUTF8 data/email/ 434.1 us 10.26 us 0.56 us 4.55
DotnetRuntimeSIMDBase64RealDataWithAllocUTF8 data/email/ 585.7 us 637.26 us 34.93 us 3.37
DotnetRuntimeBase64RealDataUTF16 data/email/ 2,210.4 us 619.72 us 33.97 us .89

After:

Method FileName Mean Error StdDev Speed (GB/s)
SSEDecodingRealDataUTF8 data/dns/swedenzonebase.txt 13,609.2 us 598.55 us 32.81 us 2.58
SSEDecodingRealDataWithAllocUTF8 data/dns/swedenzonebase.txt 16,344.4 us 11,092.34 us 608.01 us 2.15
SSEDecodingRealDataUTF16 data/dns/swedenzonebase.txt 14,942.3 us 869.03 us 47.63 us 2.35
SSEDecodingRealDataWithAllocUTF16 data/dns/swedenzonebase.txt 24,094.8 us 4,199.09 us 230.17 us 1.46
SSEDecodingRealDataUTF8 data/email/ 343.1 us 67.03 us 3.67 us 5.76
SSEDecodingRealDataWithAllocUTF8 data/email/ 484.0 us 451.40 us 24.74 us 4.08
SSEDecodingRealDataUTF16 data/email/ 338.4 us 32.81 us 1.80 us 5.84
SSEDecodingRealDataWithAllocUTF16 data/email/ 1,762.7 us 311.41 us 17.07 us 1.12
DotnetRuntimeSIMDBase64RealDataUTF8 data/dns/swedenzonebase.txt 11,020.2 us 1,918.12 us 105.14 us 3.19
DotnetRuntimeSIMDBase64RealDataWithAllocUTF8 data/dns/swedenzonebase.txt 11,794.3 us 1,063.94 us 58.32 us 2.98
DotnetRuntimeBase64RealDataUTF16 data/dns/swedenzonebase.txt 47,170.9 us 2,594.95 us 142.24 us .74
DotnetRuntimeSIMDBase64RealDataUTF8 data/email/ 443.0 us 74.44 us 4.08 us 4.46
DotnetRuntimeSIMDBase64RealDataWithAllocUTF8 data/email/ 551.8 us 843.09 us 46.21 us 3.58
DotnetRuntimeBase64RealDataUTF16 data/email/ 2,270.3 us 777.69 us 42.63 us .87

byte[] dataoutput = output[i];
int bytesConsumed = 0;
int bytesWritten = 0;
SimdBase64.Base64.DecodeFromBase64SSE(base64.AsSpan(), dataoutput, out bytesConsumed, out bytesWritten, false);
if (bytesWritten != lengths[i])
{
Console.WriteLine($"Error: {bytesWritten} != {lengths[i]}");
#pragma warning disable CA2201
throw new Exception("Error");
}
}
}



public unsafe void RunSSEDecodingBenchmarkWithAllocUTF8(string[] data, int[] lengths)
{
for (int i = 0; i < FileContent.Length; i++)
{
//string s = FileContent[i];
byte[] base64 = input[i];
byte[] dataoutput = new byte[SimdBase64.Base64.MaximalBinaryLengthFromBase64Scalar(base64.AsSpan())];
byte[] dataoutput = new byte[SimdBase64.Base64.MaximalBinaryLengthFromBase64Scalar<byte>(base64.AsSpan())];
//byte[] dataoutput = output[i];
int bytesConsumed = 0;
int bytesWritten = 0;
Expand All @@ -295,6 +335,25 @@ public unsafe void RunSSEDecodingBenchmarkWithAllocUTF8(string[] data, int[] len
}
}

public unsafe void RunSSEDecodingBenchmarkWithAllocUTF16(string[] data, int[] lengths)
{
for (int i = 0; i < FileContent.Length; i++)
{
string s = FileContent[i];
char[] base64 = s.ToCharArray();
byte[] dataoutput = new byte[SimdBase64.Base64.MaximalBinaryLengthFromBase64Scalar<char>(base64.AsSpan())];
int bytesConsumed = 0;
int bytesWritten = 0;
SimdBase64.Base64.DecodeFromBase64SSE(base64.AsSpan(), dataoutput, out bytesConsumed, out bytesWritten, false);
if (bytesWritten != lengths[i])
{
Console.WriteLine($"Error: {bytesWritten} != {lengths[i]}");
#pragma warning disable CA2201
throw new Exception("Error");
}
}
}

[GlobalSetup]
public void Setup()
{
Expand Down Expand Up @@ -388,6 +447,20 @@ public unsafe void SSEDecodingRealDataWithAllocUTF8()
RunSSEDecodingBenchmarkWithAllocUTF8(FileContent, DecodedLengths);
}

[Benchmark]
[BenchmarkCategory("SSE")]
public unsafe void SSEDecodingRealDataUTF16()
{
RunSSEDecodingBenchmarkUTF16(FileContent, DecodedLengths);
}

[Benchmark]
[BenchmarkCategory("SSE")]
public unsafe void SSEDecodingRealDataWithAllocUTF16()
{
RunSSEDecodingBenchmarkWithAllocUTF16(FileContent, DecodedLengths);
}

}
public class Program
{
Expand Down
Loading