C++ _inline 在单例中被忽略并显示在我的分析器中。怎么会?
我的项目中有很多类由单例访问,如下所示:
_inline GUI_BS_Map* GUI_GetBS_Map()
{
static GUI_BS_Map obj;
return &obj;
};
据我了解,此代码应该内联。我将 Visual Studio (2005) 选项设置为内联任何合适的内容,并且我的探查器 (AQTime) 绝对没有设置为覆盖 _inlines。 然而,当我分析代码时,我发现对每个单例函数都有数千次调用。我可能会错过什么? (我正在分析调试版本(以获取分析器的符号),但所有速度优化均已打开。) 任何建议非常感谢!
I have a lot of classes in my project accessed by a singleton like so:
_inline GUI_BS_Map* GUI_GetBS_Map()
{
static GUI_BS_Map obj;
return &obj;
};
As I understand it, this code should be inlined. I have the Visual Studio (2005) options set to inline anything suitable, and my profiler (AQTime) is definitely not set to override the _inlines.
However, when I profile the code, there they are, thousands of calls to each of my singleton functions. What could I be missing?
(I'm profiling a debug build (to get symbols for the profiler) but with all of the speed optimisations turned on.)
Any suggestions much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编译器可以随意忽略
inline
和_inline
。在 Visual C++ 中,您可以尝试__forceinline
使编译器内联函数,除非有严重的理由不这样做(此类原因在链接的 MSDN 文章中列出)。The compiler is free to ignore
inline
and_inline
. In Visual C++ you can try__forceinline
that makes the compiler inline functions unless there're serious reasons not to do so (such reasons are listed in the linked MSDN article).内联只是对编译器的建议。它可能会忽略您的建议,甚至忽略您尚未标记为内联的内联函数。
我建议尝试将本地静态移到函数之外,重新编译并再次调试以检查是否看到行为发生变化。似乎尝试用本地静态内联此函数将是一个问题。
Inline is only a suggestion to the compiler. It may ignore your suggestion or even inline functions that you haven't marked to inline.
I would suggest trying to move the local static outside of your function, recompile, and debug again to check to see if you see a change in behavior. It seems that trying to inline this function with a local static would be an issue.
inline
是一种语义含义——你不能强迫编译器实际内联任何东西,它是一个实现细节,它可以随时嘲笑你并拒绝。inline
is a semantic meaning- you can't force the compiler to actually inline anything, it's an implementation detail and it can laugh at you and refuse any time it likes.如前所述 - 编译器可以自由地忽略内联。
在调试中构建以帮助调试时,更有可能忽略任何内联调用(因此内联函数中的断点会被正确捕获等)。
不过,我建议不要分析调试版本(如果可以避免的话),因为 VS 编译器在调试和发布之间的工作方式非常不同,并且您可能会得到错误的结果......
As said - the compiler is free to ignore inline.
It is also much more likely to ignore any inline calls when building in Debug to aid in debugging (so breakpoints in inlined functions get snagged correctly etc.).
I'd advise against profiling a debug version though (if you can avoid it), as the VS compiler works very differently between Debug and Release, and you may get erroneous results.....
首先,C++ 有
inline
关键字,但没有_inline
。_inline
是宏吗?特定于编译器的扩展?与您的分析器有关的东西吗?其次,C++ 编译器通常会内联任何它喜欢的内容,而
inline
关键字充其量只是暗示您希望看到此函数内联。inline
关键字今天的主要目的并不是启用内联优化(无论您是否告诉编译器,编译器都会非常积极地应用它) ),而是抑制单定义规则(ODR),以便可以在标头中完全定义函数,而不会冒链接器出现多个定义错误的风险。First, C++ has a
inline
keyword, but not_inline
. Is_inline
a macro? A compiler-specific extension? Something related to your profiler?Second, the C++ compiler generally inlines whatever it likes, and the
inline
keyword is, at best, a hint that you'd like to see this function inlined.The main purpose of the
inline
keyword today is not so much to enable the inlining optimization (which the compiler applies pretty aggressively whether or not you tell it to), but instead to suppress the One-Definition-Rule (ODR), so that a function can be fully defined in a header without risking multiple definitions errors from the linker.