从 void* 和铸造转向带有 PVF 的 ABC(会影响速度吗?)
我刚刚继承了(咳咳)一个 QNX 实时项目,该项目使用 void*/downcasting/case 语句机制来处理消息传递。我更愿意切换到具有纯虚函数的抽象基类,但我想知道最初的解决方案是否是出于速度原因而这样做的?它看起来很像最初是用 C 编写的,后来在某个时候转移到了 C++,所以我猜测这可能是其背后的原因。
任何对此的想法都值得赞赏。我不想让代码变得漂亮、安全和整洁,然后在测试过程中由于性能原因而失败。
I've just inherited (ahem) a QNX realtime project which uses a void*/downcasting/case statement mechanism to handle messaging. I'd prefer to switch to an abstract base class with pure virtual functions instead but I'm wondering if the original solution was done like that for speed reasons? it looks a lot like it was written originally in C and got moved at some point to C++, so I'm guessing that could be the reason behind it.
Any thoughts on this are appreciated. I don't want to make the code nice, safe and neat and then have it fail for performance reasons during testing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑性能会成为一个问题。如果 switch/case 中有足够的不同值,您的编译器甚至可能不会将其优化为跳转表,从而设置虚拟分派可能比 switch 更快的可能性。
如果纯虚拟接口在设计方面有意义,我肯定会这样做(如果您真的担心的话,可以对其进行原型设计和分析)。
I doubt that performance will be a concern. If there are sufficient disparate values in the switch/case your compiler may not even optimize it into a jump table, setting up the possibility that the virtual dispatch could be faster than the switch.
If a pure virtual interface makes sense design-wise I would definitely go that way (prototype and profile it if you're really concerned).