From 68e8918b5136c113b64ff89e1b6ee81d9aab770d Mon Sep 17 00:00:00 2001 From: Michal Strehovsky Date: Fri, 16 Feb 2018 08:51:38 -0800 Subject: [PATCH] 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] --- .../Runtime/FieldInfos/RuntimeFieldInfo.cs | 10 +++++++++- .../Runtime/PropertyInfos/RuntimePropertyInfo.cs | 12 ++++++++++-- 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 392f6db9e3c..31de164abb1 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 @@ public sealed override Type FieldType { get { - return this.FieldRuntimeType; + Type fieldType = _lazyFieldType; + if (fieldType == null) + { + _lazyFieldType = fieldType = this.FieldRuntimeType; + } + + return fieldType; } } @@ -292,6 +298,8 @@ protected RuntimeFieldInfo WithDebugName() 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 7691902beb1..b8623f72c44 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 @@ public sealed override Type PropertyType 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 @@ private object GetConstantValue(bool raw) private volatile ParameterInfo[] _lazyIndexParameters; + private volatile Type _lazyPropertyType; + private String _debugName; } }