Skip to content

Commit

Permalink
Use ReadOnlySpan<byte> for defaultDerInit (#33828)
Browse files Browse the repository at this point in the history
* Use ReadOnlySpan for defaultDerInit.

This removes an array allocation for each type initializer.

* Regenerate files.
  • Loading branch information
vcsjones committed Mar 20, 2020
1 parent a449356 commit ada03d7
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace System.Security.Cryptography.Asn1
[StructLayout(LayoutKind.Sequential)]
internal partial struct OaepParamsAsn
{
private static readonly byte[] s_defaultHashFunc = { 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00 };
private static ReadOnlySpan<byte> DefaultHashFunc => new byte[] { 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00 };

private static readonly byte[] s_defaultMaskGenFunc = { 0x30, 0x16, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00 };
private static ReadOnlySpan<byte> DefaultMaskGenFunc => new byte[] { 0x30, 0x16, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00 };

private static readonly byte[] s_defaultPSourceFunc = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x09, 0x04, 0x00 };
private static ReadOnlySpan<byte> DefaultPSourceFunc => new byte[] { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x09, 0x04, 0x00 };

internal System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn HashFunc;
internal System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn MaskGenFunc;
Expand All @@ -30,15 +30,15 @@ static OaepParamsAsn()
ReadOnlyMemory<byte> rebind = default;
AsnValueReader reader;

reader = new AsnValueReader(s_defaultHashFunc, AsnEncodingRules.DER);
reader = new AsnValueReader(DefaultHashFunc, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref reader, rebind, out decoded.HashFunc);
reader.ThrowIfNotEmpty();

reader = new AsnValueReader(s_defaultMaskGenFunc, AsnEncodingRules.DER);
reader = new AsnValueReader(DefaultMaskGenFunc, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref reader, rebind, out decoded.MaskGenFunc);
reader.ThrowIfNotEmpty();

reader = new AsnValueReader(s_defaultPSourceFunc, AsnEncodingRules.DER);
reader = new AsnValueReader(DefaultPSourceFunc, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref reader, rebind, out decoded.PSourceFunc);
reader.ThrowIfNotEmpty();
}
Expand All @@ -61,7 +61,7 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)
HashFunc.Encode(tmp);
ReadOnlySpan<byte> encoded = tmp.EncodeAsSpan();

if (!encoded.SequenceEqual(s_defaultHashFunc))
if (!encoded.SequenceEqual(DefaultHashFunc))
{
writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 0));
writer.WriteEncodedValue(encoded);
Expand All @@ -78,7 +78,7 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)
MaskGenFunc.Encode(tmp);
ReadOnlySpan<byte> encoded = tmp.EncodeAsSpan();

if (!encoded.SequenceEqual(s_defaultMaskGenFunc))
if (!encoded.SequenceEqual(DefaultMaskGenFunc))
{
writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 1));
writer.WriteEncodedValue(encoded);
Expand All @@ -95,7 +95,7 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)
PSourceFunc.Encode(tmp);
ReadOnlySpan<byte> encoded = tmp.EncodeAsSpan();

if (!encoded.SequenceEqual(s_defaultPSourceFunc))
if (!encoded.SequenceEqual(DefaultPSourceFunc))
{
writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 2));
writer.WriteEncodedValue(encoded);
Expand Down Expand Up @@ -142,7 +142,7 @@ internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, Read
}
else
{
defaultReader = new AsnValueReader(s_defaultHashFunc, AsnEncodingRules.DER);
defaultReader = new AsnValueReader(DefaultHashFunc, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref defaultReader, rebind, out decoded.HashFunc);
}

Expand All @@ -155,7 +155,7 @@ internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, Read
}
else
{
defaultReader = new AsnValueReader(s_defaultMaskGenFunc, AsnEncodingRules.DER);
defaultReader = new AsnValueReader(DefaultMaskGenFunc, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref defaultReader, rebind, out decoded.MaskGenFunc);
}

