6

I wrote the following code:

class MyObjectHolder {
public:
    std::vector<int> getMyObject() const {
        return myObject;
    }

private:
    std::vector<int> myObject;
};

At some point of my program I attempt to use the getMyObject method and use only const methods on the retrieved object:

const std::vector<int> myObject = myObjectHolder.getMyObject();
myObject.size();
int a = myObject.front();
  • Now, is it possible that the compiler will optimize this code so that no copies of the std::vector<int> are done?

    Is it somehow possible that the compiler determines that I'm only using the const methods on the retrieved object (and let's assume there is no mutable nonsense happening behind it) and it would not make any copies of the objects and perform these const operations on the private member of the MyObjectHolder instead?

  • If yes, would it be possible if I didn't explicitly declare the const std::vector<int> myObject as const?

  • If no, what are the reasons not to do this? In which cases this optimization would be to hard to implement / deduce that it's possible and correct here / etc... ?

2
  • 2
    RVO involves temporary objects. Unless I am missing something, there are no temporary objects here so no RVO.
    – hmjd
    May 30, 2012 at 14:31
  • @hmjd Right. I should not be talking about the RVO here, just about the possible optimization with using the private variable directly. Thank you. May 30, 2012 at 14:34

3 Answers 3

6

Now, is it possible that the compiler will optimize this code so that no copies of the std::vector<int> are done?

No, the compiler doesn't know what callers will do with that object unless you are making use of global optimization over all code that uses the object (the compiler can't generally make assumptions about its use; moreover if object is exported from a dll it can't make any assumption at all).

If yes, would it be possible if I didn't explicitly declare the const std::vector myObject as const?

No, anyway the conversion from non-const to const could be implicit.

If no, what are the reasons not to do this? In which cases this optimization would be to hard to implement / deduce that it's possible and correct here / etc... ?

It's an optmiziation that should be done inside getMyObject() but the compiler can't be sure that callers won't cast away the const. Actually this is a very old debate about the use of const, usually I think it's more clear to always think about const as something for programmers and not for compilers.

10
  • 1
    "No, the compiler doesn't know what callers will do with that object." -> Is not generally correct. Proposed change: "Unless you are making use of global optimization over all codes that the object, the compiler can't generally make assumptions" or similar. May 30, 2012 at 14:46
  • @phresnel you're right (edited), moreover if the object is exported it can't make any assumption (and I'm not sure it can even follow casts inside the same unit). May 30, 2012 at 14:50
  • 1
    Even more so, the compiler is allowed to spit out multiple versions of the same function, which would mean an even more generalized edit :P May 30, 2012 at 15:45
  • 1
    On a side note: I think it would be great if OSs would provide a syscall to copy-on-write duplicate data. I.e. tell the OS "take this part of the address space here in my process, and mirror it there, but make an in-situ modified copy, if I write to it." The infrastructure for this is already present in most OSs, all it take was a new syscall to expose this functionality. Hmm… .oO(cd /usr/src/linux/ ; vim … ;)
    – datenwolf
    Jun 27, 2012 at 9:39
  • 1
    @Adriano: I wouldn't see this as something for the compiler to take care of. The OPs request of passing the vector without making a copy could be implemented in the library as well (keeping a reference to the original vector and only when calling a non-const function making a copy). Now in such a library based implementation you'd have to go through a full copy, which is not very efficient if only a few elements are changed. A CoW copy could be created by the library through a syscall like "memcow" into a new vector object. Modification there would make it a real copy, otherwise just reference
    – datenwolf
    Jun 27, 2012 at 10:18
3

I would suggest to use

const std::vector<int>& getMyObject() const {
    return myObject;
}

It would return the constant reference of myObject without copy that.

And use the result with

const std::vector<int>& myObject = myObjectHolder.getMyObject();
0

It is possible Copy Elision and Return Value Optimization will kick in. If you use C++ compiler with C++11 support, then you may get it optimised by move semantics.

I'd recommend to read the excellent article Want Speed? Pass by Value by Dave Abrahams with discussion in the comments below it.

However, for details you should refer documentation of your C++ compiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.