-
Notifications
You must be signed in to change notification settings - Fork 413
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
Reduce allocation with Base64UrlEncoder #2162
Conversation
- Stop calling ToCharArray to produce inputs to it; instead pass in a ReadOnlyMemory sliced appropriately - Move the char[] s_base64Table into a u8 span - Use IndexOfAny to determine whether replacements are needed in UnsafeDecode - Stackalloc a span or rent a char[] from the ArrayPool when a temporary is needed in UnsafeDecode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a nice win. LGTM
@@ -470,25 +469,39 @@ private void ReadToken(string encodedJson) | |||
throw LogHelper.LogExceptionMessage(new SecurityTokenMalformedException(LogHelper.FormatInvariant(LogMessages.IDX14310, encodedJson))); | |||
|
|||
// right number of dots for JWE | |||
_hChars = encodedJson.ToCharArray(0, Dot1); | |||
ReadOnlyMemory<char> hChars = encodedJson.AsMemory(0, Dot1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) what does h
mean in hChars
? "header"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea, I just kept the original naming :)
char[] encryptedKeyBytes = encodedJson.ToCharArray(Dot1 + 1, Dot2 - Dot1 - 1); | ||
if (encryptedKeyBytes.Length != 0) | ||
ReadOnlyMemory<char> encryptedKeyBytes = encodedJson.AsMemory(Dot1 + 1, Dot2 - Dot1 - 1); | ||
if (!encryptedKeyBytes.IsEmpty) | ||
{ | ||
EncryptedKeyBytes = Base64UrlEncoder.UnsafeDecode(encryptedKeyBytes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a comment for existing code, but all the other calls to Base64UrlEncoder.UnsafeDecode
have a try-catch around them, but this one doesn't. Do we know why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Stop calling ToCharArray to produce inputs to it; instead pass in a ReadOnlyMemory sliced appropriately - Move the char[] s_base64Table into a u8 span - Use IndexOfAny to determine whether replacements are needed in UnsafeDecode - Stackalloc a span or rent a char[] from the ArrayPool when a temporary is needed in UnsafeDecode
- Stop calling ToCharArray to produce inputs to it; instead pass in a ReadOnlyMemory sliced appropriately - Move the char[] s_base64Table into a u8 span - Use IndexOfAny to determine whether replacements are needed in UnsafeDecode - Stackalloc a span or rent a char[] from the ArrayPool when a temporary is needed in UnsafeDecode
- Stop calling ToCharArray to produce inputs to it; instead pass in a ReadOnlyMemory sliced appropriately - Move the char[] s_base64Table into a u8 span - Use IndexOfAny to determine whether replacements are needed in UnsafeDecode - Stackalloc a span or rent a char[] from the ArrayPool when a temporary is needed in UnsafeDecode
- Stop calling ToCharArray to produce inputs to it; instead pass in a ReadOnlyMemory sliced appropriately - Move the char[] s_base64Table into a u8 span - Use IndexOfAny to determine whether replacements are needed in UnsafeDecode - Stackalloc a span or rent a char[] from the ArrayPool when a temporary is needed in UnsafeDecode
NOTE: This requires #2158 before it will successfully build and run. It depends on the System.Memory package reference added there for downlevel builds.