虚拟继承对性能的影响 [英] Performance impact of virtual inheritance

查看:93
本文介绍了虚拟继承对性能的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑在实时应用程序中使用虚拟继承。使用虚拟继承是否具有与调用虚拟函数类似的性能影响?有问题的对象只会在启动时创建,但是我担心层次结构中的所有函数是否都将通过vtable进行分派,或者是否只有虚拟基类中的那些函数才被分发。

I am considering using virtual inheritance in a real-time application. Does using virtual inheritance have a performance impact similar to that of calling a virtual function? The objects in question would only be created at start up but I'm concerned if all functions from the hierarchy would be dispatched via a vtable or if only those from the virtual base class would be.

推荐答案

通用实现将使用附加的间接访问方式来访问虚拟基类的 数据成员

Common implementations will make access to data members of virtual base classes use an additional indirection.

正如James在其评论中指出的那样,在多继承方案中调用基类的成员函数将需要调整 this 指针,并且如果该基类是虚拟的,则派生类对象中基类子对象的偏移量取决于派生类的动态类型,并且需要在运行时进行计算。

As James points out in his comments, calling a member function of a base class in a multiple inheritance scenario will need adjustment of the this pointer, and if that base class is virtual, then the offset of the base class sub-object in the derived class's object depends on the dynamic type of the derived class and will need to be calculated at runtime.

这是否会对实际应用程序产生任何明显的性能影响取决于许多因素:

Whether this has any visible performance impact on real-world applications depends on many things:


  • 虚拟基地是否 拥有数据成员 ?通常,它是需要从虚拟派生的抽象基类,而具有任何数据成员的抽象基无论如何通常都是代码的味道。

  • Do virtual bases have data members at all? Often, it's abstract base classes that need to be derived from virtually, and abstract bases that have any data members are often a code smell anyway.

假设具有数据成员的虚拟基础,是否可以通过关键路径 访问那些数据库?如果用户单击GUI中的某个按钮导致了数十个其他间接访问,则没人会注意到。

Assuming you have virtual bases with data members, are those accessed in a critical path? If a user clicking on some button in a GUI results in a few dozen additional indirections, nobody will notice.

是什么 是否可以避免使用虚拟基地?该设计不仅劣质,而且替代设计也可能会对性能产生影响。毕竟,它和TANSTAAFL必须达到相同的目标。然后,您将一个性能损失换成另一种,再加上劣质的设计。

What would be the alternative if virtual bases are avoided? Not only might the design be inferior, it is also likely that the alternative design has a performance impact, too. It has to achieve the same goal, after all, and TANSTAAFL. Then you traded one performance loss for another plus an inferior design.

附加说明:看看Stan Lippmann的 在C ++对象模型中 相当彻底地回答了这些问题。

Additional note: Have a look at Stan Lippmann's Inside the C++ Object Model, which answers such questions quite thoroughly.

这篇关于虚拟继承对性能的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