Skip to content

Severe performance issues in code generation by the JIT #35159

@ameisen

Description

@ameisen

As per this SharpLab example.

For a variety of test cases related to converting between a byte/int and a bool, the JIT (as of Core CLR v4.700.20.6603, which is what SharpLab is using), generates pretty poor code unless I perform a pointer-dereference cast between bool and byte.

What is more interesting and concerning is that I get better results by calling a utility method that does the same thing than doing it in that function. Observe:

public static unsafe int AsInteger(this bool foo) { return *(byte*)&foo; }
public static unsafe int Cast(bool foo) { return *(byte*)&foo; }
public static int CastUtility(bool foo) { return foo.AsInteger(); }

Cast's JIT output:
mov [rsp+0x8], ecx
movzx eax, byte [rsp+0x8]
ret

CastUtility's JIT output:
movzx eax, cl
ret

The codegen is actually substantially worse if I specify that I want to use Roslyn master, as well.

category:cq
theme:optimization
skill-level:intermediate
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/additionsoptimization

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions