I'm unsure how much of an impact the distance of the jump instruction has on its performance. Let's say it's called every 10 milliseconds. What would the difference be in performance, between say a jump 100 or 1000 bytes forward and say 7.5 million bytes back?
CodePudding user response:
The distance by itself has no effect. A jump consists of loading a new address into the program counter (IP/EIP/RIP on x86), or in case of a relative jump, adding the desired displacement to the program counter. Load and add are both constant-time operations whose speed does not depend on the value(s) involved.
There might be a slight effect if the distance means using a longer or shorter encoding. For instance, x86 in 32- or 64-bit mode has two encodings for relative jump: opcode 0xEB with an 8-bit displacement (total size 2 bytes, so-called "short jump"), and opcode 0xE9 with a 32-bit displacement (total size 5 bytes). Your 100-byte jump could use the shorter form and thus use 3 fewer bytes of code, which will tend to be slightly faster to fetch and leave more space for other code to fit in cache. The longer jumps would need the longer form.
The CPU then fetches its next instruction from the new address and continues execution. The whole point of random-access memory is that (ignoring caching) any part of it may be accessed in the same amount of time. Fetching bytes from an address 100 bytes away is no different than fetching from an address 100 megabytes away. It's not like a disk drive that must mechanically move a head to a new physical position, which may take longer if the distance is greater, nor a tape drive that has to traverse all the tape between the current position and the desired one. So there's no essential difference there either.
Of course, caching effects do come into play. Since modern CPUs do prefetching, an address that's a short positive distance away may have a better chance of already having been loaded into cache. On the other hand, with branch prediction and speculative execution, the CPU may have seen the jump instruction coming and started caching and fetching the instructions on the other side. It may make more of a difference which region of memory has been accessed more recently. (10 ms is not very recent - it's practically forever on a CPU timescale. Indeed, any instruction that you execute only once every 10 ms is, for practical purposes, so rare that there's not much need to even think about its performance.)