1

When you reach for a smart pointer, std::unique_ptr should generally be the one closest at hand. It’s reasonable to assume that, by default, std::unique_ptrs are the same size as raw pointers, and for most operations (including dereferencing), they execute exactly the same instructions. This means you can use them even in situations where memory and cycles are tight. If a raw pointer is small enough and fast enough for you, a std::unique_ptr almost certainly is, too.

-Scott Meyers (Effective Modern C++)

How the bold part can be true, it takes two phases to find the raw ptr in smart ptr and then call the method. How they can be equally fast?

Narek
  • 38,779
  • 79
  • 233
  • 389
  • 1
    What is "find the raw ptr in smart ptr" ? The actual pointer is (usually) just a member of a smart pointer, there is no need to "find" anything. – lisyarus Sep 07 '15 at 15:19
  • 4
    Optimizations. It's the wonderful part about C++ that everyone can enjoy! – ThePhD Sep 07 '15 at 15:20
  • 4
    The indirection is most likely optimized out because all the code is available to the optimizer. – juanchopanza Sep 07 '15 at 15:20
  • 1
    It's Scott M**e**yers by the way. – Shoe Sep 07 '15 at 15:22
  • 5
    @ʎǝɹɟɟɟǝſ don't you know Mayers? Leading export on preformance – sehe Sep 07 '15 at 15:22
  • 1
    Possible duplicate of [How much is the overhead of smart pointers compared to normal pointers in C++?](http://stackoverflow.com/q/22295665/1938163) – Marco A. Sep 07 '15 at 15:27
  • @ʎǝɹɟɟɟǝſ Scott has given some very valuable input for us working in the embedded field. Less valuable for the krethi and plethi working on a PC. There they shouldn't think about any premature optimizations in 1st place. – πάντα ῥεῖ Sep 07 '15 at 15:45

1 Answers1

3

There likely is no "raw ptr in smart ptr". The smart pointer will just be the raw pointer. There is nothing you need that indirection would give you.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • when you call `smartPtr->doF()` you get `smartPtr->rawPtr->doF()` right? – Narek Sep 07 '15 at 15:54
  • @Narek No. You just get `rawPtr->doF()`. What does the indirection do that can't be eliminated? It's no different from `int a = 3; return a;`. The compiler will just generate the same code as it would if you did `return 3;`. – David Schwartz Sep 07 '15 at 16:08
  • You mean compiler easily optimizes away? – Narek Sep 07 '15 at 16:09
  • @Narek Yes. The compiler doesn't have to put code to look up something it can determine at compile time. The compiler knows at compile time where the raw pointer is, so why should it look at the smart pointer to get it? And then once it never needs the smart pointer for anything, it just removes it. – David Schwartz Sep 07 '15 at 16:10
  • In fact it is "smartPtr.rawPtr->doF()" and this means the offset (which is always 0) can be calculated and used. There is not even optimization involved as this is the normal function invocation and you get it with -O0. – Lothar Jan 27 '21 at 18:40