-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nullable type reflection cache #2715
base: master
Are you sure you want to change the base?
Conversation
return new NullableTypeReflectionInfo(false, t, null); | ||
|
||
#if !HAVE_CONCURRENT_DICTIONARY | ||
lock(NullableInfoCache){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lock(NullableInfoCache){ | |
lock(NullableInfoCache) { |
if (!NullableInfoCache.TryGetValue(t, out var info)) | ||
{ | ||
var genericTypeDefinition = t.GetGenericTypeDefinition(); | ||
var isNullable = (genericTypeDefinition == typeof(Nullable<>)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var isNullable = (genericTypeDefinition == typeof(Nullable<>)); | |
var isNullable = genericTypeDefinition == typeof(Nullable<>); |
dotnet/runtime#73860 |
@@ -0,0 +1,92 @@ | |||
#region License |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
License
Deserializing a class like the one bellow has a performance hit presumably due to calls to
GetGenericTypeDefinition
andNullable.GetUnderlyingType
.This is aggravated when using converters like
StringEnumConverter
which add a nullable type check atCanConvert
methodThis PR opens up the discussion of adding a simple cache for these expensive reflection calls.
On this particular example, around 30% gains at the expense of the memory for holding the cache.
Benchmark code is included.