let's say I have a function that accepts a callback argument (example given in rust and C)
void foo(void (*bar)(int)) {
// lots of computation
bar(3);
}
fn foo(bar: fn(u32)) {
// lots of computation
bar(3)
}
can I rely on the indirect call to bar
being correctly predicted by the CPU? Assume that at the callsite of foo
, the value of bar
is in fact highly dynamic and unpredictable - but because of // lots of computation
, my expectation is that the CPU has enough "advance warning" such that speculative/out-of-order execution can work across the function pointer boundary.
Is this the case? If not, is there anything I can do as the programmer to help the CPU out?