混合模式 C++CLI 性能注意事项 - 最佳实践
我有一个 C++/CLI 库,它调用许多本机 C++ 方法。我读过许多线程,指出不应混合托管和非托管代码。我找不到任何说明如何避免这些开关以及为什么它会导致性能问题的信息。有人可以分享最佳实践吗?
I have a C++/CLI library that calls many native C++ methods. I have read many threads stating that you should not mix managed and unmanaged code. I couldnt find any that says how to avoid those switches and why it will cause a performance issue. Can someone share best practices.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 C++/CLI 的唯一原因是它支持混合托管代码和本机代码。如果一切都是托管的,则使用 C# 或 VB,如果一切都是本机的,则使用 C 或 C++。或者您喜欢的任何语言。显然避免混合是荒谬的。
从托管到非托管有少量开销。 C++/CLI 编译器自动生成一些机器代码,将“cookie”推送到堆栈上,旨在防止垃圾收集器误入非托管堆栈帧并将该帧上的指针错误解释为托管对象引用。花费大约 7 纳秒,或多或少。
The only reason to use C++/CLI is for its support of mixing managed and native code. If everything is managed then use C# or VB, if everything is native then use C or C++. Or whatever language you prefer. Clearly avoiding mixing is nonsensical.
There is a small amount of overhead going from managed to unmanaged. The C++/CLI compiler auto-generates a bit of machine code that pushes a 'cookie' on the stack, designed to prevent the garbage collector from blundering into unmanaged stack frames and mis-interpreting pointers on that frame as managed object references. Costs about 7 nanoseconds, give or take.