Expand All @@ -168,7 +168,7 @@ internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, Read
}
else
{
defaultReader = new AsnValueReader(s_defaultPSourceFunc, AsnEncodingRules.DER);
defaultReader = new AsnValueReader(DefaultPSourceFunc, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref defaultReader, rebind, out decoded.PSourceFunc);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace System.Security.Cryptography.Asn1
[StructLayout(LayoutKind.Sequential)]
internal partial struct Pbkdf2Params
{
private static readonly byte[] s_defaultPrf = { 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x07, 0x05, 0x00 };
private static ReadOnlySpan<byte> DefaultPrf => new byte[] { 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x07, 0x05, 0x00 };

internal System.Security.Cryptography.Asn1.Pbkdf2SaltChoice Salt;
internal int IterationCount;
Expand All @@ -27,7 +27,7 @@ static Pbkdf2Params()
ReadOnlyMemory<byte> rebind = default;
AsnValueReader reader;

reader = new AsnValueReader(s_defaultPrf, AsnEncodingRules.DER);
reader = new AsnValueReader(DefaultPrf, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref reader, rebind, out decoded.Prf);
reader.ThrowIfNotEmpty();
}
Expand Down Expand Up @@ -58,7 +58,7 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)
Prf.Encode(tmp);
ReadOnlySpan<byte> encoded = tmp.EncodeAsSpan();

