Skip to content

Commit 01b57cc

Browse files
Speed up resolving members of types (#79784)
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.
1 parent 2d1ca83 commit 01b57cc

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/coreclr/tools/Common/TypeSystem/Ecma/EcmaModule.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Reflection.Metadata.Ecma335;
99
using System.Reflection.PortableExecutable;
1010

11+
using Debug = System.Diagnostics.Debug;
12+
1113
namespace Internal.TypeSystem.Ecma
1214
{
1315
public partial class EcmaModule : ModuleDesc
@@ -376,6 +378,26 @@ public FieldDesc GetField(EntityHandle handle)
376378
return field;
377379
}
378380

381+
internal EcmaField GetField(FieldDefinitionHandle handle, EcmaType owningType)
382+
{
383+
if (!_resolvedTokens.TryGetValue(handle, out IEntityHandleObject result))
384+
{
385+
Debug.Assert(_metadataReader.GetFieldDefinition(handle).GetDeclaringType() == owningType.Handle);
386+
result = _resolvedTokens.AddOrGetExisting(new EcmaField(owningType, handle));
387+
}
388+
return (EcmaField)result;
389+
}
390+
391+
internal EcmaMethod GetMethod(MethodDefinitionHandle handle, EcmaType owningType)
392+
{
393+
if (!_resolvedTokens.TryGetValue(handle, out IEntityHandleObject result))
394+
{
395+
Debug.Assert(_metadataReader.GetMethodDefinition(handle).GetDeclaringType() == owningType.Handle);
396+
result = _resolvedTokens.AddOrGetExisting(new EcmaMethod(owningType, handle));
397+
}
398+
return (EcmaMethod)result;
399+
}
400+
379401
public object GetObject(EntityHandle handle, NotFoundBehavior notFoundBehavior = NotFoundBehavior.Throw)
380402
{
381403
IEntityHandleObject obj = _resolvedTokens.GetOrCreateValue(handle);

src/coreclr/tools/Common/TypeSystem/Ecma/EcmaType.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public override IEnumerable<MethodDesc> GetMethods()
305305
{
306306
foreach (var handle in _typeDefinition.GetMethods())
307307
{
308-
yield return (EcmaMethod)_module.GetObject(handle);
308+
yield return _module.GetMethod(handle, this);
309309
}
310310
}
311311

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

@@ -329,7 +329,7 @@ public override MethodDesc GetMethod(string name, MethodSignature signature, Ins
329329
{
330330
if (stringComparer.Equals(metadataReader.GetMethodDefinition(handle).Name, name))
331331
{
332-
var method = (EcmaMethod)_module.GetObject(handle);
332+
var method = _module.GetMethod(handle, this);
333333
if (signature == null || signature.Equals(method.Signature.ApplySubstitution(substitution)))
334334
return method;
335335
}
@@ -349,7 +349,7 @@ public override MethodDesc GetStaticConstructor()
349349
if (methodDefinition.Attributes.IsRuntimeSpecialName() &&
350350
stringComparer.Equals(methodDefinition.Name, ".cctor"))
351351
{
352-
var method = (EcmaMethod)_module.GetObject(handle);
352+
var method = _module.GetMethod(handle, this);
353353
return method;
354354
}
355355
}
@@ -372,7 +372,7 @@ public override MethodDesc GetDefaultConstructor()
372372
if (attributes.IsRuntimeSpecialName() && attributes.IsPublic()
373373
&& stringComparer.Equals(methodDefinition.Name, ".ctor"))
374374
{
375-
var method = (EcmaMethod)_module.GetObject(handle);
375+
var method = _module.GetMethod(handle, this);
376376
MethodSignature sig = method.Signature;
377377

378378
if (sig.Length != 0)
@@ -424,7 +424,7 @@ public override IEnumerable<FieldDesc> GetFields()
424424
{
425425
foreach (var handle in _typeDefinition.GetFields())
426426
{
427-
var field = (EcmaField)_module.GetObject(handle);
427+
var field = _module.GetField(handle, this);
428428
yield return field;
429429
}
430430
}
@@ -438,7 +438,7 @@ public override TypeDesc UnderlyingType
438438

439439
foreach (var handle in _typeDefinition.GetFields())
440440
{
441-
var field = (EcmaField)_module.GetObject(handle);
441+
var field = _module.GetField(handle, this);
442442
if (!field.IsStatic)
443443
return field.FieldType;
444444
}
@@ -456,7 +456,7 @@ public override FieldDesc GetField(string name)
456456
{
457457
if (stringComparer.Equals(metadataReader.GetFieldDefinition(handle).Name, name))
458458
{
459-
var field = (EcmaField)_module.GetObject(handle);
459+
var field = _module.GetField(handle, this);
460460
return field;
461461
}
462462
}
@@ -560,7 +560,7 @@ public override ClassLayoutMetadata GetClassLayout()
560560
// Note: GetOffset() returns -1 when offset was not set in the metadata
561561
int specifiedOffset = fieldDefinition.GetOffset();
562562
result.Offsets[index] =
563-
new FieldAndOffset((EcmaField)_module.GetObject(handle), specifiedOffset == -1 ? FieldAndOffset.InvalidOffset : new LayoutInt(specifiedOffset));
563+
new FieldAndOffset(_module.GetField(handle, this), specifiedOffset == -1 ? FieldAndOffset.InvalidOffset : new LayoutInt(specifiedOffset));
564564

565565
index++;
566566
}

0 commit comments

Comments
 (0)