Jump to content

  • Log In with Google      Sign In   
  • Create Account


jump table


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 07 December 2012 - 03:27 PM

I have an array of function pointers
[source lang="cpp"]void a();void b();void c();void d();jump={a, b, c, d};[/source]
I'm indirectly calling those functions in a loop
[source lang="cpp"]while(some_condition){ int i=...//i is random jump[i]();}[/source]
If i is consistent, not just random values, does the loop run faster?
Does the cpu predict which function to call like in branch prediction?
An invisible text.

Sponsor:

#2 smr   Members   -  Reputation: 1540

Like
2Likes
Like

Posted 07 December 2012 - 06:56 PM

I doubt it. If I were random, never. Theoretically this could be reduced by the optimizer if there were no random. The loop could be unrolled, from there it could eliminate looking the function pointers up because they are never changed, but I doubt it would go that far. Maybe if you made the array immutable. I'm no expert on the inner workings of code optimization though.

#3 _swx_   Members   -  Reputation: 874

Like
1Likes
Like

Posted 07 December 2012 - 07:03 PM

http://www.altdevblogaday.com/2011/08/22/optimizing-for-the-instruction-cache/

#4 Hodgman   Moderators   -  Reputation: 26986

Like
2Likes
Like

Posted 07 December 2012 - 07:06 PM

Each time you perform a jump, the code at the jump target has to be loaded into the L1 icache. If you're calling the same function over and over, there's a bigger chance it will still be present in the cache. On the other hand, if you're calling random functions, there's a larger chance of cache misses.

Regarding branch prediction, it depends on the CPU heavily.
Some CPUs have a history-based predictor, where it's first guess for any branch will be the target that this branch jumped to last time. In this case, as long as a/b/c/d aren't long enough (contain enough branches of their own) to completely flush that history table, then using the same 'i' repeatedly would help the predictor.
That said, many of these history-based schemes only store true/false values, not actual addresses, so they only work for conditional jumps, not unconditional jumps like yours.

If the distance between fetching the value at jump and jumping to that value is large enough, then some CPUs may be able to fully determine the branch target before branching, meaning there's no prediction to be done. If this is the case, you might be able to help by unrolling your loop somewhat:
int i1=...
int i2=...
int i3=...
int i4=...
jump[i1]();
jump[i2]();
jump[i3]();
jump[i4]();
However, the best overall solution is probably to use 4 loops:
while(...)
  a();
while(...)
  b();
while(...)
  c();
while(...)
  d();

Edited by Hodgman, 07 December 2012 - 07:13 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS