Skip to content

Conversation

mokarchi
Copy link

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

Refactored lazy initialization to use a thread-safe `Lazy<List<TEnum>> _enumOptions` for caching enum options, reducing redundant calls to `GetAllOptions()`. Updated related methods and properties to leverage the cached options.

Changed the return type of `GetAllOptions` to `List<TEnum>` for better performance. Simplified the `List` property to directly use the cached options.

Introduced a `ConcurrentDictionary` in `TypeExtensions` to cache results of `GetFieldsOfType<TFieldType>`, minimizing reflection overhead.

Performed code cleanup, improved formatting, adjusted `using` directives, and ensured thread-safety with `LazyThreadSafetyMode.ExecutionAndPublication`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant