0

(Inspired by this question which is unfortunately subjective).

Which factors affect whether a compiler chooses to apply (N)VRO?

I know there are two broad categories of factors as the optimizer needs to answer two questions about the (N)VRO decision: (1) Is it even possible and (2) is it an optimization ? I expect each factor to apply to just one of those two questions.

Please, no unfounded speculation on the optimization part. It's OK to list a factor when you can explain exactly how it affects optimization, or a factor that's used by an existing compiler. But don't say sizeof(T) without explaining how that affects the decision to apply (N)VRO.

Community
  • 1
  • 1
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • The official term is *copy elision*. It's obviously an optimization since when it applies , nothing happens. It's when copy elision doesn't occur that runtime operations might happen. You can't do better than a no-op. It will be interesting to see if anyone can come up with an example where applying `-fno-elide-constructors` increases performance – M.M Mar 13 '15 at 10:55
  • I'm unsure about the "is it an optimization" part of the question. Constructing an object in-place surely is _always_ an optimization over constructing and then moving or copying it, no? Even if the operation is very cheap, it's still an optimization not to do it at all. – Damon Mar 13 '15 at 11:08
  • @Damon: Viewed in isolation, yes - but possibly that would affect another optimization forcing the compiler to pick. – MSalters Mar 13 '15 at 11:24
  • Why would it (or should it) have to? If the result of a function is, say, a loop invariant, then why should the fact that the compiler isn't doing another one or two extra copies prevent it from moving the invariant to another place outside the loop? There's sequence points around function calls, so by definition... no interference? – Damon Mar 13 '15 at 12:43
  • @Damon: Well, the function might not have stored the object in contiguous memory (allowed under as-if rule). – MSalters Mar 13 '15 at 13:31

0 Answers0