So from my POV, the problem is that we promised RVO/NRVO, but never quite followed on that promise.
Isn't this a misoptimization for the current semantics of ExprRepeat
, as it evaluates $expr
multiple times? The correct version would be this:
if $n > 0 {
place[0] = $expr;
place[1..] = [place[0]; $n-1];
}
Maybe we should also have a variant of ExprRepeat
that works with non-Copy
but constexpr $expr
.
For years there was supposed to be a guaranteed MIR optimization that does exactly this, except also in a few more cases (e.g., NRVO). It just never happened to be implemented because nobody found the time for it.