It is ideal to have every piece of code as optimized as possible, but the most effort should be spent on code which takes the most time. In this case of Galaga, this takes up a large portion of the emulation time. It is possible to use a multithreaded approach to simulate micros running in parallel, but since my cpu emulator code depends on static variables for speed, I chose to use a round-robin approach. Galaga requires a high level of synchronization between the micros for the sound to work, so this was an area that eats lots of cycles doing context switches. A technique which improved performance dramatically was busy-loop removal.
Busy loops are code which sits in a loop waiting for an event such as an interrupt.
These are wasted cycles which waste valuable time doing nothing. This applies to all function calls which take place within the cpu emulation. Galaga has a shared memory area between the three processors that required a handler routine. My point is that there can be lots of fat to trim in the handler routines as well. This is one area of the code which I have completely rewritten at least 5 times.