4

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!

kikito
  • 51,734
  • 32
  • 149
  • 189
cliffski
  • 81
  • 4
  • Also: singleton -> no sympathy. – Puppy Jun 23 '11 at 13:07
  • If your singleton is really implemented like that and you don't mind creating the object even if you app never needs it you could replace it with a simple macro(#define). – Karoly Horvath Jun 23 '11 at 13:08
  • If `GUI_BS_Map` is a type whose constructor might possibly throw an exception, many compilers are reluctant to inline this as it will be difficult to recover from the exception. – Bo Persson Jun 23 '11 at 13:21
  • "Also: singleton -> no sympathy. – DeadMG" Maybe there could be an explanation there? rather than just a pithy one liner, as I have no idea what that is suggesting...? – cliffski Jun 23 '11 at 13:26
  • 2
    singletons produce code coupling and knowledge on the client side about the object being a singleton. Consider reversing the interface, that the singleton is passed as parameter where needed. If you some day decide that the singleton should be a non-singleton you can easily change it. Wikipedia also lists a few drawbacks: http://en.wikipedia.org/wiki/Singleton_pattern#Drawbacks Additionally, passing singleton as parameter to the function, you could mock the singleton object and unit test if it is properly used by the function which requires it. – ovanes Jun 23 '11 at 13:39

5 Answers5

9

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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Even if __forceinline works ensure that your object is not created twice and you really do not inline lot's of management stuff for lazy object instantiation as I explained below. It might be that you really produce much more code cache misses with the inlining. – ovanes Jun 23 '11 at 13:30
1

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.

RC.
  • 27,409
  • 9
  • 73
  • 93
1

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
1

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

NotJarvis
  • 1,267
  • 11
  • 17
0

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.

jalf
  • 243,077
  • 51
  • 345
  • 550