-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Replace confusing new T[] { ... } optimization with collection expressions
#93125
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 all commits
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 |
|---|---|---|
|
|
@@ -10,62 +10,62 @@ namespace System.Security.Cryptography | |
| internal static class RsaPaddingProcessor | ||
| { | ||
| // DigestInfo header values taken from https://tools.ietf.org/html/rfc3447#section-9.2, Note 1. | ||
| private static ReadOnlySpan<byte> DigestInfoMD5 => new byte[] | ||
| { | ||
| private static ReadOnlySpan<byte> DigestInfoMD5 => | ||
| [ | ||
| 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, | ||
| 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, | ||
| 0x04, 0x10, | ||
| }; | ||
| ]; | ||
|
|
||
| private static ReadOnlySpan<byte> DigestInfoSha1 => new byte[] | ||
| { | ||
| private static ReadOnlySpan<byte> DigestInfoSha1 => | ||
| [ | ||
| 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, | ||
| 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14, | ||
| }; | ||
| ]; | ||
|
|
||
| private static ReadOnlySpan<byte> DigestInfoSha256 => new byte[] | ||
| { | ||
| private static ReadOnlySpan<byte> DigestInfoSha256 => | ||
| [ | ||
| 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, | ||
| 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, | ||
| 0x20, | ||
| }; | ||
| ]; | ||
|
|
||
| private static ReadOnlySpan<byte> DigestInfoSha384 => new byte[] | ||
| { | ||
| private static ReadOnlySpan<byte> DigestInfoSha384 => | ||
| [ | ||
| 0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, | ||
| 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, | ||
| 0x30, | ||
| }; | ||
| ]; | ||
|
|
||
| private static ReadOnlySpan<byte> DigestInfoSha512 => new byte[] | ||
| { | ||
| private static ReadOnlySpan<byte> DigestInfoSha512 => | ||
| [ | ||
| 0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, | ||
| 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, | ||
| 0x40, | ||
| }; | ||
| ]; | ||
|
|
||
| private static ReadOnlySpan<byte> DigestInfoSha3_256 => new byte[] | ||
| { | ||
| private static ReadOnlySpan<byte> DigestInfoSha3_256 => | ||
| [ | ||
| 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, | ||
| 0x01, 0x65, 0x03, 0x04, 0x02, 0x08, 0x05, 0x00, 0x04, | ||
| 0x20, | ||
| }; | ||
| ]; | ||
|
|
||
| private static ReadOnlySpan<byte> DigestInfoSha3_384 => new byte[] | ||
| { | ||
| private static ReadOnlySpan<byte> DigestInfoSha3_384 => | ||
| [ | ||
| 0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, | ||
| 0x01, 0x65, 0x03, 0x04, 0x02, 0x09, 0x05, 0x00, 0x04, | ||
| 0x30, | ||
| }; | ||
| ]; | ||
|
|
||
| private static ReadOnlySpan<byte> DigestInfoSha3_512 => new byte[] | ||
| { | ||
| private static ReadOnlySpan<byte> DigestInfoSha3_512 => | ||
| [ | ||
| 0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, | ||
| 0x01, 0x65, 0x03, 0x04, 0x02, 0x0A, 0x05, 0x00, 0x04, | ||
| 0x40, | ||
| }; | ||
| ]; | ||
|
|
||
| private static ReadOnlySpan<byte> EightZeros => new byte[8]; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this one was actually allocating on each call :(
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that necessarily bad? Often we avoid caching memory that would never leave Gen0 (I don't know this code OC)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If this had instead been: private static ReadOnlySpan<byte> EightZeros => new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };there wouldn't have been any allocation, not even on first use: it would have simply been eight zero'd bytes in the assembly data, and the span would have pointed directly to that memory. And, yes, a property that looks like it should be non-allocating but that allocates on every access is bad :)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, wow, TIL.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should just fix that in Roslyn. I don't see a good reason for these two to exhibit such a codegen difference. (Though moving forward we'll use that syntax much less and so hopefully won't trip over it again.) |
||
| private static ReadOnlySpan<byte> EightZeros => [0, 0, 0, 0, 0, 0, 0, 0]; | ||
|
|
||
| private static ReadOnlySpan<byte> GetDigestInfoForAlgorithm( | ||
| HashAlgorithmName hashAlgorithmName, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.