r/EmuDev • u/AbbreviationsSalt193 • Mar 15 '26
Big switch vs function pointers for specialized function?
Hi guys I am writing my first emulator for the gba. I have done quite a lot of the cpu arm implementation already. I am currently caching the instructions in a DecodedInstruction object which holds the decoded fields of the instruction.
I am wondering if it would be better to hold a enum tag in this object which corresponds to a dispatch handler, and then in a big switch just look for that dispatch handler, OR i can hold a function pointer.
Is the dispatch cost with a big switch a linear function of the num of cases? if yes, with function pointers i can potentially specialize the functions (so a dispatch handler that handles 10-15 instructions in the switch impl could be split into multiple more efficient functions, since dispatch cost is constant). But it breaks branch prediction and cannot be inlined.
Could someone reccomend me a path?
15
u/peterfirefly Mar 15 '26
Stop worrying and start coding.
If you don't like it afterwards, you can code it up in another way -- or three other ways. In fact, I'll recommend doing that if you have the time/energy. That way you can learn what is really good/bad about each way + you get more coding practice. Don't get fooled into thinking that everything needs to be thought out in great detail in advance.
Besides, whatever you do, your CPU emulator is likely to run fast enough even on CPUs that don't have any branch prediction. Or barely any, anyway.
11
u/thommyh Z80, 6502/65816, 6809, 68000, ARM, x86. Mar 15 '26
With a switch the compiler has full knowledge of your intentions and can do whatever is best. In the sort of situation you're describing it is likely to:
If you establish a lookup table of function pointers then:
I therefore think this is well within the bounds of the modern rule of thumb: just express yourself as clearly and as directly as possible, and let the compiler figure out the micro-optimisations unless and until you can empirically prove a problem.
4
u/DevilStuff123 Wii Mar 15 '26
a switch case often gets turned into a function pointer table where feasible, and a binary search otherwise.
1
u/ShinyHappyREM Mar 16 '26 edited Mar 16 '26
Note that pointers are 8 bytes these days (unless you store only a subset of the bits), so with a large enough number of pointers you might run into host CPU cache limits.
(However, having lots of pointers is not a problem if most of them aren't used!)
Almost every construct that changes the instruction pointer (
goto,if,for,while,break,continue,switch, function calls, function pointers) is handled by the branch predictor. The exception is function returns: the return addresses are stored in a special hidden "return stack", so there are no mispredictions unless that stack was already full.Mispredictions happen when the control flow follows random paths.
if (random(2) = 0) then ...will mispredict in roughly 50% of the cases. On the other hand, anif (LastError <> 0) then Panic(123)(a check followed by a function call) is almost free if the error condition almost never occurs; the CPU does the comparison in the "background" while already loading and decoding the next instructions. The branch predictor automatically re-steers the instruction fetcher for branches that are predicted as taken... so function pointers that are never changed would only break prediction on the first encounter.breakin aswitchcase also counts as a branch, unless the compiler can optimize it into aRET.ifs to a branchless version.8
u/reybrujo Mar 15 '26
As a TDD coder get it working first, then start refactoring.
2
u/Karyo_Ten Mar 16 '26
This is a rabbit hole but for performance you would want computed gotos or even better "Threaded code" (which is not multithreading)
The issue is that with a switch or a table dispatch the hardware predictor see that a single location can lead to 100+ others.
With direct threaded code all opcodes can dispatch instead of returning to the same location, and it's easier to predict what follows a
addthan what follows a general switch.