Skip to content

bufio: Reader and Writer Support add a method use caller's buf? #13650

@Terry-Mao

Description

@Terry-Mao

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)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions