-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.8.0
-
Component/s: Performance, Serialization
-
None
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_)