Farfetch / kafkaflow

Apache Kafka .NET Framework to create applications simple to use and extend.
https://farfetch.github.io/kafkaflow/
MIT License
634 stars 110 forks source link

[Feature Request]: DefaultTypeResolver caching #437

Open adambajguz opened 11 months ago

adambajguz commented 11 months ago

Is your request related to a problem you have?

I think adding a cache in DefaultTypeResolver can improve performance and memoery allocations.

https://github.com/Farfetch/kafkaflow/blob/d662b9dde9d627a7ef6db795368eec5b58f96ba1/src/KafkaFlow.Serializer/DefaultTypeResolver.cs#L15

Describe the solution you'd like

private readonly ConcurrentDictionary<string, Type> _consumeTypeCache = new(StringComparer.Ordinal);
private readonly ConcurrentDictionary<Type, string> _produceTypeCache = new();

(...)
return typeName is null ?
   null :
   _consumeTypeCache.GetOrAdd(typeName, Type.GetType);

(...)

string messageTypeName = _produceTypeCache.GetOrAdd(
    messageType,
    static messageType => $"{messageType.FullName}, {messageType.Assembly.GetName().Name}");

context.Headers.SetString(MessageType, messageTypeName);

Are you able to help bring it to life and contribute with a Pull Request?

Yes

Additional context

No response

adambajguz commented 11 months ago

Also why DefaultTypeResolver is basically a transient dependency?