Skip to content

Commit 79dad59

Browse files
committed
Use ReadOnlySpan for CustomAttribute binaryData
1 parent 5a7da08 commit 79dad59

39 files changed

+109
-115
lines changed

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public override Assembly GetSatelliteAssembly(CultureInfo culture, Version? vers
288288
/// <summary>
289289
/// Use this function if client decides to form the custom attribute blob themselves.
290290
/// </summary>
291-
protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute)
291+
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
292292
{
293293
lock (SyncRoot)
294294
{

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeConstructorBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ internal override Type GetReturnType()
157157
return m_methodBuilder.ReturnType;
158158
}
159159

160-
protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute)
160+
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
161161
{
162162
m_methodBuilder.SetCustomAttribute(con, binaryAttribute);
163163
}

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEnumBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
274274
}
275275

276276
// Use this function if client decides to form the custom attribute blob themselves
277-
protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute)
277+
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
278278
{
279279
m_typeBuilder.SetCustomAttribute(con, binaryAttribute);
280280
}

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeEventBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected override void AddOtherMethodCore(MethodBuilder mdBuilder)
8080

8181
// Use this function if client decides to form the custom attribute blob themselves
8282

83-
protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute)
83+
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
8484
{
8585
m_type.ThrowIfCreated();
8686

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeFieldBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected override void SetConstantCore(object? defaultValue)
150150
RuntimeTypeBuilder.SetConstantValue(m_typeBuilder.GetModuleBuilder(), m_fieldTok, m_fieldType, defaultValue);
151151
}
152152

153-
protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute)
153+
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
154154
{
155155
RuntimeModuleBuilder moduleBuilder = (RuntimeModuleBuilder)m_typeBuilder.Module;
156156

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeGenericTypeParameterBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public override Type MakeArrayType(int rank)
215215
#endregion
216216

217217
#region Protected Members Overrides
218-
protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute)
218+
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
219219
{
220220
m_type.SetGenParamCustomAttribute(con, binaryAttribute);
221221
}

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ internal Module GetModule()
695695
return GetModuleBuilder();
696696
}
697697

698-
protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute)
698+
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
699699
{
700700
ThrowIfGeneric();
701701
RuntimeTypeBuilder.DefineCustomAttribute(m_module, MetadataToken,

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ internal int GetSignatureToken(byte[] sigBytes, int sigLength)
12891289

12901290
#region Other
12911291

1292-
protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute)
1292+
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
12931293
{
12941294
RuntimeTypeBuilder.DefineCustomAttribute(
12951295
this,

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimePropertyBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected override void AddOtherMethodCore(MethodBuilder mdBuilder)
9797
}
9898

9999
// Use this function if client decides to form the custom attribute blob themselves
100-
protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute)
100+
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
101101
{
102102
m_containingType.ThrowIfCreated();
103103
RuntimeTypeBuilder.DefineCustomAttribute(

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,12 @@ private sealed class CustAttr
111111
private readonly byte[]? m_binaryAttribute;
112112
private readonly CustomAttributeBuilder? m_customBuilder;
113113

114-
public CustAttr(ConstructorInfo con, byte[] binaryAttribute)
114+
public CustAttr(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
115115
{
116116
ArgumentNullException.ThrowIfNull(con);
117-
ArgumentNullException.ThrowIfNull(binaryAttribute);
118117

119118
m_con = con;
120-
m_binaryAttribute = binaryAttribute;
119+
m_binaryAttribute = binaryAttribute.ToArray();
121120
}
122121

123122
public CustAttr(CustomAttributeBuilder customBuilder)
@@ -173,21 +172,13 @@ private static partial void SetMethodIL(QCallModule module, int tk, [MarshalAs(U
173172

174173
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "TypeBuilder_DefineCustomAttribute")]
175174
private static partial void DefineCustomAttribute(QCallModule module, int tkAssociate, int tkConstructor,
176-
byte[]? attr, int attrLength);
175+
ReadOnlySpan<byte> attr, int attrLength);
177176

178177
internal static void DefineCustomAttribute(RuntimeModuleBuilder module, int tkAssociate, int tkConstructor,
179-
byte[]? attr)
178+
ReadOnlySpan<byte> attr)
180179
{
181-
byte[]? localAttr = null;
182-
183-
if (attr != null)
184-
{
185-
localAttr = new byte[attr.Length];
186-
Buffer.BlockCopy(attr, 0, localAttr, 0, attr.Length);
187-
}
188-
189180
DefineCustomAttribute(new QCallModule(ref module), tkAssociate, tkConstructor,
190-
localAttr, (localAttr != null) ? localAttr.Length : 0);
181+
attr, attr.Length);
191182
}
192183

193184
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "TypeBuilder_DefineProperty", StringMarshalling = StringMarshalling.Utf16)]
@@ -670,7 +661,7 @@ internal void SetGenParamAttributes(GenericParameterAttributes genericParameterA
670661
m_genParamAttributes = genericParameterAttributes;
671662
}
672663

673-
internal void SetGenParamCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
664+
internal void SetGenParamCustomAttribute(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
674665
{
675666
CustAttr ca = new CustAttr(con, binaryAttribute);
676667

@@ -1858,7 +1849,12 @@ internal int TypeToken
18581849
}
18591850
}
18601851

1861-
protected override void SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute)
1852+
internal void SetCustomAttribute(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
1853+
{
1854+
SetCustomAttributeCore(con, binaryAttribute);
1855+
}
1856+
1857+
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
18621858
{
18631859
DefineCustomAttribute(m_module, m_tdType, m_module.GetMethodMetadataToken(con), binaryAttribute);
18641860
}

0 commit comments

Comments
 (0)