CRTP是在这个关于动态多态性的问题中提出的。 然而,据称这种模式仅对静态多态性有用。 我正在查看的设计似乎受到虚拟函数调用的速度阻碍,如 在这里暗示。 甚至 2.5 倍的加速都很棒。
所讨论的类很简单,可以完全内联编码,但是直到运行时才知道将使用哪些类。 此外,他们可能会以任何顺序被锁起来,使表现雪上加霜。
欢迎提出任何建议(包括在这种情况下如何使用 CRTP)。
编辑:谷歌搜索显示了函数模板的提及。 这些看起来很有希望。
The CRTP is suggested in this question about dynamic polymorphism. However, this pattern is allegedly only useful for static polymorphism. The design I am looking at seems to be hampered speedwise by virtual function calls, as hinted at here. A speedup of even 2.5x would be fantastic.
The classes in question are simple and can be coded completely inline, however it is not known until runtime which classes will be used. Furthermore, they may be chained, in any order, heaping performance insult onto injury.
Any suggestions (including how the CRTP can be used in this case) welcome.
Edit: Googling turns up a mention of function templates. These look promising.
发布评论
评论(3)
多态性字面意思是多种(多)形式(变形)。 在静态类型语言(例如 C++)中,存在三种类型的多态性。
如果直到运行时才知道将使用哪些类,则必须使用子类型多态性,这将涉及虚函数调用。
与静态绑定调用相比,虚拟方法调用的性能开销非常小。 我强烈建议您查看这个SO问题的答案。
Polymorphism literally means multiple (poly) forms (morphs). In statically typed languages (such as C++) there are three types of polymorphism.
If it is not known until runtime which classes will be used, you must use Subtype Polymorphism which will involve virtual function calls.
Virtual method calls have a very small performance overhead over statically bound calls. I'd urge you to look at the answers to this SO question.
我同意 m-sharp 的观点,即你不会避免运行时多态性。
如果你看重优化而不是优雅,请尝试
用类似
不漂亮的东西替换 say,当然不是 OOP(更多的是回归到你在旧的“C”中可能做的事情),但如果虚拟方法足够简单,你应该得到一个函数没有调用(取决于足够好的编译器和优化选项)。 使用dynamic_cast或typeid的变体可能稍微更优雅/安全,但要注意这些功能有自己的开销,无论如何可能与虚拟调用相当。
您最有可能看到上述改进的地方是,如果某些类方法是无操作的,并且它使您免于调用它们,或者如果函数包含常见的循环不变代码并且优化器设法将其从环形。
I agree with m-sharp that you're not going to avoid runtime polymorphism.
If you value optimisation over elegance, try replacing say
with something like
it's not pretty, certainly not OOP (more a reversion to what you might do in good old 'C') but if the virtual methods are trivial enough you should get a function with no calls (subject to good enough compiler & optimisation options). A variant using dynamic_cast or typeid might be slightly more elegant/safe but beware that those features have their own overhead which is probably comparable to a virtual call anyway.
Where you'll most likely see an improvement from the above is if some classes methods are no-ops, and it saved you from calling them, or if the functions contain common loop-invariant code and the optimiser manages to hoist it out of the loop.
您可以走 Ole C 路线并使用工会。 尽管这也可能很混乱。
You can go the Ole C Route and use unions. Although that too can be messy.