Skip to content

Improve startup time for runtime EventSources #45105

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -499,20 +499,21 @@ public static bool IsDefined(MemberInfo element, Type attributeType, bool inheri

public static Attribute? GetCustomAttribute(MemberInfo element, Type attributeType)
{
return GetCustomAttribute(element, attributeType, true);
return GetCustomAttribute(element, attributeType, inherit: true);
}

public static Attribute? GetCustomAttribute(MemberInfo element, Type attributeType, bool inherit)
{
Attribute[] attrib = GetCustomAttributes(element, attributeType, inherit);
if (element == null)
throw new ArgumentNullException(nameof(element));

if (attrib == null || attrib.Length == 0)
return null;
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

if (attrib.Length == 1)
return attrib[0];
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);

throw new AmbiguousMatchException(SR.RFLCT_AmbigCust);
return element.GetCustomAttribute(attributeType, inherit);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,53 @@ internal static bool IsDefined(RuntimeModule module, RuntimeType caType)
return IsCustomAttributeDefined(module, module.MetadataToken, caType);
}

internal static Attribute? GetCustomAttribute(RuntimeType type, RuntimeType caType, bool inherit)
{
Debug.Assert(type != null);
Debug.Assert(caType != null);

if (type.GetElementType() != null)
return null;

if (type.IsGenericType && !type.IsGenericTypeDefinition)
type = (type.GetGenericTypeDefinition() as RuntimeType)!;

PseudoCustomAttribute.GetCustomAttributes(type, caType, out RuntimeType.ListBuilder<Attribute> pcas);

RuntimeType.ListBuilder<object> attributes = default;
// if we are asked to go up the hierarchy chain we have to do it now and regardless of the
// attribute usage for the specific attribute because a derived attribute may override the usage...
// ... however if the attribute is sealed we can rely on the attribute usage
if (!inherit || (caType.IsSealed && !CustomAttribute.GetAttributeUsage(caType).Inherited))
{
AddCustomAttributes(ref attributes, type.GetRuntimeModule(), type.MetadataToken, caType, false, default);
}
else
{
bool mustBeInheritable = false;

for (int i = 0; i < pcas.Count; i++)
{
attributes.Add(pcas[i]);
}

while (type != (RuntimeType)typeof(object) && type != null)
{
AddCustomAttributes(ref attributes, type.GetRuntimeModule(), type.MetadataToken, caType, mustBeInheritable, attributes);
mustBeInheritable = true;
type = (type.BaseType as RuntimeType)!;
}
}

if (attributes.Count == 0)
return null;

if (attributes.Count == 1)
return attributes[0] as Attribute;

throw new AmbiguousMatchException(SR.RFLCT_AmbigCust);
}

internal static object[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool inherit)
{
Debug.Assert(type != null);
Expand Down Expand Up @@ -985,6 +1032,50 @@ internal static object[] GetCustomAttributes(RuntimeType type, RuntimeType caTyp
return typedResult;
}

internal static Attribute? GetCustomAttribute(RuntimeMethodInfo method, RuntimeType caType, bool inherit)
{
Debug.Assert(method != null);
Debug.Assert(caType != null);

if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
method = (method.GetGenericMethodDefinition() as RuntimeMethodInfo)!;

PseudoCustomAttribute.GetCustomAttributes(method, caType, out RuntimeType.ListBuilder<Attribute> pcas);

RuntimeType.ListBuilder<object> attributes = default;
// if we are asked to go up the hierarchy chain we have to do it now and regardless of the
// attribute usage for the specific attribute because a derived attribute may override the usage...
// ... however if the attribute is sealed we can rely on the attribute usage
if (!inherit || (caType.IsSealed && !CustomAttribute.GetAttributeUsage(caType).Inherited))
{
AddCustomAttributes(ref attributes, method.GetRuntimeModule(), method.MetadataToken, caType, false, default);
}
else
{
bool mustBeInheritable = false;

for (int i = 0; i < pcas.Count; i++)
{
attributes.Add(pcas[i]);
}

while (method != null)
{
AddCustomAttributes(ref attributes, method.GetRuntimeModule(), method.MetadataToken, caType, mustBeInheritable, attributes);
mustBeInheritable = true;
method = method.GetParentDefinition()!;
}
}

if (attributes.Count == 0)
return null;

if (attributes.Count == 1)
return attributes[0] as Attribute;

throw new AmbiguousMatchException(SR.RFLCT_AmbigCust);
}

internal static object[] GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, bool inherit)
{
Debug.Assert(method != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,18 @@ internal bool HasSameMetadataDefinitionAsCore<TOther>(MemberInfo other) where TO

return true;
}

internal virtual Attribute? GetCustomAttribute(Type attributeType, bool inherit)
{
Attribute[]? attrib = GetCustomAttributes(attributeType, inherit) as Attribute[];

if (attrib == null || attrib.Length == 0)
return null;

if (attrib.Length == 1)
return attrib[0];

throw new AmbiguousMatchException(SR.RFLCT_AmbigCust);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType, inherit);
}

internal override Attribute? GetCustomAttribute(Type attributeType, bool inherit)
{
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.GetCustomAttribute(this, attributeRuntimeType, inherit);
}

public override bool IsDefined(Type attributeType, bool inherit)
{
if (attributeType == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,19 @@ internal FieldInfo GetField(RuntimeFieldHandleInternal field)
return retval;
}

internal override Attribute? GetCustomAttribute(Type attributeType, bool inherit)
{
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.GetCustomAttribute(this, attributeRuntimeType, inherit);
}

internal object? GenericCache
{
get => CacheIfExists?.GenericCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Tracing\EventProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Tracing\EventSource.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Tracing\EventSourceException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Tracing\EventSources.MetaData.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Tracing\FrameworkEventSource.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Tracing\IEventProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\Tracing\IncrementingEventCounter.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace System.Buffers
{
[EventSource(Guid = "0866B2B8-5CEF-5DB9-2612-0C0FFD814A44", Name = "System.Buffers.ArrayPoolEventSource")]
internal sealed class ArrayPoolEventSource : EventSource
internal sealed partial class ArrayPoolEventSource : EventSource
{
internal static readonly ArrayPoolEventSource Log = new ArrayPoolEventSource();

Expand Down
Loading