未验证 提交 13d8d2ec 编写于 作者: S Steve Harter 提交者: GitHub

For perf, remove Invoker pattern for fields (#74614)

上级 6c3c2347
......@@ -18,23 +18,12 @@ internal sealed unsafe class RtFieldInfo : RuntimeFieldInfo, IRuntimeFieldInfo
// lazy caching
private string? m_name;
private RuntimeType? m_fieldType;
internal FieldAccessor? m_invoker;
private InvocationFlags m_invocationFlags;
internal InvocationFlags InvocationFlags
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (Invoker._invocationFlags & InvocationFlags.Initialized) != 0 ?
Invoker._invocationFlags : InitializeInvocationFlags();
}
private FieldAccessor Invoker
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
m_invoker ??= new FieldAccessor(this);
return m_invoker;
}
get => (m_invocationFlags & InvocationFlags.Initialized) != 0 ?
m_invocationFlags : InitializeInvocationFlags();
}
[MethodImpl(MethodImplOptions.NoInlining)]
......@@ -67,7 +56,7 @@ private InvocationFlags InitializeInvocationFlags()
}
// must be last to avoid threading problems
return Invoker._invocationFlags = invocationFlags | InvocationFlags.Initialized;
return m_invocationFlags = invocationFlags | InvocationFlags.Initialized;
}
#endregion
......@@ -113,63 +102,6 @@ internal override bool CacheEquals(object? o)
return o is RtFieldInfo m && m.m_fieldHandle == m_fieldHandle;
}
[DebuggerStepThrough]
[DebuggerHidden]
internal object? GetValueNonEmit(object? obj)
{
RuntimeType? declaringType = DeclaringType as RuntimeType;
RuntimeType fieldType = (RuntimeType)FieldType;
bool domainInitialized = false;
if (declaringType == null)
{
return RuntimeFieldHandle.GetValue(this, obj, fieldType, null, ref domainInitialized);
}
else
{
domainInitialized = declaringType.DomainInitialized;
object? retVal = RuntimeFieldHandle.GetValue(this, obj, fieldType, declaringType, ref domainInitialized);
declaringType.DomainInitialized = domainInitialized;
return retVal;
}
}
[DebuggerStepThrough]
[DebuggerHidden]
internal void SetValueNonEmit(object? obj, object? value)
{
RuntimeType? declaringType = DeclaringType as RuntimeType;
RuntimeType fieldType = (RuntimeType)FieldType;
bool domainInitialized = false;
if (declaringType == null)
{
RuntimeFieldHandle.SetValue(
this,
obj,
value,
fieldType,
Attributes,
declaringType: null,
ref domainInitialized);
}
else
{
domainInitialized = declaringType.DomainInitialized;
RuntimeFieldHandle.SetValue(
this,
obj,
value,
fieldType,
Attributes,
declaringType,
ref domainInitialized);
declaringType.DomainInitialized = domainInitialized;
}
}
#endregion
#region MemberInfo Overrides
......@@ -211,7 +143,20 @@ internal override RuntimeModule GetRuntimeModule()
CheckConsistency(obj);
return Invoker.GetValue(obj);
RuntimeType fieldType = (RuntimeType)FieldType;
bool domainInitialized = false;
if (declaringType == null)
{
return RuntimeFieldHandle.GetValue(this, obj, fieldType, null, ref domainInitialized);
}
else
{
domainInitialized = declaringType.DomainInitialized;
object? retVal = RuntimeFieldHandle.GetValue(this, obj, fieldType, declaringType, ref domainInitialized);
declaringType.DomainInitialized = domainInitialized;
return retVal;
}
}
public override object GetRawConstantValue() { throw new InvalidOperationException(); }
......@@ -258,7 +203,17 @@ public override void SetValue(object? obj, object? value, BindingFlags invokeAtt
fieldType.CheckValue(ref value, copyBack: ref _ref, binder, culture, invokeAttr);
}
Invoker.SetValue(obj, value);
bool domainInitialized = false;
if (declaringType is null)
{
RuntimeFieldHandle.SetValue(this, obj, value, fieldType, m_fieldAttributes, null, ref domainInitialized);
}
else
{
domainInitialized = declaringType.DomainInitialized;
RuntimeFieldHandle.SetValue(this, obj, value, fieldType, m_fieldAttributes, declaringType, ref domainInitialized);
declaringType.DomainInitialized = domainInitialized;
}
}
[DebuggerStepThrough]
......
......@@ -1185,7 +1185,6 @@ public RuntimeFieldInfoStub(RuntimeFieldHandleInternal fieldHandle, object keepa
private object? m_d;
private int m_b;
private object? m_e;
private object? m_f;
private RuntimeFieldHandleInternal m_fieldHandle;
#pragma warning restore 414, 169
......
......@@ -1153,7 +1153,6 @@ protected:
INT32 m_empty2;
OBJECTREF m_empty3;
OBJECTREF m_empty4;
OBJECTREF m_empty5;
FieldDesc * m_pFD;
public:
......
......@@ -617,7 +617,6 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\EventInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\ExceptionHandlingClause.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\ExceptionHandlingClauseOptions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\FieldAccessor.cs" Condition="'$(FeatureNativeAot)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\FieldAttributes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\FieldInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\GenericParameterAttributes.cs" />
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
namespace System.Reflection
{
internal sealed partial class FieldAccessor
{
private readonly RtFieldInfo _fieldInfo;
public InvocationFlags _invocationFlags;
public FieldAccessor(RtFieldInfo fieldInfo)
{
_fieldInfo = fieldInfo;
}
[DebuggerStepThrough]
[DebuggerHidden]
public object? GetValue(object? obj)
{
// Todo: add strategy for calling IL Emit-based version
return _fieldInfo.GetValueNonEmit(obj);
}
[DebuggerStepThrough]
[DebuggerHidden]
public void SetValue(object? obj, object? value)
{
// Todo: add strategy for calling IL Emit-based version
_fieldInfo.SetValueNonEmit(obj, value);
}
}
}
......@@ -38,8 +38,6 @@ internal abstract class RtFieldInfo : FieldInfo
internal abstract object UnsafeGetValue(object obj);
internal abstract void UnsafeSetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
internal abstract void CheckConsistency(object target);
internal abstract object? GetValueNonEmit(object? obj);
internal abstract void SetValueNonEmit(object? obj, object? value);
}
[StructLayout(LayoutKind.Sequential)]
......@@ -51,19 +49,8 @@ internal sealed class RuntimeFieldInfo : RtFieldInfo
private string? name;
private Type? type;
private FieldAttributes attrs;
private FieldAccessor? invoker;
#pragma warning restore 649
private FieldAccessor Invoker
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
invoker ??= new FieldAccessor(this);
return invoker;
}
}
public override Module Module
{
get
......@@ -128,13 +115,6 @@ public override void SetValueDirect(TypedReference obj, object value)
}
}
[DebuggerStepThrough]
[DebuggerHidden]
internal override void SetValueNonEmit(object? obj, object? value)
{
SetValueInternal(this, obj, value);
}
[DebuggerStepThrough]
[DebuggerHidden]
public override object GetValueDirect(TypedReference obj)
......@@ -149,13 +129,6 @@ public override object GetValueDirect(TypedReference obj)
}
}
[DebuggerStepThrough]
[DebuggerHidden]
internal override object? GetValueNonEmit(object? obj)
{
return GetValueInternal(obj);
}
public override FieldAttributes Attributes
{
get
......@@ -239,7 +212,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (!IsLiteral)
CheckGeneric();
return Invoker.GetValue(obj);
return GetValueInternal(obj);
}
public override string ToString()
......@@ -278,7 +251,7 @@ public override void SetValue(object? obj, object? val, BindingFlags invokeAttr,
}
}
Invoker.SetValue(obj, val);
SetValueInternal(this, obj, val);
}
internal RuntimeFieldInfo Clone(string newName)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册