On Saturday, May 25, 2013 5:11:06 AM UTC-6, Jan Mercl wrote:Thinking about it more I (only) now realize why it is not possible to
ever user realloc in this scenario. The compiler cannot decide if
there are or there are not any other pointers to the backing array.
That is actually determinable (at least for certain reasonable cases) with escape analysis, likely the same kind of escape analysis being done today. For many cases, however, it's not necessarily useful: the gc runtime (and I'd imagine the gccgo runtime too) uses a size-based arena allocator, in which any requested allocation will occupy a slot in the smallest size-class that will contain it. For example, make([]byte, 9) would be given an 16-byte reservation, and new([32769]byte) would typically reserve nine 4096-byte pages. Because of arena allocation, if that 9-byte slice were shrunk in-place to 8 bytes, there would still be nothing to occupy the second half, since any 8-byte allocation would be allocated in the 8-byte arena. Shrinking in-place allocations that span multiple pages would be useful in cases mentioned in the SO question, but only if the shrunken data wouldn't fit in a smaller size class.
I think that this kind of potential auto-optimization (shrink-in-place for large allocations) would primarily benefit just the kind of programs written without particular attention to allocations or memory use. Optimizations that could benefit just about everybody, however, include: making append transitively aware of wasted space in an arena slot, so that `append(make([]byte, 15), 0)` wouldn't require a memcpy, and making appends to large allocations look for right-adjacent free pages when reallocating, again avoiding memcpy of the source data and the creation of an additional orphaned object. Of course, both of these optimizations would need to be limited to cases in which the compiler could prove that there is no concurrent access to the backing array at the time of append, and that all pre-append references to the backing array are dead prior to any post-append access, otherwise expected semantics (that the result of the append would be a distinct copy) would be violated.