IL2CPP converts switch statement from jump table to sequence of ifs

I have simple switch statement in code

switch( _aMap )
{
   case RemapKind.Xp:    _mover.MoveXPositive( ); break;
   case RemapKind.Xn:    _mover.MoveXNegative( ); break;
   case RemapKind.Yp:    _mover.MoveYPositive( ); break;
   case RemapKind.Yn:    _mover.MoveYNegative( ); break;
   case RemapKind.Zp:    _mover.MoveZPositive( ); break;
   case RemapKind.Zn:    _mover.MoveZNegative( ); break;
}

in IL it is simple instruction that generates jump table:
IL_0009: switch (IL_002b, IL_0041, IL_0057, IL_006d, IL_0083, IL_0099)

but IL2CPP generate sequence of ifs:

uint8_t L_0 = (uint8_t)__this->get__cMap_3();
V_0 = (uint8_t)L_0;
uint8_t L_1 = V_0;
if (L_1 == 0)
{
    goto IL_002a;
}
if (L_1 == 1)
{
    goto IL_0040;
}
if (L_1 == 2)
{
    goto IL_0056;
}
if (L_1 == 3)
{
    goto IL_006c;
}
if (L_1 == 4)
{
    goto IL_0082;
}
if (L_1 == 5)
{
    goto IL_0098;
}

WHY Sequence of ifs why not C++ switch statement that can be optimized to 2 jump instructions without branching at all?

What performance I will have for big switches with may be 200 cases?
200 ifs vs 2 jumps?

may be I not understand some thing and C++ compiler can convert this huge if sequence into few jumps?
Can UT explain this? Because looks like we need avoid using switch statement at all in our code.

Maybe @JoshPeterson can answer your question. :slight_smile:

I can’t say exactly why IL2CPP generates a sequence of if statements in this case. I don’t recall why the code was written that way. I suspect it was just done so arbitrarily. I suspect that the difference doesn’t have an impact on the resulting assembly code that the C++ compiler generates. For example, check out this case: Compiler Explorer

Here, the clang generates the exact same assembly code.

With that said, the latest patch releases of Unity actually generate a C++ switch statement, not a series of if statements, for this code. We needed to work around what is probably an obscure compiler bug in Xcode 8.3.

Thanks a lot :slight_smile:

Your answer and asspecially tool was very helpfull :slight_smile: