I wrote a tcp server, i use bufio.Writer and bufio.Reader to read and write with a connection, after many connections, the heap objects grows many, if bufio support this, i can reduce 4 objects per connections, the bufio.Writer and bufio.Reader inline with my own struct, and buf i use a big buffer reused by bufio.
// sth like this
func (w *Writer) NewWriterBuffer(w io.Writer, buf []byte) error {
w.buf = buf
// ...
}
type MyStruct struct {
w *bufio.Writer
// others
}
// switch to
type MyStruct struct {
w bufio.Writer
r bufio.Reader
// others
}
func NewMyStruct() *MyStruct {
s := new(MyStruct)
s.w.NewWriterBuffer(xxx, mybuf)
s.r.NewReaderBuffer(xxx, mybuf1)
}