-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Cleanup a few more allocations in X.509 #69831
Conversation
Tagging subscribers to this area: @dotnet/area-system-security, @vcsjones Issue DetailsA few more small allocations that can be avoided, and a little bit of formatting cleanup in
|
if (publicKey == null) | ||
{ | ||
string keyAlgorithmOid = GetKeyAlgorithm(); | ||
byte[] parameters = GetKeyAlgorithmParameters(); | ||
byte[] keyValue = GetPublicKey(); | ||
byte[] parameters = Pal.KeyAlgorithmParameters; |
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.
The public APIs GetKeyAlgorithmParameters
and GetPublicKey
create defensive copies, then we were feeding them in to AsnEncodedData
, which again created defensive copies. If we go to the PAL directly for the bytes we can avoid the first defensive copy.
@@ -118,7 +118,7 @@ public void Dispose() | |||
|
|||
private SafeCreateHandle PreparePoliciesArray(bool checkRevocation) | |||
{ | |||
IntPtr[] policies = new IntPtr[checkRevocation ? 2 : 1]; | |||
Span<IntPtr> policies = checkRevocation ? stackalloc IntPtr[2] : stackalloc IntPtr[1]; |
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.
It's okay to put stack data in to CFArrayCreate
as long as the items themselves are not from the stack. It creates a CoreFoundation array from the contents of the stack, it doesn't try to take ownership of the stack data.
We're also doing this in SafeDeleteSslContext
as well.
runtime/src/libraries/System.Net.Security/src/System/Net/Security/Pal.OSX/SafeDeleteSslContext.cs
Lines 120 to 122 in cde8b0b
Span<IntPtr> handles = certList.Count <= 256 | |
? stackalloc IntPtr[256] | |
: new IntPtr[certList.Count]; |
A few more small allocations that can be avoided, and a little bit of formatting cleanup in
X509Certificate2
.