really big switch statements

Started by
13 comments, last by foofightr 21 years, 10 months ago
I know VC++ 6 with the optimizations turned on makes very good jump tables. If the numbers are wierd it creates 2 tables, one 32-bit table for addresses and one 8-bit table for indexes to the address. Compilers are very good because they can make very optimized code rather than ASM that has to be human changeable

Creating a list of functions is no good, it is way more complicated to pass arguments and access variables and much slower than one big function

You already know a lot about assembly, so just look at the ASM listings to see if the code is any good
Advertisement
This is a fairly complex problem - which method is faster depends on a couple of different things.

If the code to handle the op code is short, inline it and put it in a switch. If the code is long, you want to make a function.

Only profiling will tell you for certain. The switch statement should be faster if the code is inlined into the switch, because no function call takes place then. However, as the code in the switch becomes larger it can cause more cache misses, and if it swells to over 4k it could cause an extra page fault (lethal).

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Preliminary tests... in Release mode

big switch statement, emulator runs at ~31MHz
array of func ptrs, emulator runs at ~28MHz

I think the speed gain of the "jump table" is mostly negated by the fact that VC++ has over 20 instructions between the switch (opcode) and the first case line. Seems it isn't too good at accessing its own jump table

Not much difference in the two so far. Kind of shocking that my emulator, in its infancy stage, runs over 30 times slower than the hardware it emulates (I have a 1000 MHz t-bird)

[edited by - foofightr on June 1, 2002 12:50:54 PM]
How about std::map?

I assume you''re getting you OPCODES from some sort of commands typed into the console. A std::map has a hash table built into it and does lookups very fast.
No, I meant console emulator as in gaming console emulator. You know, video game consoles like Nintendo. You get the opcodes from the ROM file.

Magmai, the source file for the CPU code is 76kb, the majority which is the opcodes, so that would be quite a bit bigger than 4k even when optimized for space

This topic is closed to new replies.

Advertisement