its been dealt with before, cython optimizes it in a very similar way (via a switch jump table instead of loops as its code generator)