Skip to content
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

Remove object[] allocation from CustomAttribute.AddCustomAttributes #87902

Merged
merged 1 commit into from Jun 22, 2023

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
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
108 checks passed
@stephentoub stephentoub deleted the proparrayalloc branch June 22, 2023 10:47
@dotnet dotnet 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.

None yet

3 participants