it depends on the body of the loop
for i, b := range []byte(str) {
b[i]++
}
(you could construct a more complex and useful example) would overwrite the string, but strings are constant.
so with proper analysis, the compiler could avoid the allocation, but not in general.
-rob
you're right. the (pseudo-)copy is unreachable so the allocation is unnecessary, i think. but i've been wrong before.
-rob
That's a possible optimization but not one that is done.
It may be worth doing, but it's important to make sure
it happens in both compilers. Otherwise you get very
different performance characteristics in the two worlds.
This happened once before, when I made
*(*T)(unsafe.Pointer(&x))
recognized by gc and not trigger an allocation (it is how
math.Float32bits etc get implemented). That math tests
that used it ran gccgo-compiled binaries out of memory
before Ian added the same logic to gccgo. I'm a little wary
of alloc-removing optimizations after that, but I think they
are in the end unavoidable.
Russ