Can I selectively (force) inline a function?

后端 未结 6 533
执念已碎
执念已碎 2020-12-28 13:46

In the book Clean Code (and a couple of others I have come across and read) it is suggested to keep the functions small and break them up if they become large. It also sugge

相关标签:
6条回答
  • 2020-12-28 14:11

    No, inline is a recommendation to the compiler ; it does not force it to do anything. Also, if you're working with MSVC++, note that __forceinline is a misnomer as well ; it's just a stronger recommendation than inline.

    0 讨论(0)
  • 2020-12-28 14:20

    There is nothing that prevents you to put inline in a static function in a .cpp file.

    Some compilers have the option to force an inline function, see e.g. the GCC attribute((always_inline)) and a ton of options to fine tune the inlining optimizations (see -minline-* parameters).

    My recommendation is to use inline or even better static inline wherever you see fit, and let the compiler decide. They usually do it pretty well.

    0 讨论(0)
  • 2020-12-28 14:22

    You cannot force the inline. Also, function calls are pretty cheap on modern CPUs, compared to the cost of the work done. If your functions are large enough to need to be broken down, the additional time taken to do the call will be essentially nothing.

    Failing that, you could ... try ... to use a macro.

    0 讨论(0)
  • 2020-12-28 14:22

    This is as much about good old fashioned straight C as it is about C++. I was pondering this the other day, because in an embedded world, where both speed and space need to be carefully managed, this can really matter (as opposed to the all too oft "don't worry about it, your compiler is smart and memory is cheap prevalent in desktop/server development).

    A possible solution that I have yet to vet is to basically use two names for the different variants, something like

    inline int _max(int a, int b) {
        return a > b ? a : b;
    }
    

    and then

    int max(int a, int b) {
        return _max(a, b);
    }
    

    This would give one the ability to selectively call either _max() or max() and yet still having the algorithm defined once-and-only-once.

    0 讨论(0)
  • 2020-12-28 14:32

    Inlining – For example, if there exists a function A that frequently calls function B, and function B is relatively small, then profile-guided optimizations will inline function B in function A.

    VS Profile-Guided Optimizations

    You can use the automated Profile Guided Optimization for Visual C++ plug-in in the Performance and Diagnostics Hub to simplify and streamline the optimization process within Visual Studio, or you can perform the optimization steps manually in Visual Studio or on the command line. We recommend the plug-in because it is easier to use. For information on how to get the plug-in and use it to optimize your app, see Profile Guided Optimization Plug-In.

    0 讨论(0)
  • 2020-12-28 14:37

    Compilers are actually really really good at generating optimized code.

    I would suggest just organizing your code into logical groupings (using additional functions if that enhanced readability), marking them inline if appropriate, and letting the compiler decide what code to optimally generate.

    0 讨论(0)
提交回复
热议问题