-
Notifications
You must be signed in to change notification settings - Fork 10.5k
perf: get rid of MemoryStream in KeyRingBasedDataProtector
#59322
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
Changes from 2 commits
c686038
8ac940f
4b5ca1a
a046b3f
f233b4e
51dec55
8212333
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |||||||||||||||||||||||||||||||||||||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| using System; | ||||||||||||||||||||||||||||||||||||||||||
| using System.Buffers.Binary; | ||||||||||||||||||||||||||||||||||||||||||
| using System.Buffers; | ||||||||||||||||||||||||||||||||||||||||||
| using System.Collections.Generic; | ||||||||||||||||||||||||||||||||||||||||||
| using System.Diagnostics; | ||||||||||||||||||||||||||||||||||||||||||
| using System.Diagnostics.CodeAnalysis; | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -14,6 +16,7 @@ | |||||||||||||||||||||||||||||||||||||||||
| using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal; | ||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.AspNetCore.Shared; | ||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.Extensions.Logging; | ||||||||||||||||||||||||||||||||||||||||||
| using System.Buffers.Text; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| namespace Microsoft.AspNetCore.DataProtection.KeyManagement; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -317,37 +320,6 @@ private struct AdditionalAuthenticatedDataTemplate | |||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| private byte[] _aadTemplate; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public AdditionalAuthenticatedDataTemplate(IEnumerable<string> purposes) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| const int MEMORYSTREAM_DEFAULT_CAPACITY = 0x100; // matches MemoryStream.EnsureCapacity | ||||||||||||||||||||||||||||||||||||||||||
| var ms = new MemoryStream(MEMORYSTREAM_DEFAULT_CAPACITY); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // additionalAuthenticatedData := { magicHeader (32-bit) || keyId || purposeCount (32-bit) || (purpose)* } | ||||||||||||||||||||||||||||||||||||||||||
| // purpose := { utf8ByteCount (7-bit encoded) || utf8Text } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| using (var writer = new PurposeBinaryWriter(ms)) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| writer.WriteBigEndian(MAGIC_HEADER_V0); | ||||||||||||||||||||||||||||||||||||||||||
| Debug.Assert(ms.Position == sizeof(uint)); | ||||||||||||||||||||||||||||||||||||||||||
| var posPurposeCount = writer.Seek(sizeof(Guid), SeekOrigin.Current); // skip over where the key id will be stored; we'll fill it in later | ||||||||||||||||||||||||||||||||||||||||||
| writer.Seek(sizeof(uint), SeekOrigin.Current); // skip over where the purposeCount will be stored; we'll fill it in later | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| uint purposeCount = 0; | ||||||||||||||||||||||||||||||||||||||||||
| foreach (string purpose in purposes) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| Debug.Assert(purpose != null); | ||||||||||||||||||||||||||||||||||||||||||
| writer.Write(purpose); // prepends length as a 7-bit encoded integer | ||||||||||||||||||||||||||||||||||||||||||
| purposeCount++; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Once we have written all the purposes, go back and fill in 'purposeCount' | ||||||||||||||||||||||||||||||||||||||||||
| writer.Seek(checked((int)posPurposeCount), SeekOrigin.Begin); | ||||||||||||||||||||||||||||||||||||||||||
| writer.WriteBigEndian(purposeCount); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| _aadTemplate = ms.ToArray(); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public byte[] GetAadForKey(Guid keyId, bool isProtecting) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| // Multiple threads might be trying to read and write the _aadTemplate field | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -381,6 +353,118 @@ public byte[] GetAadForKey(Guid keyId, bool isProtecting) | |||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| #if NET10_0_OR_GREATER | ||||||||||||||||||||||||||||||||||||||||||
| public AdditionalAuthenticatedDataTemplate(string[] purposes) | ||||||||||||||||||||||||||||||||||||||||||
DeagleGross marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| // additionalAuthenticatedData := { magicHeader (32-bit) || keyId || purposeCount (32-bit) || (purpose)* } | ||||||||||||||||||||||||||||||||||||||||||
| // purpose := { utf8ByteCount (7-bit encoded) || utf8Text } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| var keySize = sizeof(Guid); | ||||||||||||||||||||||||||||||||||||||||||
| int totalPurposeLen = 4 + keySize + 4; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| var purposeLengthsPool = ArrayPool<int>.Shared.Rent(purposes.Length); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Gen2 | Allocated |
|---|---|---|---|---|---|---|---|
| MemoryStream | 141.4 ns | 2.62 ns | 3.31 ns | 0.0067 | - | - | 512 B |
| Manual | 101.8 ns | 0.98 ns | 0.92 ns | 0.0023 | 0.0002 | 0.0002 | 320 B |
| Manual_CalculateByteCountEveryTime | 124.7 ns | 1.62 ns | 1.52 ns | 0.0017 | - | - | 128 B |
| Manual_StackAllocForSmallPurposeArrays | 122.4 ns | 0.70 ns | 0.54 ns | 0.0014 | - | - | 128 B |
It seems like that last option Marc suggested is the most balanced - we indeed dont expect many purposes (I think), and this solution does not contest for the ArrayPool rent.
What do you think?
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.
are the last two doing the same work as Manual? it seems odd that the perf is worse than Manual - we're doing less work (by not touching the pool at all) - why is it taking longer? question: can you try this with [SkipLocalsInit] on the method? or the entire type for fairness (this is applied globally in aspnetcore, I believe, but I don't know whether it'll apply to your benchmark which looks to be custom).
I also don't know why the memory is less if it is doing the same work - the only non-amortized allocation here is the result byte[]; so I assume this is a different data test than Manual?
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.
can you try this with [SkipLocalsInit] on the method? or the entire type for fairness (this is applied globally in aspnetcore, I believe
Don't think it is. #26586
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.
we have talked with Marc: he launched the benchmarks on his machine and there is no crazy allocs which are confusing. I think we should merge with the current state of code (stackalloc for under 32 dynamic length of purposes)
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.
can you try this with [SkipLocalsInit] on the method? or the entire type for fairness (this is applied globally in aspnetcore, I believe
Don't think it is. #26586
We should revisit this, IMHO.
Uh oh!
There was an error while loading. Please reload this page.