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

Add FAST GetAttribute methods. #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/FastEnum.Benchmark/Models/Enums.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Runtime.Serialization;
using System;
using System.ComponentModel;
using System.Runtime.Serialization;



Expand All @@ -8,7 +10,7 @@ public enum Fruits : byte
{
Unknown = 0,

[EnumMember(Value = "🍎")]
[EnumMember(Value = "🍎"), Description("It is a rose family"), Color("Red"), Color("green")]
Apple,
Banana,
Peach,
Expand All @@ -22,4 +24,18 @@ public enum Fruits : byte
Pear,
Pineapple,
}

/// <summary>
/// Sample AllowMultiple Attribute
/// </summary>
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = true)]
public sealed class ColorAttribute : Attribute
{
public string Color { get; }

public ColorAttribute(string Color)
{
this.Color = Color;
}
}
}
4 changes: 3 additions & 1 deletion src/FastEnum.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Program
static void Main(string[] args)
{
var switcher = new BenchmarkSwitcher(new[]
{
{
typeof(GetValuesBenchmark),
typeof(GetNamesBenchmark),
typeof(GetNameBenchmark),
Expand All @@ -26,6 +26,8 @@ static void Main(string[] args)
typeof(DictionaryStringKeyBenchmark),
typeof(EnumMemberAttributeBenchmark),
typeof(ForEachBenchmark),
typeof(EnumAttributeBenchmark),
typeof(EnumAttributesBenchmark),
});
switcher.Run(args, new BenchmarkConfig());
}
Expand Down
46 changes: 46 additions & 0 deletions src/FastEnum.Benchmark/Scenarios/EnumAttributeBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;
using BenchmarkDotNet.Attributes;
using FastEnum.Benchmark.Models;
using _FastEnum = FastEnum.FastEnum;

namespace FastEnum.Benchmark.Scenarios
{
public class EnumAttributeBenchmark
{
private const Fruits Value = Fruits.Apple;


[GlobalSetup]
public void Setup()
{
_ = Enum.GetNames(typeof(Fruits));
_ = EnumsNET.Enums.GetNames<Fruits>();
_ = _FastEnum.GetNames<Fruits>();
}

[Benchmark(Baseline = true)]
public string NetCore()
{
var type = typeof(Fruits);
var name = Enum.GetName(type, Value);
var info = type.GetField(name);
var attr = info.GetCustomAttribute<DescriptionAttribute>();
return attr?.Description;
}

[Benchmark]
public string EnumsNet()
=> EnumsNET.Enums.GetAttributes(Value).Get<DescriptionAttribute>()?.Description;

[Benchmark]
public string FastEnumFromMember()
=> Value.ToMember().GetAttribute<DescriptionAttribute>()?.Description;
[Benchmark]
public string FastEnumDirect()
=> Value.GetAttribute<Fruits, DescriptionAttribute>()?.Description;
}
}
48 changes: 48 additions & 0 deletions src/FastEnum.Benchmark/Scenarios/EnumAttributesBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using BenchmarkDotNet.Attributes;
using FastEnum.Benchmark.Models;
using _FastEnum = FastEnum.FastEnum;

namespace FastEnum.Benchmark.Scenarios
{
public class EnumAttributesBenchmark
{
private const Fruits Value = Fruits.Apple;


[GlobalSetup]
public void Setup()
{
_ = Enum.GetNames(typeof(Fruits));
_ = EnumsNET.Enums.GetNames<Fruits>();
_ = _FastEnum.GetNames<Fruits>();
}

[Benchmark(Baseline = true)]
public string[] NetCore()
{
var type = typeof(Fruits);
var name = Enum.GetName(type, Value);
var info = type.GetField(name);
var attr = info.GetCustomAttributes<ColorAttribute>();
return attr.Select(x => x.Color).ToArray();
}

[Benchmark]
public string[] EnumsNet()
=> EnumsNET.Enums.GetAttributes(Value).GetAll<ColorAttribute>().Select(x => x.Color).ToArray();

[Benchmark]
public string[] FastEnumFromMember()
=> Value.ToMember().GetAttributes<ColorAttribute>().Select(x => x.Color).ToArray();
[Benchmark]
public string[] FastEnumDirect()
=> Value.GetAttributes<Fruits, ColorAttribute>().Select(x => x.Color).ToArray();
}

}
31 changes: 31 additions & 0 deletions src/FastEnum/FastEnum.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using FastEnumUtility.Internals;

Expand Down Expand Up @@ -525,6 +526,36 @@ private static bool IsContinuousInternal()
}
#endregion
}

