KEMBAR78
Remove object[] allocation from CustomAttribute.AddCustomAttributes by stephentoub · Pull Request #87902 · dotnet/runtime · GitHub
Skip to content

Conversation

@stephentoub
Copy link
Member

Method Toolchain Mean Ratio Allocated Alloc Ratio
GetCustomAttributes \main\corerun.exe 888.7 ns 1.00 296 B 1.00
GetCustomAttributes \pr\corerun.exe 829.7 ns 0.94 232 B 0.78
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;

[MemoryDiagnoser(false)]
public class Program
{
    static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

    [Benchmark]
    public object[] GetCustomAttributes() => typeof(C).GetCustomAttributes(typeof(MyAttribute), true);
}

[My(Value1 = 1, Value2 = 2)]
class C { }

[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute
{
    public int Value1 { get; set; }
    public int Value2 { get; set; }
}

@ghost ghost assigned stephentoub Jun 22, 2023
@ghost
Copy link

ghost commented Jun 22, 2023

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

Issue Details
Method Toolchain Mean Ratio Allocated Alloc Ratio
GetCustomAttributes \main\corerun.exe 888.7 ns 1.00 296 B 1.00
GetCustomAttributes \pr\corerun.exe 829.7 ns 0.94 232 B 0.78
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;

[MemoryDiagnoser(false)]
public class Program
{
    static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

    [Benchmark]
    public object[] GetCustomAttributes() => typeof(C).GetCustomAttributes(typeof(MyAttribute), true);
}

[My(Value1 = 1, Value2 = 2)]
class C { }

[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute
{
    public int Value1 { get; set; }
    public int Value2 { get; set; }
}
Author: stephentoub
Assignees: stephentoub
Labels:

area-System.Reflection

Milestone: -

@stephentoub stephentoub merged commit 7b7d56f into dotnet:main Jun 22, 2023
@stephentoub stephentoub deleted the proparrayalloc branch June 22, 2023 10:47
@ghost ghost locked as resolved and limited conversation to collaborators Jul 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants