First is making gettersDictionary static, so that it is reused across AvroConvert.Serialize calls, e.g. currently this will cause gettersDictionary to be populated on each AvroConvert.Serialize call
var users = new List<User>();
AvroConvert.Serialize(users);
AvroConvert.Serialize(users);
Making it static will ensure it is populated only once. Given that GenerateGetValue is an expensive operation eliminating its execution would improve performance.
Second suggested improvement is gettersDictionary population if race conditions occur. Normally if 2 or more threads try to execute GenerateGetValue concurrently to store the value in dictionary, all of them will execute but only single return value will be used. Since GenerateGetValue is an expensive operation, using Lazy as its value will ensure that even if multiple threads try to create value, they will create Lazy instance which is much cheaper. Later on when value is accessed, Lazy will ensure only 1 GenerateGetValue is executed avoiding to run multiple expensive operations altogether. See this blog post for more details about this improvement.
This PR introduces 2 small improvements.
First is making
gettersDictionary
static, so that it is reused acrossAvroConvert.Serialize
calls, e.g. currently this will causegettersDictionary
to be populated on eachAvroConvert.Serialize
callMaking it static will ensure it is populated only once. Given that
GenerateGetValue
is an expensive operation eliminating its execution would improve performance.Second suggested improvement is
gettersDictionary
population if race conditions occur. Normally if 2 or more threads try to executeGenerateGetValue
concurrently to store the value in dictionary, all of them will execute but only single return value will be used. SinceGenerateGetValue
is an expensive operation, usingLazy
as its value will ensure that even if multiple threads try to create value, they will createLazy
instance which is much cheaper. Later on when value is accessed,Lazy
will ensure only 1GenerateGetValue
is executed avoiding to run multiple expensive operations altogether. See this blog post for more details about this improvement.