io.Copy uses an internal 32KiB buffer. I would expect it to be stack allocated by doing something like:
var tmp [32*1024]byte
buf := tmp[:]
Instead, they heap-allocate the buffer:
buf := make([]byte, 32*1024)
(see
http://golang.org/src/pkg/io/io.go#L350).
I'm wondering if anyone knows what drove this decision or has an idea of why one might do this? My first-pass guess is that the buffer isn't always used, and so allocating it dynamically saves wasted stack space in cases where it's not used, although that strikes me as a tradeoff that's not worth it for the allocation time costs.
Cheers,
Josh