4 ms·
This article is almost ten years old, I believe that modern compilers all create efficient dispatch tables without all of this manual work - even ten years ago
by zxcvbn4038 6y ago
This article is almost ten years old, I believe that modern compilers all create efficient dispatch tables without all of this manual work - even ten years ago I don’t think large switch statements were a problem area. Most emulators use these heavily. But I think it does give an idea what is going on behind the scenes. There are a number of ways compilers could implement this based on the number of cases and how dense they are. I know that go will organize sparse case values into a sorted array and do a binary search to find where to jump. I think Perl uses hash tables for the same purpose.
- hansvm 6y agoIt'd be interesting to analyze more languages. C# .NET Core for example still couldn't handle large switch statements well as of a year ago.
- KMag 6y agoIf you have a while-loop wrapping a switch statement, will most modern compilers put dispatch code at the end of every case, to help the branch predictor (actually, branch target buffer)? Even if modern branch target buffer entries were dependent upon the target of the previous indirect branch (in the switch statement), if any of your bytecodes go off and do some C++ virtual dispatches, it will quickly swamp any extra context the BTB logic may have.
- chj 6y agoAs of today, Computed goto is still much faster than switch, both on x64 or arm64.