Optimized switch statements

Started by
4 comments, last by Kest 16 years, 11 months ago
Is there any automatic optimization behind the C/C++ switch-case statements when it's converted to asm? For example, if you have a switch statement with 500 cases, you could reduce comparisons to a max of 251 with a simple if( value < half ). Is this the reason behind only const values as case labels? Sorry for the noob-like question in here.
Advertisement
The main reason behind case labels being constant is that otherwise one cannot verify that the case labels are distinct. Performance is a secondary reason, because jumping to the correct code can be done in a single operation (for instance, if the switch case labels are consecutive values).
Quote:Original post by Kest
Is there any automatic optimization behind the C/C++ switch-case statements when it's converted to asm?

There's a couple. For densely packed cases, a jump table is usually generated. For sparse cases, some compilers use a binary search, or a perfect hash to a jump table. All of these things require that the cases be constant in order to preserve the performance benefits.
Quote:Original post by Sneftel
Quote:Original post by Kest
Is there any automatic optimization behind the C/C++ switch-case statements when it's converted to asm?

There's a couple. For densely packed cases, a jump table is usually generated. For sparse cases, some compilers use a binary search, or a perfect hash to a jump table. All of these things require that the cases be constant in order to preserve the performance benefits.

That's really nice to know. I was hoping that sort of thing may be happening.

Quote:Original post by ToohrVyk
The main reason behind case labels being constant is that otherwise one cannot verify that the case labels are distinct. Performance is a secondary reason, because jumping to the correct code can be done in a single operation (for instance, if the switch case labels are consecutive values).

Is consecutive order of the case labels important? If you have a large list of case labels which could be in order, with all of them breaking or returning, but have them listed in the incorrect order, will that affect the optimization? In other words, where the case labels could be consecutive, but your listing of them is not?
I can't imagine that compiler developers wouldn't do that, given how easy it would be. You wouldn't even need to restrict it to situations where all case statements had breaks.

In these sorts of situations, however, there's no substitute for compiling some test code and looking at the generated assembly.
That I can do. I'm just not entirely sure I'll understand what I'm looking at. But it can't hurt to give it a try.

Thanks for your help.

This topic is closed to new replies.

Advertisement