Visual Studio 2008 调试托管到非托管转换的速度非常慢(尽管只有一台机器)
我有一些 C# 代码,它通过 NUnit 测试中的 P/Invoke 函数调用将委托作为回调传递给非托管方法。
该代码运行良好,并通过了发布和调试模式下的所有测试。无论是否在调试器下运行,它在一台机器上运行速度都很快。
但在另一台电脑上为即将开始的新开发人员设置几乎相同的开发环境后,它在发布和调试配置中运行得很快。但是当连接调试器时速度非常慢。
请注意,我发现在项目上启用“调试非托管代码”时会出现某种缓慢的情况。我已经禁用了它,重新编译,有或没有都没有关系。我两种方法都尝试了好几次。
此外,没有设置任何断点或监视变量。
顺便说一句,这个单元测试实际上在循环中调用非托管方法 100 万次,该方法在递增计数器后返回。这是非常简单的代码,仅测试跨 AppDomain 进行非托管调用的性能。
请记住,这是来自 git commit 的相同代码,仅在其中一台机器上的调试器下运行缓慢。它们之间没有代码修改不同,因此看来最终这不是“代码”问题,而是 Visual Studio 中与非托管与托管调试相关的设置,我会疯狂猜测。
预先感谢您的任何想法。如果您确实认为查看代码会有帮助。我也会发布单元 C# 测试和 cpp 文件。
编辑:我缩小了范围,调试器中的这种缓慢情况仅发生在调用不同 AppDomain 的非托管代码中。因此,在这些性能测试中,存在主 AppDomain 和另一个辅助 AppDomain。托管到非托管调用经过测试以从主域回调到其自身。那些很快!但是那些从非托管回调到其他 AppDomain 的速度非常非常慢。这意味着从每秒 2000 万条下降到每秒只有 4、5 千条。
请注意,调用测试的方法是 void callback()——因此不是参数或返回值。换句话说,没有什么可以整理的。
编辑:我用不同的设置进行了调整,现在我的开发盒也很慢。我确信“仅我的代码”设置对于更快的机器来说是关闭的,因此可以让它尝试一下。但现在,即使再次禁用它,它仍然很慢。所以不确定这是否是原因。
I have some C# code which passed a delegate as a callback to an unmanaged method via a P/Invoke function call in a NUnit test.
The code works great and passes all tests in both Relase and Debug modes. And it runs fast on one machine whether running under the Debugger or not.
But after setting up a nearly identical development environment on another PC for a new developer starting soon, it runs fast in Release and Debug configuration. But horribly slow when the Debugger is attached.
Note that I have seen a type of slowness with "debug unmanaged code" enabled on the Project. I have disabled that, recompiled and it doesn't matter with or w/o. I tried it both ways several times.
Also, there aren't any break points or watch variables set.
As an aside, this unit test actually calls the unmanaged method in a loop 1 million times which returns after incrementing a counter. It's extremely simple code that was only testing the performance of making unmanaged calls across AppDomains.
Please remember that this is identical code from git commit that only runs slow when under the debugger on one of the machines. No code modifications are different between them so it seems conclusively this isn't a "code" issue but rather a setting in Visual Studio somewhere related to unmanaged vs. managed debugging, I will wildly guss.
Thanks in advance for any ideas. If you really think seeing the code will help. I'll post the unit C# test and the cpp file too.
Edit: I narrowed down that this slowness in the debugger only happens for the unmanaged code that calls into a different AppDomain. So in these performance tests there is the primary and another, secondary AppDomain. Managed to Unmanaged calls are tested to callback from the primary domain to itself. Those are fast! But those that callback across from unmanaged into the other AppDomain are very, very slow. This means from 20 million per second down to only 4 or 5 thousand per second.
Note that the method being called to test is void callback()--so not arguments or return value. In other words, there's nothing to marshall.
Edit: I was jiggerng with different settings and now my development box is SLOW too. I was sure it was the "Just My Code" setting that saw was off for the faster machine so enabled it to try that out. But now, even after disabling it again, it's still slow. So not sure if this is the cause or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查两台机器上的符号文件设置是否相同。加载本机代码的所有符号可能需要很长时间(工具 -> 选项 -> 调试 -> 符号)。
Check if symbol files settings are the same on both machines. Loading all symbols for native code may take very long time (Tools -> Options ->Debugging -> Symbols).