Is there a way to write this without a GoTo statement?

Viewed 327

EDIT: This is not a question about whether it's OK to use a GoTo statement.

It is a question about how to handle the center of an O(n^3) algorithm in .NET/IL without using a GoTo statement. Adherents and fellow travelers of Dijkstra's philosophies, please take note before failing to read the question.

Consider the following code, in which for most use cases the contents of the For o = 0 to nz loop will be executed between 3 million and 18 million times. The subroutine takes its place in my code as an argument for a Parallel.For() call. The domain of m, ny, and nz are all between 10 and 300.

It is hand-optimized to avoid stack pushes and subroutine calls, in other words, for speed. My desire is to avoid a compilation to IL which includes a calli or call opcode inside the innermost loop.

To abort the innermost three loops once a test is satisfied, I use a GoTo statement to abort unneeded tests.

The question is, is there a way to code this without the GoTo? Is there a way to code this which the .net JIT-Compiler will compile to faster code without call or calli opcodes ending up in the object code?

Sub SomeLambda(m As Integer, newarray As Short(,,))
    For n = 0 To ny
        For o = 0 To nz
            If newarray(m, n, o) <> 1 AndAlso newarray(m, n, o) <> -1 Then
                For m1 = m - 1 To m + 1
                    For n1 = n - 1 To n + 1
                        For o1 = o - 1 To o + 1
                            If SomeCondition = True Then 'the array is not out of bounds '
                                Dim testVal = newarray(m1, n1, o1)
                                If testVal = -1 Then
                                    newarray(m, n, o) = -2
                                    GoTo Exitloopslabel2
                                End If
                            End If
                        Next
                    Next
                Next
   Exitloopslabel2: 
            End If
        Next
    Next
End Sub
4 Answers
Related