Do function pointers force an instruction pipeline to clear?

前端 未结 4 805
面向向阳花
面向向阳花 2020-12-09 02:29

Modern CPUs have extensive pipelining, that is, they are loading necessary instructions and data long before they actually execute the instruction.

Sometimes, the da

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 03:15

    There's not a great deal of difference between a function-pointer call and a "normal" call, other than an extra level of indirection. So potentially there's a greater latency involved; if the destination address is not already in cache or registers, then the CPU potentially has to wait while it's retrieved from main memory.

    So the answer is; yes, the pipeline can stall, but this is no different to normal function calls. And as usual, mechanisms such as branch prediction and out-of-order execution can help minimise the penalty.

提交回复
热议问题