/// <summary>
/// Provides cache for enum attributes.
/// </summary>
/// <typeparam name="T">Enum Type</typeparam>
/// <typeparam name="TAttribute">Attribute Type</typeparam>
internal static class EnumAttributeCache<T, TAttribute>
where T : struct, Enum
where TAttribute : Attribute
{
internal static FrozenDictionary<T, TAttribute> Cache { get; } = GetValues<T>()
.ToFrozenDictionary(
x => x,
x => x.ToMember().FieldInfo.GetCustomAttribute<TAttribute>());
}

/// <summary>
/// Provides cache for enum attributes.
/// </summary>
/// <typeparam name="T">Enum Type</typeparam>
/// <typeparam name="TAttribute">Attribute Type</typeparam>
internal static class EnumAttributesCache<T, TAttribute>
where T : struct, Enum
where TAttribute : Attribute
{
internal static FrozenDictionary<T, IReadOnlyList<TAttribute>> Cache { get; } = GetValues<T>()
.ToFrozenDictionary(
x => x,
x => x.ToMember().FieldInfo.GetCustomAttributes<TAttribute>().ToArray() as IReadOnlyList<TAttribute>);
}
#endregion
}
}
36 changes: 36 additions & 0 deletions src/FastEnum/FastEnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;


Expand Down Expand Up @@ -82,5 +86,37 @@ public static string GetLabel<T>(this T value, int index = 0, bool throwIfNotFou
? throw new NotFoundException($"{nameof(LabelAttribute)} that is specified index {index} is not found.")
: default(string);
}

/// <summary>
/// Gets the Attribute of specified enumration member.
/// </summary>
/// <typeparam name="T">Enum Type</typeparam>
/// <typeparam name="TAttribute">Attribute Type</typeparam>
/// <param name="value"></param>
/// <param name="throwIfNotFound"></param>
/// <returns></returns>
public static TAttribute GetAttribute<T, TAttribute>(this T value, bool throwIfNotFound = true)
where T : struct, Enum
where TAttribute : Attribute
{
var attr = FastEnum.EnumAttributeCache<T, TAttribute>.Cache[value];
return attr == null && throwIfNotFound
? throw new NotFoundException($"{typeof(TAttribute)} is not found.")
: attr;
}

/// <summary>
/// Gets the Attribute of specified enumration member.
/// </summary>
/// <typeparam name="T">Enum Type</typeparam>
/// <typeparam name="TAttribute">Attribute Type</typeparam>
/// <param name="value"></param>
/// <param name="throwIfNotFound"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IReadOnlyList<TAttribute> GetAttributes<T, TAttribute>(this T value)
where T : struct, Enum
where TAttribute : Attribute
=> FastEnum.EnumAttributesCache<T, TAttribute>.Cache[value];
}
}
18 changes: 18 additions & 0 deletions src/FastEnum/Member.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using FastEnumUtility.Internals;

Expand Down Expand Up @@ -68,6 +69,23 @@ internal Member(string name)
}
#endregion

/// <summary>
/// Gets the Attribute of specified enumration member.
/// </summary>
/// <typeparam name="TAttribute">Attribute Type</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TAttribute GetAttribute<TAttribute>()
where TAttribute : Attribute
=> FastEnum.EnumAttributeCache<T, TAttribute>.Cache[Value];

/// <summary>
/// Gets the Attributes of specified enumration member.
/// </summary>
/// <typeparam name="TAttribute">Attribute Type</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IReadOnlyList<TAttribute> GetAttributes<TAttribute>()
where TAttribute : Attribute
=> FastEnum.EnumAttributesCache<T, TAttribute>.Cache[Value];

#region Classes
/// <summary>
Expand Down