if (!encoded.SequenceEqual(s_defaultPrf))
if (!encoded.SequenceEqual(DefaultPrf))
{
writer.WriteEncodedValue(encoded);
}
Expand Down Expand Up @@ -122,7 +122,7 @@ internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, Read
}
else
{
defaultReader = new AsnValueReader(s_defaultPrf, AsnEncodingRules.DER);
defaultReader = new AsnValueReader(DefaultPrf, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref defaultReader, rebind, out decoded.Prf);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace System.Security.Cryptography.Asn1.Pkcs12
[StructLayout(LayoutKind.Sequential)]
internal partial struct MacData
{
private static readonly byte[] s_defaultIterationCount = { 0x02, 0x01, 0x01 };
private static ReadOnlySpan<byte> DefaultIterationCount => new byte[] { 0x02, 0x01, 0x01 };

internal System.Security.Cryptography.Asn1.DigestInfoAsn Mac;
internal ReadOnlyMemory<byte> MacSalt;
Expand All @@ -25,7 +25,7 @@ static MacData()
MacData decoded = default;
AsnValueReader reader;

reader = new AsnValueReader(s_defaultIterationCount, AsnEncodingRules.DER);
reader = new AsnValueReader(DefaultIterationCount, AsnEncodingRules.DER);

if (!reader.TryReadInt32(out decoded.IterationCount))
{
Expand Down Expand Up @@ -55,7 +55,7 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)
tmp.WriteInteger(IterationCount);
ReadOnlySpan<byte> encoded = tmp.EncodeAsSpan();

if (!encoded.SequenceEqual(s_defaultIterationCount))
if (!encoded.SequenceEqual(DefaultIterationCount))
{
writer.WriteEncodedValue(encoded);
}
Expand Down Expand Up @@ -116,7 +116,7 @@ internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, Read
}
else
{
defaultReader = new AsnValueReader(s_defaultIterationCount, AsnEncodingRules.DER);
defaultReader = new AsnValueReader(DefaultIterationCount, AsnEncodingRules.DER);

if (!defaultReader.TryReadInt32(out decoded.IterationCount))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ namespace System.Security.Cryptography.Asn1
[StructLayout(LayoutKind.Sequential)]
internal partial struct PssParamsAsn
{
private static readonly byte[] s_defaultHashAlgorithm = { 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00 };
private static ReadOnlySpan<byte> DefaultHashAlgorithm => new byte[] { 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00 };

private static readonly byte[] s_defaultMaskGenAlgorithm = { 0x30, 0x16, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00 };
private static ReadOnlySpan<byte> DefaultMaskGenAlgorithm => new byte[] { 0x30, 0x16, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00 };

private static readonly byte[] s_defaultSaltLength = { 0x02, 0x01, 0x14 };
private static ReadOnlySpan<byte> DefaultSaltLength => new byte[] { 0x02, 0x01, 0x14 };

private static readonly byte[] s_defaultTrailerField = { 0x02, 0x01, 0x01 };
private static ReadOnlySpan<byte> DefaultTrailerField => new byte[] { 0x02, 0x01, 0x01 };

internal System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn HashAlgorithm;
internal System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn MaskGenAlgorithm;
Expand All @@ -33,15 +33,15 @@ static PssParamsAsn()
ReadOnlyMemory<byte> rebind = default;
AsnValueReader reader;

reader = new AsnValueReader(s_defaultHashAlgorithm, AsnEncodingRules.DER);
reader = new AsnValueReader(DefaultHashAlgorithm, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref reader, rebind, out decoded.HashAlgorithm);
reader.ThrowIfNotEmpty();

reader = new AsnValueReader(s_defaultMaskGenAlgorithm, AsnEncodingRules.DER);
reader = new AsnValueReader(DefaultMaskGenAlgorithm, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref reader, rebind, out decoded.MaskGenAlgorithm);
reader.ThrowIfNotEmpty();

reader = new AsnValueReader(s_defaultSaltLength, AsnEncodingRules.DER);
reader = new AsnValueReader(DefaultSaltLength, AsnEncodingRules.DER);

if (!reader.TryReadInt32(out decoded.SaltLength))
{
Expand All @@ -50,7 +50,7 @@ static PssParamsAsn()

reader.ThrowIfNotEmpty();

reader = new AsnValueReader(s_defaultTrailerField, AsnEncodingRules.DER);
reader = new AsnValueReader(DefaultTrailerField, AsnEncodingRules.DER);

if (!reader.TryReadInt32(out decoded.TrailerField))
{
Expand Down Expand Up @@ -78,7 +78,7 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)
HashAlgorithm.Encode(tmp);
ReadOnlySpan<byte> encoded = tmp.EncodeAsSpan();

if (!encoded.SequenceEqual(s_defaultHashAlgorithm))
if (!encoded.SequenceEqual(DefaultHashAlgorithm))
{
writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 0));
writer.WriteEncodedValue(encoded);
Expand All @@ -95,7 +95,7 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)
MaskGenAlgorithm.Encode(tmp);
ReadOnlySpan<byte> encoded = tmp.EncodeAsSpan();

if (!encoded.SequenceEqual(s_defaultMaskGenAlgorithm))
if (!encoded.SequenceEqual(DefaultMaskGenAlgorithm))
{
writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 1));
writer.WriteEncodedValue(encoded);
Expand All @@ -112,7 +112,7 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)
tmp.WriteInteger(SaltLength);
ReadOnlySpan<byte> encoded = tmp.EncodeAsSpan();

if (!encoded.SequenceEqual(s_defaultSaltLength))
if (!encoded.SequenceEqual(DefaultSaltLength))
{
writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 2));
writer.WriteEncodedValue(encoded);
Expand All @@ -129,7 +129,7 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)
tmp.WriteInteger(TrailerField);
ReadOnlySpan<byte> encoded = tmp.EncodeAsSpan();

if (!encoded.SequenceEqual(s_defaultTrailerField))
if (!encoded.SequenceEqual(DefaultTrailerField))
{
writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 3));
writer.WriteEncodedValue(encoded);
Expand Down Expand Up @@ -176,7 +176,7 @@ internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, Read
}
else
{
defaultReader = new AsnValueReader(s_defaultHashAlgorithm, AsnEncodingRules.DER);
defaultReader = new AsnValueReader(DefaultHashAlgorithm, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref defaultReader, rebind, out decoded.HashAlgorithm);
}

