Passing a pointer can be much slower due to how a modern processor works. But it has nothing to do with run-time memory.
Instead, prediction and cache affect speed.
Prediction is easy when the pointer has not been changed or when it has been changed in predictable ways (for example, increasing or decreasing by four in a cycle). This allows the CPU to function substantially ahead of the actual code execution, find out what the pointer value will be, and load this address into the cache. Prediction becomes impossible when a pointer value is created by a complex expression, such as a hash function.
The cache enters the game because the pointer may point to memory that is not in the cache and needs to be removed. This is minimized if the prediction works, but if the prediction is impossible, then in the worst case you can have a double impact: the pointer is not in the cache, and the pointer is also not in the cache. In the worst case, the processor will stand twice.
If a pointer is used to point to a function, the processor branch predictor comes into play. In C ++ virtual tables, the values โโof the functions are constant, and the predictor is easy. The CPU will have ready-made code to run in the pipeline when execution passes through an indirect transition. But if this is an unpredictable pointer to a function, the performance impact can be severe, because it will be necessary to flush the pipeline, which with each jump will spend 20-40 cycles of the processor.
Zan lynx
source share