Is there a way to remove the array bounds check in C#
?
here is what I want to achieve:
public static int F(int[] M, int i)
{
return M[i]; // I can guarantee that [i] will never be outside of [0, M.Length]
}
Before this function call I have a logic which already does check for the bounds (with some extra logic into it). The thing I want to remove are following lines:
Program.F(Int32[], Int32)
L0000: sub rsp, 0x28
L0004: cmp edx, [rcx+8] ; I don't need this line
L0007: jae short L0015 ; I don't need this line
L0009: movsxd rax, edx
L000c: mov eax, [rcx+rax*4+0x10]
L0010: add rsp, 0x28
L0014: ret
L0015: call 0x00007ffc8877bc70 ; I don't need this line
L001a: int3 ; I don't need this line
Question
Is there a way of removing those instructions?
Note
- I tried to put an if check with a hope that the compiler will get that but it made the situation even worse.
public static int G(int[] M, int i)
{
if (i >= 0 && i < M.Length)
return M[i];
return -1;
}
this generates:
Program.G(Int32[], Int32)
L0000: sub rsp, 0x28
L0004: test edx, edx
L0006: jl short L001f
L0008: mov eax, [rcx+8]
L000b: cmp eax, edx
L000d: jle short L001f
L000f: cmp edx, eax
L0011: jae short L0029
L0013: movsxd rax, edx
L0016: mov eax, [rcx+rax*4+0x10]
L001a: add rsp, 0x28
L001e: ret
L001f: mov eax, 0xffffffff
L0024: add rsp, 0x28
L0028: ret
L0029: call 0x00007ffc8877bc70
L002e: int3
as you can see it didn't help.
- What I can do is: using
unsafe
:
public static unsafe int H(int* M, int i)
{
return M[i];
}
this generates what I was looking for:
Program.H(Int32*, Int32)
L0000: movsxd rax, edx
L0003: mov eax, [rcx+rax*4]
L0006: ret
But I sadly can't enable unsafe for my project. Is there a solution in "non-unsafe" world?
unsafe
version is faster, than the regular one. But as I said It'll be hard for me to enable unsafe in my project. It is also hard to include the benchmark because it has lot of dependencies and clearing them out and including them into my question would take too much time (+ I don't think that changing the code would give us accurate results here).0 .. ar.Length
took 803μs, fsumlength - 1 .. 0
891μs and without check optimizations0 .. length
918μs on my PC