SmartEnum icon indicating copy to clipboard operation
SmartEnum copied to clipboard

Optimize reflection dependency in SmartEnum List property for performance-sensitive scenarios

Open mokarchi opened this issue 3 months ago • 1 comments

This PR optimizes the reflection dependency through strategic caching improvements:

SmartFlagEnum Optimization

  • Added cached enum options: Introduced static readonly Lazy<List<TEnum>> _enumOptions field to match the efficiency of SmartEnum
  • Updated List property: Changed from rebuilding collections via _fromName.Value.Values.ToList().AsReadOnly() to direct cached access
  • Eliminated repeated reflection calls: Updated FromValue(), DeserializeValue(), TryFromValue(), and FromValueToString() methods to use cached data instead of calling GetAllOptions() repeatedly

Type-Level Reflection Caching

Enhanced TypeExtensions.GetFieldsOfType<T>() with ConcurrentDictionary<Type, object> caching to prevent repeated reflection operations on the same type across different instances.

Backward Compatibility

  • No breaking changes to public APIs
  • Maintains full backward compatibility
  • Thread-safe implementation preserved

mokarchi avatar Sep 30 '25 19:09 mokarchi

@ardalis Could you review this, please?

mokarchi avatar Oct 06 '25 17:10 mokarchi