Skip to content

Faster Object.GetType #88631

Open
Open
@EgorBo

Description

@EgorBo
Type Test(object o) => o.GetType();

Current codegen:

; Method Test(System.Object):System.Type:this (FullOpts)
       sub      rsp, 40
       mov      rcx, rdx
       call     System.Object:GetType():System.Type:this
       nop
       add      rsp, 40
       ret      
; Total bytes of code: 18

This calls directly into the internal vm helper, although, JIT special cases Object.GetType into GT_INTRINSIC even today to fold some type comparisons. We can slightly help jit here to generate an inlined access to Type object. Basically, it's:

nuint handle = RuntimeHelpers.GetMethodTable(o)->WriteableData->ExposedClassObject;
if (handle & 1)
    return (Type)(handle -1); // direct frozen RuntimeType object 
else
    return <fallback to internal call>

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions