Hi, folks,
I could use some assistance getting rid of some unnecessary heap allocations. I have code that needs to write individual bytes to an io.Writer. (The Writer implementation given to my code is probably buffered, but my code shouldn't rely on a particular concrete type.) The relevant part of code looks like this:
func writeByte(b byte) {
var buf [1]byte
buf[0] = b
_, err = w.Write(buf[:])
}
Unfortunately, I find that every call of this function allocates a byte on the heap. The compiler's escape analysis can't be sure that the Write method doesn't keep a copy of the pointer it's given, so "buf" escapes to the heap.
How can I implement a writeByte function, against an unknown io.Writer implementation, that doesn't allocate heap memory?
Thanks,
Steve