Expand All @@ -189,7 +189,7 @@ internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, Read
}
else
{
defaultReader = new AsnValueReader(s_defaultMaskGenAlgorithm, AsnEncodingRules.DER);
defaultReader = new AsnValueReader(DefaultMaskGenAlgorithm, AsnEncodingRules.DER);
System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn.Decode(ref defaultReader, rebind, out decoded.MaskGenAlgorithm);
}

Expand All @@ -207,7 +207,7 @@ internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, Read
}
else
{
defaultReader = new AsnValueReader(s_defaultSaltLength, AsnEncodingRules.DER);
defaultReader = new AsnValueReader(DefaultSaltLength, AsnEncodingRules.DER);

if (!defaultReader.TryReadInt32(out decoded.SaltLength))
{
Expand All @@ -230,7 +230,7 @@ internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, Read
}
else
{
defaultReader = new AsnValueReader(s_defaultTrailerField, AsnEncodingRules.DER);
defaultReader = new AsnValueReader(DefaultTrailerField, AsnEncodingRules.DER);

if (!defaultReader.TryReadInt32(out decoded.TrailerField))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace System.Security.Cryptography.Asn1
[StructLayout(LayoutKind.Sequential)]
internal partial struct X509ExtensionAsn
{
private static readonly byte[] s_defaultCritical = { 0x01, 0x01, 0x00 };
private static ReadOnlySpan<byte> DefaultCritical => new byte[] { 0x01, 0x01, 0x00 };

internal Oid ExtnId;
internal bool Critical;
Expand All @@ -25,7 +25,7 @@ static X509ExtensionAsn()
X509ExtensionAsn decoded = default;
AsnValueReader reader;

reader = new AsnValueReader(s_defaultCritical, AsnEncodingRules.DER);
reader = new AsnValueReader(DefaultCritical, AsnEncodingRules.DER);
decoded.Critical = reader.ReadBoolean();
reader.ThrowIfNotEmpty();
}
Expand All @@ -49,7 +49,7 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)
tmp.WriteBoolean(Critical);
ReadOnlySpan<byte> encoded = tmp.EncodeAsSpan();

if (!encoded.SequenceEqual(s_defaultCritical))
if (!encoded.SequenceEqual(DefaultCritical))
{
writer.WriteEncodedValue(encoded);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ internal static void Decode(ref AsnValueReader reader, Asn1Tag expectedTag, Read
}
else
{
defaultReader = new AsnValueReader(s_defaultCritical, AsnEncodingRules.DER);
defaultReader = new AsnValueReader(DefaultCritical, AsnEncodingRules.DER);
decoded.Critical = defaultReader.ReadBoolean();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ namespace <xsl:value-of select="@namespace" />
</xsl:template>

<xsl:template match="*[@defaultDerInit]" mode="DefaultFieldDef">
private static readonly byte[] <xsl:call-template name="DefaultValueField"/> = { <xsl:value-of select="@defaultDerInit"/> };
private static ReadOnlySpan&lt;byte&gt; <xsl:call-template name="DefaultValueField"/> =&gt; new byte[] { <xsl:value-of select="@defaultDerInit"/> };
</xsl:template>

<xsl:template match="*[@defaultDerInit]" mode="DefaultFieldVerify">
Expand Down Expand Up @@ -909,7 +909,7 @@ namespace <xsl:value-of select="@namespace" />

<xsl:template name="ContextTag">new Asn1Tag(TagClass.ContextSpecific, <xsl:value-of select="@implicitTag | @explicitTag"/>)</xsl:template>

<xsl:template name="DefaultValueField">s_default<xsl:value-of select="@name"/></xsl:template>
<xsl:template name="DefaultValueField">Default<xsl:value-of select="@name"/></xsl:template>

<xsl:template name="DefaultValueDecoder"><xsl:if test="@defaultDerInit">
else
Expand Down
Loading

0 comments on commit ada03d7

Please sign in to comment.