3 ms·
I didn't even say anything about performance here. Even ignoring performance, sometimes switch() is best. For example, I wrote a very small JIT compiler for B
by haberman 13y ago
I didn't even say anything about performance here. Even ignoring performance, sometimes switch() is best.
For example, I wrote a very small JIT compiler for Brainfuck. It uses switch() to do code generation. It's extremely clean and elegant code, if I do say so myself. Breaking apart the switch() with some kind of OO approach would make the program longer and less clear: http://blog.reverberate.org/2012/12/hello-jit-world-joy-of-simple-jits.html http://blog.reverberate.org/2012/12/hello-jit-world-joy-of-s...
Do you look at that and think "this doesn't look good" just because it uses switch()? If so, your heuristic could use a little tuning.
But now let's talk performance. In interpreter/VM main loops, bytecode dispatch is a huge factor in the overall performance of a VM. This issue isn't "theoretical," "minor," or "premature," it's the result of extensive measurement and experimentation by many people over a large amount of time. If you are not familiar with this well-studied issue, here is some recommended reading:
http://software.intel.com/en-us/forums/topic/298506 http://software.intel.com/en-us/forums/topic/298506
http://lua-users.org/lists/lua-l/2011-02/msg00742.html http://lua-users.org/lists/lua-l/2011-02/msg00742.html
http://www.cs.tcd.ie/publications/tech-reports/reports.07/TCD-CS-2007-49.pdf http://www.cs.tcd.ie/publications/tech-reports/reports.07/TC... (see section 3.3)