0

Consider the following example:

#include <iostream>
#include <type_traits>

inline void f()
{
    std :: cout << "Welcome to f!" << std :: endl;
}

inline void g()
{
    std :: cout << "Welcome to g!" << std :: endl;
}

inline void h()
{
    std :: cout << "Welcome to h!" << std :: endl;
}

typedef void (*function)();
const function table[] = {f, g, h};

int main()
{
    int idx;
    std :: cin >> idx;
    table[idx]();
}

Here I have three functions, f, g and h. I put their pointers into a const array. Then, at runtime, I ask my user to provide a number and the corresponding function in the table is called.

I guess this is not going to get inlined, am I correct? I tried to inspect the assembly code but I really suck at reading assembly.

So I wonder, do I have better options to get the call to either f, g and h inlined? I mean, if I were to just make a switch this would work flawlessly.

Note: this is just an example, in a real-world scenario I will have a set of functions, known at compile time but determined by a kinda lengthy procedure. So I am not able to just, e.g., write the code in a switch statement.

Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
  • 3
    The standard doesn't say much about optimizations. Most optimizations rely on the [as-if rule](http://en.cppreference.com/w/cpp/language/as_if) and are not standarized. Not only does it greatly depend on which compiler you are using and the flags you use, but small changes to the code may change when and where optimizations are applied. So a small example may not be representative of a larger project. The best bet would be to try it for your use case and check it yourself. If your case is relatively simple, you can try https://godbolt.org/ – François Andrieux Jun 29 '17 at 13:40
  • Why would you need that? – iehrlich Jun 29 '17 at 13:41
  • If the function gets called only after a "kinda lengthy procedure", why worry about inlining at all? – Lanting Jun 29 '17 at 13:41
  • "I guess this is not going to get inlined", probably not. remember inline is just an hint... – Stargateur Jun 29 '17 at 13:42
  • 2
    `table[idx]();` if `idx` is given at runtime, how could the compiler determine if there's a `inline` function selected or not? I'm also not sure if a `switch` selection might help to optimize, since the table still can contain functions that aren't hinted to be inlined. – πάντα ῥεῖ Jun 29 '17 at 13:43
  • @Lanting I didn't explain myself, sorry: there is a lengthy compile-time procedure (e.g. complicated recursions of templates) that in the end determine a set of functions in that form. The lengthy procedure is completely done at compile time. – Matteo Monti Jun 29 '17 at 13:45
  • @πάνταῥεῖ that does make a lot of sense. What I mean when I say that a `switch` would be more optimized is: if I copied the bodies of `f`, `g` and `h` in a `switch` statement, this would be more efficient than fetching from a table in that form. Am I correct? – Matteo Monti Jun 29 '17 at 13:50
  • @MatteoMonti _"Am I correct?"_ Yes you are. I had something else in mind (using a `switch` + the table). – πάντα ῥεῖ Jun 29 '17 at 13:52
  • @πάνταῥεῖ see, that's what bugs me: you cannot make some kind of `template switch`, where the `case`s of the switch are determined by a template. The best approximation you can get with templates and recursion is a chain if `if`s. What I was trying to do here is the equivalent of a jumptable, which makes a `switch` faster than a chain of `if`s.. – Matteo Monti Jun 29 '17 at 13:55
  • @πάνταῥεῖ "_Am I correct?_" (in thinking a switch being more efficient). Unless your functions involve horrendous parameter passing, or are on an extremely critical code-path and are exceedingly lightweight, then I would have thought the (very small) difference in overhead between a function call and jumping into and out of the correct bit of the switch statement would be insignificant compared executing the selected code. – TripeHound Jun 29 '17 at 14:11
  • 1
    @TripeHound Well, that's true for all these kind of _micro optimization_ attempts. – πάντα ῥεῖ Jun 29 '17 at 14:13

1 Answers1

0

Provided you make use of proper const expressions, it is guaranteed that the code will be inlined at compile time. Have a look at the conditionals for that in this thread: When does a constexpr function get evaluated at compile time?

Lanting
  • 3,060
  • 12
  • 28