-
Notifications
You must be signed in to change notification settings - Fork 10.3k
perf: Antiforgery token validation & generation #61989
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
base: main
Are you sure you want to change the base?
Conversation
{ | ||
return false; | ||
} | ||
|
||
var areEqual = true; | ||
for (var i = 0; i < a.Length; i++) | ||
for (int i = 0; i < a.Length; i++) |
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.
Here in ASP.NET Core the convention is to use var
, so please undo that change.
for (int i = 0; i < a.Length; i++) | |
for (var i = 0; i < a.Length; i++) |
Same on other places (I'll just leave a short comment over there, so you can find these easier).
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 only a draft PR @gfoidl, I wont yet address PR comments. I need to confirm I am getting perf improvement here. I will come back to the comments later
var tokenLength = serializedToken.Length; | ||
var charsRequired = WebEncoders.GetArraySizeRequiredToDecode(tokenLength); | ||
|
||
var chars = charsRequired < 128 ? stackalloc byte[128] : (rented = ArrayPool<byte>.Shared.Rent(charsRequired)); |
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.
Move the 128
to a named constant like StackAllocTheshold
or similar.
var chars = charsRequired < 128 ? stackalloc byte[128] : (rented = ArrayPool<byte>.Shared.Rent(charsRequired)); | ||
chars = chars[..charsRequired]; | ||
|
||
var decodedResult = WebEncoders.TryBase64UrlDecode( |
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.
Starting with .NET 9 the built-in Base64Url methods could be used. They're faster than WebEncoders
-version due to vectorization.
Nice that use WebEncoders here as shim for the built-in methods 👍🏻
(When reading the PR first, I thought that's missed)
@@ -119,6 +139,41 @@ public DefaultClaimUidExtractor(ObjectPool<AntiforgerySerializationContext> pool | |||
return identifierParameters; | |||
} | |||
|
|||
private static void ComputeSha256(IEnumerable<string> parameters, Span<byte> output, out int bytesWritten) | |||
{ | |||
int estimatedSize = 0; |
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.
int estimatedSize = 0; | |
var estimatedSize = 0; |
int estimatedSize = 0; | ||
foreach (var param in parameters) | ||
{ | ||
int byteCount = Encoding.UTF8.GetByteCount(param); |
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.
int byteCount = Encoding.UTF8.GetByteCount(param); | |
var byteCount = Encoding.UTF8.GetByteCount(param); |
int offset = 0; | ||
foreach (var param in parameters) | ||
{ | ||
int byteCount = Encoding.UTF8.GetByteCount(param); |
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.
int byteCount = Encoding.UTF8.GetByteCount(param); | |
var byteCount = Encoding.UTF8.GetByteCount(param); |
int result = 0; | ||
int shift = 0; |
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.
int result = 0; | |
int shift = 0; | |
var result = 0; | |
var shift = 0; |
throw new InvalidOperationException("Invalid 7-bit encoded int."); | ||
} | ||
|
||
byte b = span[bytesRead++]; |
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.
byte b = span[bytesRead++]; | |
var b = span[bytesRead++]; |
|
||
internal static class ReadOnlySpanExtensions | ||
{ | ||
public static bool BinaryReadBoolean(this ReadOnlySpan<byte> span, ref int offset) |
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.
public static bool BinaryReadBoolean(this ReadOnlySpan<byte> span, ref int offset) | |
public static bool BinaryReadBooleanAndAdvance(this ReadOnlySpan<byte> span, ref int offset) |
? Otherwise the side effect of offset++
maybe astonishing.
Or add an xml-doc comment.
int length = span.Read7BitEncodedInt(out int prefixLength); | ||
int stringEnd = prefixLength + length; |
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.
int length = span.Read7BitEncodedInt(out int prefixLength); | |
int stringEnd = prefixLength + length; | |
var length = span.Read7BitEncodedInt(out int prefixLength); | |
var stringEnd = prefixLength + length; |
IN PROGRESS
main stats for benchmark
{PR title}
Summary of the changes (Less than 80 chars)
Description
{Detail}
Fixes #{bug number} (in this specific format)