Jump to content
  • Advertisement
Sign in to follow this  
Kest

Optimized switch statements

This topic is 4154 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!