Skip to content

RyuJIT: Intrinsify GetType().TypeHandle.Value and typeof(T).TypeHandle.Value #5973

@omariom

Description

@omariom

Sometimes I use IntPtr instead of Type as a key (or part of a complex key) in Dictionary.
Advantages is that it is a value type so I don't keep full blown Type object alive, and GC likes value type fields more than ref ones because less references to follow.

But each time I need to get the value or check if a key exists I have to get Type just to retrieve its handle value.

I suggest intrinsifying these calls:

[obj.]GetType().TypeHandle.Value typeof(T).TypeHandle.Value

How

In the following code JIT just compares method table pointer in the object header to the address of method table of String type.

[MethodImpl(MethodImplOptions.NoInlining)] private static bool TestMtCheck(object obj) { return obj.GetType() == typeof(String); }
 mov rax,7FEF61BDA88h   cmp qword ptr [rcx],rax  sete al   movzx eax,al   ret 

It could do the same when getting type handle value :

[MethodImpl(MethodImplOptions.NoInlining)] private static IntPtr TestGetHandle(object obj) { return obj.GetType().TypeHandle.Value; } MethodImpl(MethodImplOptions.NoInlining)] private static IntPtr TestGetHandle() { return typeof(String).TypeHandle.Value; }
mov rax, qword ptr [rcx] ret
mov rax, 7FEF61BDA88h  ret

category:cq
theme:type-intrinsics
skill-level:expert
cost:medium

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIenhancementProduct code improvement that does NOT require public API changes/additionsoptimizationtenet-performancePerformance related issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions