AdrianStrugala / AvroConvert

Rapid Avro serializer for C# .NET
Other
102 stars 27 forks source link

Reduce Lazy/Func allocations by using valueFactory instead of a value #134

Closed gmanvel closed 11 months ago

gmanvel commented 11 months ago

This PR removes unnecessary allocations of Lazy/Func instances by using a valueFactory instead of a value when using GetOrAdd method on concurrent dictionary.

Allocations before (48 Mb):

Existing

Allocations after (40 Mb):

optimized

To Profile I used Profiler project using the following code

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        Fixture fixture = new Fixture();
        var data = fixture
            .Build<User>()
            .With(u => u.Offerings, fixture.CreateMany<Offering>(50).ToList)
            .CreateMany(1000).ToArray();

        var serialized = AvroConvert.Serialize(data);
    }
}