开发者

C++ _inline is being ignored in a singleton and showing up in my profiler. How come?

开发者 https://www.devze.com 2023-03-15 02:52 出处:网络
I have a lot of classes in my project accessed by a singleton like so: _inline GUI_BS_Map* GUI_GetBS_Map()

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!


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 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.


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.....


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消