Details
-
Improvement
-
Resolution: Fixed
-
Minor
-
None
-
1
Description
In order to support query generation, Linq2Couchbase uses the configured ITypeSerializer. The only route to get to any configuration is via IBucket. Currently, Linq2Couchbase is getting the serializer from ClientConfiguration.Serializer via IBucket.Cluster.Configuration.
However, ClientConfiguration.Serializer isn't actually the serializer, but a factory function that creates a serializer. This means that for each query, Linq2Couchbase is instantiating a new serializer instance, causing unnecessary heap allocations. This could be somewhat optimized by caching in BucketContext, but since BucketContext is intended to be short-lived (i.e. per MVC request) the value would be minimal.
If instead we expose the actual instance of ITypeSerializer from IBucket (which is generally a singleton), Linq2Couchbase will be able to avoid the unnecessary instantiation of additional ITypeSerializers. Additionally, it will make caching the results of some method calls on the type serializer effective (see https://github.com/brantburnett/Linq2Couchbase/blob/master/Src/Couchbase.Linq/Serialization/DefaultDateTimeSerializationFormatProvider.cs#L33), further improving query generation performance.