mongodb/mongo-csharp-driver — pull request #97 by optimiz3 (2012-04-21 08:19:57+00:00)
Title: Improve deserialization performance by ~22%.

Deserialization CPU performance was suffering because both LookupDiscriminatorConvention and LookupSerializer were being called for nearly each class member. Both functions take out and release the global config read-lock. Profiling showed that around 25% of all CPU time was being spent in ReaderWriterLockSlim.TryEnterReadLock and ReaderWriterLockSlim.ExitReadLock due to these functions.

Fix makes the BsonClassMap and BsonMemberMap persist references to the appropriate IDiscriminatorConvention and IBsonSerializer singletons. By doing this, lookups are avoided while maintaining semantic correctness due to the associated BsonClassMap and BsonMemberMap types never changing once constructed.

Also introduced is the FastSingleton class, which extends the existing runtime singleton logic as follows:
1.	Enables more static singleton binding both at compile time and when using generics.
2.	Enables lazy singleton instantiation
3.	Decouples singleton data structure logic from type-specific instantiation logic.
4.	Provides structural performance advantages by isolating runtime binding reader/writer locks and dictionaries by use-case.
Other issues addressed:
1.	DefaultSerializers are now always lazy-bound. This simplified the implementation by ensuring singletons are only set once in the case where a user has specified a custom serializer.
2.	BsonSerializer.Serialize had a bug where a class implementing IBsonSerializable would always be serialized using the IBsonSerializable implemention, even if the implementation was overridden by a registered IBsonSerializer.