Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 917
cliffski Avatar asked Dec 16 '22 12:12

cliffski


2 Answers

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

like image 67
sharptooth Avatar answered Dec 19 '22 01:12

sharptooth


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.

like image 26
RC. Avatar answered Dec 19 '22 01:12

RC.