Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2556

Reduce memory allocations in BsonSerializerRegistry.GetSerializer()

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.8.0
    • Component/s: Performance, Serialization
    • Labels:
      None
    • Fully Compatible

      The BsonSerializerRegistry.GetSerializer() gets called repeatedly on the hot path during serialization/deserialization. It allocates memory due to an implicit closure of the current instance variable ("this").

      This allocation can be avoided.

      For the versions of the .NET frameworks which we are dealing with (.NET 4.5.2/.NET Standard 1.5) the following approach would work: https://github.com/dotnet/corefx/issues/394

      From .NET 4.7.2 onwards it is possible to simply switch to a special overload of the ConcurrentDictionary.GetOrAdd() method which accepts a factory argument as described here https://github.com/dotnet/corefx/pull/1783 and documented here (https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.getoradd?view=netframework-4.7.2#System_Collections_Concurrent_ConcurrentDictionary_2_GetOrAdd__1__0_System_Func__0___0__1____0_)

            Assignee:
            Unassigned Unassigned
            Reporter:
            daniel.hegener@gmx.net Daniel Hegener
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated: