Skip to content

Commit

Permalink
Speed up resolving members of types (#79784)
Browse files Browse the repository at this point in the history
When constructing type system representation of a type member, we need two things - the token of the member, and type system representation of the owning type.

We have a couple places where we iterate members on a type - in those places, we already have a type system representation of the owning type. Shortcut the codepath that would recompute this information.
  • Loading branch information
MichalStrehovsky authored Dec 17, 2022
1 parent 2d1ca83 commit 01b57cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
22 changes: 22 additions & 0 deletions src/coreclr/tools/Common/TypeSystem/Ecma/EcmaModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;

using Debug = System.Diagnostics.Debug;

namespace Internal.TypeSystem.Ecma
{
public partial class EcmaModule : ModuleDesc
Expand Down Expand Up @@ -376,6 +378,26 @@ public FieldDesc GetField(EntityHandle handle)
return field;
}

internal EcmaField GetField(FieldDefinitionHandle handle, EcmaType owningType)
{
if (!_resolvedTokens.TryGetValue(handle, out IEntityHandleObject result))
{
Debug.Assert(_metadataReader.GetFieldDefinition(handle).GetDeclaringType() == owningType.Handle);
result = _resolvedTokens.AddOrGetExisting(new EcmaField(owningType, handle));
}
return (EcmaField)result;
}

internal EcmaMethod GetMethod(MethodDefinitionHandle handle, EcmaType owningType)
{
if (!_resolvedTokens.TryGetValue(handle, out IEntityHandleObject result))
{
Debug.Assert(_metadataReader.GetMethodDefinition(handle).GetDeclaringType() == owningType.Handle);
result = _resolvedTokens.AddOrGetExisting(new EcmaMethod(owningType, handle));
}
return (EcmaMethod)result;
}

public object GetObject(EntityHandle handle, NotFoundBehavior notFoundBehavior = NotFoundBehavior.Throw)
{
IEntityHandleObject obj = _resolvedTokens.GetOrCreateValue(handle);
Expand Down
18 changes: 9 additions & 9 deletions src/coreclr/tools/Common/TypeSystem/Ecma/EcmaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public override IEnumerable<MethodDesc> GetMethods()
{
foreach (var handle in _typeDefinition.GetMethods())
{
yield return (EcmaMethod)_module.GetObject(handle);
yield return _module.GetMethod(handle, this);
}
}

Expand All @@ -316,7 +316,7 @@ public override IEnumerable<MethodDesc> GetVirtualMethods()
{
MethodDefinition methodDef = reader.GetMethodDefinition(handle);
if ((methodDef.Attributes & MethodAttributes.Virtual) != 0)
yield return (EcmaMethod)_module.GetObject(handle);
yield return _module.GetMethod(handle, this);
}
}

Expand All @@ -329,7 +329,7 @@ public override MethodDesc GetMethod(string name, MethodSignature signature, Ins
{
if (stringComparer.Equals(metadataReader.GetMethodDefinition(handle).Name, name))
{
var method = (EcmaMethod)_module.GetObject(handle);
var method = _module.GetMethod(handle, this);
if (signature == null || signature.Equals(method.Signature.ApplySubstitution(substitution)))
return method;
}
Expand All @@ -349,7 +349,7 @@ public override MethodDesc GetStaticConstructor()
if (methodDefinition.Attributes.IsRuntimeSpecialName() &&
stringComparer.Equals(methodDefinition.Name, ".cctor"))
{
var method = (EcmaMethod)_module.GetObject(handle);
var method = _module.GetMethod(handle, this);
return method;
}
}
Expand All @@ -372,7 +372,7 @@ public override MethodDesc GetDefaultConstructor()
if (attributes.IsRuntimeSpecialName() && attributes.IsPublic()
&& stringComparer.Equals(methodDefinition.Name, ".ctor"))
{
var method = (EcmaMethod)_module.GetObject(handle);
var method = _module.GetMethod(handle, this);
MethodSignature sig = method.Signature;

if (sig.Length != 0)
Expand Down Expand Up @@ -424,7 +424,7 @@ public override IEnumerable<FieldDesc> GetFields()
{
foreach (var handle in _typeDefinition.GetFields())
{
var field = (EcmaField)_module.GetObject(handle);
var field = _module.GetField(handle, this);
yield return field;
}
}
Expand All @@ -438,7 +438,7 @@ public override TypeDesc UnderlyingType

foreach (var handle in _typeDefinition.GetFields())
{
var field = (EcmaField)_module.GetObject(handle);
var field = _module.GetField(handle, this);
if (!field.IsStatic)
return field.FieldType;
}
Expand All @@ -456,7 +456,7 @@ public override FieldDesc GetField(string name)
{
if (stringComparer.Equals(metadataReader.GetFieldDefinition(handle).Name, name))
{
var field = (EcmaField)_module.GetObject(handle);
var field = _module.GetField(handle, this);
return field;
}
}
Expand Down Expand Up @@ -560,7 +560,7 @@ public override ClassLayoutMetadata GetClassLayout()
// Note: GetOffset() returns -1 when offset was not set in the metadata
int specifiedOffset = fieldDefinition.GetOffset();
result.Offsets[index] =
new FieldAndOffset((EcmaField)_module.GetObject(handle), specifiedOffset == -1 ? FieldAndOffset.InvalidOffset : new LayoutInt(specifiedOffset));
new FieldAndOffset(_module.GetField(handle, this), specifiedOffset == -1 ? FieldAndOffset.InvalidOffset : new LayoutInt(specifiedOffset));

index++;
}
Expand Down

0 comments on commit 01b57cc

Please sign in to comment.