AI Applications in C++: How costly are virtual functions? What are the possible optimizations?

前端 未结 15 1338
慢半拍i
慢半拍i 2020-12-23 12:43

In an AI application I am writing in C++,

  1. there is not much numerical computation
  2. there are lot of structures for which run-time polymorphism is ne
15条回答
  •  长情又很酷
    2020-12-23 13:27

    Virtual calls do not present much greater overhead over normal functions. Although, the greatest loss is that a virtual function when called polymorphically cannot be inlined. And inlining will in a lot of situations represent some real gain in performance.

    Something You can do to prevent wastage of that facility in some situations is to declare the function inline virtual.

    Class A {
       inline virtual int foo() {...}
    };
    

    And when you are at a point of code you are SURE about the type of the object being called, you may make an inline call that will avoid the polymorphic system and enable inlining by the compiler.

    class B : public A {
         inline virtual int foo() 
         {
             //...do something different
         }
    
         void bar()
         {
          //logic...
          B::foo();
          // more  logic
         }
    };
    

    In this example, the call to foo() will be made non-polymorphic and bound to B implementation of foo(). But do it only when you know for sure what the instance type is, because the automatic polymorphism feature will be gone, and this is not very obvious for later code readers.

提交回复
热议问题