Skip to content
Merged
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
13 changes: 13 additions & 0 deletions src/libs/FastEnum.Core/Internals/JitOptimization.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Buffers;
using System.Collections.Frozen;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -460,4 +461,16 @@ public readonly struct Entry<TValue>(ulong key, TValue value)
}
#endregion
}



public static class MemberHelper
{
#region Methods
public static FrozenDictionary<int, string?> CreateLabels(FieldInfo field)
=> field
.GetCustomAttributes<LabelAttribute>()
.ToFrozenDictionary(static x => x.Index, static x => x.Value);
#endregion
}
}
19 changes: 2 additions & 17 deletions src/libs/FastEnum.Core/Member.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text;
using FastEnumUtility.Internals;

namespace FastEnumUtility;

Expand Down Expand Up @@ -74,7 +75,7 @@ internal Member(string name, T value, FieldInfo field)
this.NameUtf8 = ImmutableCollectionsMarshal.AsImmutableArray(Encoding.UTF8.GetBytes(name));
this.FieldInfo = field;
this.EnumMemberAttribute = this.FieldInfo.GetCustomAttribute<EnumMemberAttribute>();
this.Labels = MemberHelper.CreateLabels(this.FieldInfo);
this.Labels = JitOptimization.MemberHelper.CreateLabels(this.FieldInfo);
}


Expand All @@ -93,22 +94,6 @@ public void Deconstruct(out string name, out T value)



internal static class MemberHelper
{
// note:
// - Methods here are non-generic, so they are compiled by JIT only once and shared by all enum types.
// - Lambdas written in Member<T> belong to its generic closure class, so they are compiled for each enum type.

#region Methods
public static FrozenDictionary<int, string?> CreateLabels(FieldInfo field)
=> field
.GetCustomAttributes<LabelAttribute>()
.ToFrozenDictionary(static x => x.Index, static x => x.Value);
#endregion
}



internal interface IMemberValue<T>
{
public T Value { get; }
Expand Down