https://codereview.appspot.com/14419054/diff/13001/doc/go_spec.htmlFile doc/go_spec.html (right):
https://codereview.appspot.com/14419054/diff/13001/doc/go_spec.html#newcode5357doc/go_spec.html:5357: For instance, if the capacity of <code>s</code>
is not large enough to fit the additional
On 2013/10/10 01:45:59, kortschak wrote:
I don't think this yet addresses the concern raised in Issue 5180. It would if
s/For instance, if/For instance, iff/ though. I know that you have addresses
this in that issue, but like minux, I think the idiom depending on this
behaviour is very widely used and probably expected by most gophers.
The spec should not prescribe a specific implementation if not
absolutely necessary, hence the _may_. Of course any sensible
implementation _will_ (at the moment) reuse the underlying array if
there's enough capacity since that is (usually) the most efficient
approach. Furthermore, it's a very straight-forward approach, which is
another reason why there's no need to prescribe it (it's a different
story if it were difficult to achieve with so much code that relies on
it for efficiency - we don't have that case).
It's important that it's not prescribed, though. Here's a (admittedly
somewhat contrived, but illustrative) example why: A future smarter
compiler might optimize the statement
copy(dst, append(src, x))
such that the generated code directly copies the result of append(src,
x) into dst, _without_ storing that intermediate result in the
underlying array of src, even if there would be enough space for it. If
we require append to _always_ reuse the underlying array independent of
context, we give up this optimization (and others that we might not be
thinking about now). We don't want to restrict implementations this way.
Furthermore, code that _does_ rely on the result of append reusing the
underlying array if there's enough space should be explicit about it by
not using append in these cases. That code _must_ know that space is
available and so _can_ use easy alternatives to append (say, copy).
In short, requiring re-use when possible is a mistake. Any sensible
implementation should be free to choose the best possible approach.
https://codereview.appspot.com/14419054/