Bounds checks: multiple monotonically increasing counters (within bounds)
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- csharp
- Domain
- compilers, performance
Research direction
Start with the DecodeBytes_safe sample and compare its current generated assembly with the bounds-check behavior described in the issue. Investigate the JIT area responsible for optimizing multiple monotonically increasing counters; done means the generated loop no longer retains the unnecessary bounds check while preserving the required range-check failure behavior.
Written by the indexing model from the issue text.
Description
A popular loop pattern when there are two (or more) monotonically increasing counters, unfortunately, JIT doesn't optimize bounds checks for them.
public int DecodeBytes_safe(Span<byte> buffer)
{
int j = 0;
for (int i = 0; i < buffer.Length; i++)
{
// e.g. under some condition
buffer[j++] = 0;
}
return j;
}
Current codegen:
; Method Progam:DecodeBytes_safe(System.Span`1[ubyte]):int:this (FullOpts)
G_M20838_IG01: ;; offset=0x0000
sub rsp, 40
G_M20838_IG02: ;; offset=0x0004
mov rax, bword ptr [rdx]
mov ecx, dword ptr [rdx+0x08]
xor edx, edx
test ecx, ecx
jg SHORT G_M20838_IG06
G_M20838_IG03: ;; offset=0x0010
mov r10d, edx
G_M20838_IG04: ;; offset=0x0013
mov eax, r10d
G_M20838_IG05: ;; offset=0x0016
add rsp, 40
ret
G_M20838_IG06: ;; offset=0x001B
mov r8d, ecx
align [2 bytes for IG07]
G_M20838_IG07: ;; offset=0x0020
lea r10d, [rdx+0x01]
cmp edx, ecx
jae SHORT G_M20838_IG09
mov edx, edx
mov byte ptr [rax+rdx], 0
dec r8d
je SHORT G_M20838_IG04
G_M20838_IG08: ;; offset=0x0033
mov edx, r10d
jmp SHORT G_M20838_IG07
G_M20838_IG09: ;; offset=0x0038
call CORINFO_HELP_RNGCHKFAIL
int3
; Total bytes of code: 62
- 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 ·