C++ use templates to avoid compiler from checking a boolean

后端 未结 5 671
忘了有多久
忘了有多久 2021-02-12 13:51

Let\'s say I have a function:

template 
inline void doSomething() {
    if(stuff) {
        cout << \"Hello\" << endl;
    }
    el         


        
相关标签:
5条回答
  • 2021-02-12 14:19

    This is inherently up to the compiler, so you'd have to check the compiler's documentation or the generated code. But in simple cases like this, you can easily implement the optimization yourself:

    template <bool stuff>
    inline void doSomething();
    
    template<>
    inline void doSomething<true>() {
        cout << "Hello" << endl;
    }
    
    template<>
    inline void doSomething<false>() {
        cout << "Goodbye" << endl;
    }
    

    But "optimization" isn't really the right word to use since this might actually degrade performance. It's only an optimization if it actually benefits your code performance.

    0 讨论(0)
  • 2021-02-12 14:24

    Compilers are really good at constant folding. That is, in this case it would surprise me if the check would stay until after optimization. A non-optimized build might still have the check. The easiest way to verify is to create assembler output and check.

    That said, it is worth noting that the compiler has to check both branches for correctness, even if it only ever uses one branch. This frequently shows up, e.g., when using slightly different algorithms for Random Access Iterators and other iterators. The condition would depend on a type-trait and one of the branches may fail to compile depending on operations tested for by the traits. The committee has discussed turning off this checking under the term static if although there is no consensus, yet, on how the features would look exactly (if it gets added).

    0 讨论(0)
  • 2021-02-12 14:24

    If I understand you correctly you want (in essence) end up with 'two' functions that are optimised for either a true or a false input so that they don't need check that flag?

    Aside from any trivial optimisation that may yield (I'm against premature otimisation - I believe in maintainability before measurement before optimisation), I would say why not refactor your function to actually be two functions? If they have common code then then that code could be refactored out too. However if the requirement is such that the refactoring is non optimal then I'd replace that with a #define refactoring.

    0 讨论(0)
  • 2021-02-12 14:25

    Indeed, it really createa two functions, but

    premature optimization is the root of all evil

    especially if your changing your code structure because of a simple if statement. I doubt that this will affect performance. Also the boolean must be static, that means you cant take a runtime evaluated var and pass it to the function. How should the linker know which function to call? In this case youll have to manually evaluate it and call the appropiate function on your own.

    0 讨论(0)
  • 2021-02-12 14:43

    Disclaimer: Noone can guarantee anything.

    That said, this an obvious and easy optimization for any compiler. It's quite safe to say that it will be optimized away, unless the optimizer is, well, practically useless.

    Since your "true" and "false" are constants, you are unambiguously creating an obvious dead branch in each class, and the compiler should optimize it away. Should is taken literally here - I would consider it a major, major problem if an "optimising" compiler did not do dead branch removal.

    In other words, if your compiler cannot optimize this, it is the use of that compiler that should be evaluated, not the code.

    So, I would say your gut feeling is correct: while yes, no "guarantees" as such can be made on each and every compiler, I would not use a compiler incapable of performing simplistic optimizations in any production environment, and of course not in any performance critical one. (In release builds of course).

    So, use it. Any modern optimizing compiler will optimize it away because it is a trivial optimization. If in doubt, check disassembly, and if it is not optimized, change the compiler to something more modern.

    In general, if you are writing any kind of performance-critical code, you must rely, at least to some extent, on compiler optimizations.

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