JIT bit test is not recognized for pattern matching syntax
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
Start with the two C# functions in the issue and reproduce their generated assembly, comparing the pattern-matching form with the explicit comparisons. Trace how the JIT handles the pattern and bit-test opportunity; done means the pattern-matching form can produce equivalent bit-test code rather than the larger jump table.
Written by the indexing model from the issue text.
Description
Reported in https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-9/?commentid=21177#comment-21177
The following two functions emit different codegen (suboptimal in case of Test1):
void Test1(char c)
{
if (c is ' ' or '\t' or '\r' or '\n')
Console.WriteLine("Hello, World!");
}
void Test2(char c)
{
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
Console.WriteLine("Hello, World!");
}
Codegen:
; Assembly listing for method Program:Test1(ushort):this (FullOpts)
movzx rcx, dx
lea eax, [rcx-0x09]
cmp eax, 4
ja SHORT G_M39625_IG04
mov ecx, eax
lea rax, [reloc @RWD00]
mov eax, dword ptr [rax+4*rcx]
lea rdx, G_M39625_IG02
add rax, rdx
jmp rax
G_M39625_IG04:
cmp ecx, 32
je SHORT G_M39625_IG06
G_M39625_IG05:
ret
G_M39625_IG06:
mov rcx, 0x21100204D88 ; 'Hello, World!'
tail.jmp [System.Console:WriteLine(System.String)]
RWD00 dd 00000029h ; case G_M39625_IG06
dd 00000029h ; case G_M39625_IG06
dd 00000028h ; case G_M39625_IG05
dd 00000028h ; case G_M39625_IG05
dd 00000029h ; case G_M39625_IG06
; Total bytes of code 57
; Assembly listing for method Program:Test2(ushort):this (FullOpts)
movzx rcx, dx
cmp ecx, 32
ja SHORT G_M37482_IG04
mov eax, 0xFFFFD9FF
bt rax, rcx
jae SHORT G_M37482_IG05
G_M37482_IG04:
ret
G_M37482_IG05:
mov rcx, 0x21100204D88 ; 'Hello, World!'
tail.jmp [System.Console:WriteLine(System.String)]
; Total bytes of code 36
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·