ANSI C: If a function pointer points to executable code does that mean less execution overhead than simply invoking the function? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-06 08:28:48

In general case, when function pointer is stored in data segment's memory, invoking a function through such a pointer will involve more overhead, not less overhead.

The real-life performance might vary greatly, depending on whether the pointer is in the CPU cache, already loaded into CPU register, properly predicted by branch prediction mechanism and other factors.

A call through a function pointer pretty much necessarily has more, not less overhead than a simple function call. Why? Simply because an indirect function call using a function pointer makes it harder to do optimizations such as function inlining, because the compiler cannot deduce beforehand which function will be called.

Given your example code, a smart compiler can deduce that

void foo(void) {
    printf("hello\n");
}

int testFcn(void) {
    foo(); 
    return 0;
}

foo can be inlined into testFcn - as if it was written

int testFcn(void) {
    printf("hello\n");
    return 0;
}

yet the behaviour would be the same. However, such inlining cannot generally occur if a function pointer is used, because it can be changed before and between the invocations.

Real function calls always involve function pointers internally so the ptr-example should be at best equivalent.

Practically, calls via pointers are often worse because they may prevent the compiler from inlining (if the pointer isn't const or the compiler isn't very smart) and they may introduce additional layers of indirection if the pointer is global and not static/DSO-hidden (will have to go through the GOT if you're compiling PIC). If the pointer isn't in cache, fetching it will be expensive too.

What is the basis for this question? What makes you think that using a function pointer could have less overhead than invoking the function directly? Why would you expect that doing something indirectly would be faster than doing something directly?

Suppose that using a function pointer were somehow faster than invoking the function directly. If so, then it would be a trivial optimization for compilers to automatically convert all functions to function pointers! With such an optimization, you wouldn't be able to see any difference, so invoking via function pointers could never be observably faster.

Function pointer is NO less overhead than normal simply function call.

It allows you to use variable as function pointer, for example as parameter to function for callback, or any other usage.

void do_something(int (*get_value_callback)(void)) {
    int a;

    a = get_value_callback();
    printf("%d\n", a);
}

And then you can use it as this:

int func_ptr_1(void) { return 1; }
int func_ptr_2(void) { return 2; }

do_something(func_ptr_2); //Prints 1
do_something(func_ptr_2); //Prints 2

This is a example, how you can use function pointers acting like prototypes but there is no less overhead than in normal call. You can expect more overhead, not less. Real performance depends on many things and varies between CPUs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!