diff options
author | Michal Strehovsky <michals@microsoft.com> | 2018-02-16 19:51:38 +0300 |
---|---|---|
committer | Michal Strehovsky <michals@microsoft.com> | 2018-02-16 19:51:38 +0300 |
commit | 8f5b47837458280b922977b541e0f03a8fa5af97 (patch) | |
tree | b604a84ca09f810d22abd9e5533d368a6e6a5919 /src/System.Private.Reflection.Core | |
parent | 0933297ef1aaec789fea48eb8816f47df6bceb7f (diff) |
Cache property type and field type on PropertyInfo/FieldInfo
Customer code was accessing the type of a cached property in a hot path. Always going back to metadata meant that we spent 20% of time resolving the property type.
I proactively applied the same fix to FieldInfo.
[tfs-changeset: 1689010]
Diffstat (limited to 'src/System.Private.Reflection.Core')
2 files changed, 19 insertions, 3 deletions
diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/FieldInfos/RuntimeFieldInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/FieldInfos/RuntimeFieldInfo.cs index 392f6db9e..31de164ab 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/FieldInfos/RuntimeFieldInfo.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/FieldInfos/RuntimeFieldInfo.cs @@ -89,7 +89,13 @@ namespace System.Reflection.Runtime.FieldInfos { get { - return this.FieldRuntimeType; + Type fieldType = _lazyFieldType; + if (fieldType == null) + { + _lazyFieldType = fieldType = this.FieldRuntimeType; + } + + return fieldType; } } @@ -292,6 +298,8 @@ namespace System.Reflection.Runtime.FieldInfos private volatile FieldAccessor _lazyFieldAccessor = null; + private volatile Type _lazyFieldType = null; + private String _debugName; } } diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/PropertyInfos/RuntimePropertyInfo.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/PropertyInfos/RuntimePropertyInfo.cs index 7691902be..b8623f72c 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/PropertyInfos/RuntimePropertyInfo.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/PropertyInfos/RuntimePropertyInfo.cs @@ -187,8 +187,14 @@ namespace System.Reflection.Runtime.PropertyInfos ReflectionTrace.PropertyInfo_PropertyType(this); #endif - TypeContext typeContext = ContextTypeInfo.TypeContext; - return PropertyTypeHandle.Resolve(typeContext); + Type propertyType = _lazyPropertyType; + if (propertyType == null) + { + TypeContext typeContext = ContextTypeInfo.TypeContext; + _lazyPropertyType = propertyType = PropertyTypeHandle.Resolve(typeContext); + } + + return propertyType; } } @@ -402,6 +408,8 @@ namespace System.Reflection.Runtime.PropertyInfos private volatile ParameterInfo[] _lazyIndexParameters; + private volatile Type _lazyPropertyType; + private String _debugName; } } |