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

Use Base64.DecodeFromUtf8InPlace #2504

Merged
merged 2 commits into from
Apr 5, 2024
Merged

Conversation

keegan-caruso
Copy link
Contributor

Use Base64.DecodeFromUtf8InPlace for base64 decode

Description

Base64.DecodeFromUtf8InPlace is SIMD aware.

CPU: ~ -12% on token read time

before:

Method Mean Error StdDev Median P90 P95 P100 Gen0 Allocated
ReadJWS_FromMemory 3.487 us 0.0287 us 0.0612 us 3.465 us 3.601 us 3.604 us 3.605 us 0.1068 2.67 KB
ReadJWS_FromString 3.492 us 0.0101 us 0.0217 us 3.490 us 3.524 us 3.529 us 3.545 us 0.1068 2.67 KB

after:

Method Mean Error StdDev P90 P95 P100 Gen0 Allocated
ReadJWS_FromMemory 3.068 us 0.0139 us 0.0303 us 3.094 us 3.117 us 3.151 us 0.1068 2.67 KB
ReadJWS_FromString 3.076 us 0.0134 us 0.0292 us 3.113 us 3.121 us 3.127 us 0.1068 2.67 KB

Note: this was run with a cpu with the following intrinsics: AVX-512F+CD+BW+DQ+VL+VBMI

@keegan-caruso keegan-caruso changed the title Use Base64.DecodeFromUtf8InPlace for base64 decode Use Base64.DecodeFromUtf8InPlace Feb 28, 2024
byte[] output = ArrayPool<byte>.Shared.Rent(outputSize);
try
{
Base64UrlEncoding.Decode(strSpan, startIndex, length, output);
ReadOnlySpan<char> slice = strSpan.Slice(startIndex, length);
Base64UrlEncoder.UnsafeDecode(slice, output);
Copy link
Contributor

Choose a reason for hiding this comment

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

Base64UrlEncoding.Decode(..) was throwing ArgumentOutOfRangeException and ArgumentException exceptions. The new code path will not throw these exceptions. We might want to share this in the release notes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, good idea.

@keegan-caruso keegan-caruso force-pushed the kecaruso/buffers-base64 branch from 5e516ed to 38d9b18 Compare February 29, 2024 19:58
Copy link
Contributor

@sruke sruke left a comment

Choose a reason for hiding this comment

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

LGTM!

@keegan-caruso keegan-caruso force-pushed the kecaruso/buffers-base64 branch from 7440712 to 487d7ef Compare April 4, 2024 18:22
Copy link
Contributor

@pmaytak pmaytak left a comment

Choose a reason for hiding this comment

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

Approved with suggestions. The code looks like it matches the previous one (except using the DecodeFromUtf8InPlace). Main suggestions are about refactoring a bit of the duplicate code. This class was a bit difficult to review since there are similarly named methods, duplicate code, and .NET6+ flags.

Copy link
Contributor

@kllysng kllysng left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@FuPingFranco FuPingFranco left a comment

Choose a reason for hiding this comment

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

LGTM

@keegan-caruso keegan-caruso merged commit ceeb23c into dev Apr 5, 2024
5 checks passed
@keegan-caruso keegan-caruso deleted the kecaruso/buffers-base64 branch April 5, 2024 19:12
@kllysng kllysng added this to the 7.5.1 milestone Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants