Functors if known at compile time will usually be inlines, but if they are too big to be inlined then a regular (not indirect) function call will be made so like "call foo" instead of like "call [EDX]" but when you use a function pointer instead of a template function, this is what happens:
if all of the following conditions are met then we will get a direct call (like we do with templates) 1) function body of the function that takes a function pointer as a parameter is avaliable in current compilation unit (possible) 2) the function that takes a function pointer as a parameter is small enough to inline (possible, but for what we are talking about not the case probably) 3) the compiler has to be smart enough to know how to do this type of substitution (many compilers don't) with templates, the template code must be avaliable in a header (like condition #1 above) and the function is generated from that function and the functor will be compiled as a direct call. There are obviously 2 downsides. First, you have to use templates and have the template function body avaliable in the header (but this is a standard deal with templates). Second, if there are multiple possibilities for what the functor or callback could be (not the case in what we are talking about) this could lead to code bloat. As far as indirect calls being inefficient. I know it seems counterintuitive since they are so "low level" and I use them in certain instances (and I love them), but the reality is that indirect function calls are one of the worst things for performance because the decoding of instructions is done considerably ahead of time and the processor needs to predict branches in code to keep the pipeline of decoded instructions full. If it comes to a branch that is 2 way, it has a 50% chance and with recent processors this has been optimized so that if it guesses wrong it isn't the end of the world, but with an indirect call, it has no good way to guess the location (the most likely is supposed to be placed as an unconditional jump after the indirect call on intel platforms) and it can't spread its resources across all possible locations it could jump to because there are millions of possibilities. If a function is getting called many times and it is a small function, most of the resources are devoted to this overhead. I know they have made some strides with the core 2 duo, but this is a really hard to deal with problem. And all this is irrelevent if this aspect isn't the bottleneck. Anyway, this is kind of off topic... Point is, templates and functors are going to be faster and they certainly are cleaner so I prefer those. That is why the STL uses them. - Nick On 2/16/07, Petri Pitkanen <[EMAIL PROTECTED]> wrote:
2007/2/16, Nick Apperson <[EMAIL PROTECTED]>: > trouble. Also, the alternative is usually function pointers which have > atleast 50 times the overhead of a function object. Correct me if I'm > wrong. > > - Nick > function objects really cannot be 50 times more efficient as function pointer are rather efficient with very little overhead. Besides unless unless function object is small enough to be inlined it will be compiled as function pointer in many cases so there cannot be any efficiency difference - well unless function object get inlined that is. Petri _______________________________________________ computer-go mailing list computer-go@computer-go.org http://www.computer-go.org/mailman/listinfo/computer-go/
_______________________________________________ computer-go mailing list computer-go@computer-go.org http://www.computer-go.org/mailman/listinfo/computer-go/