Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Terry-Mao opened this issue Dec 17, 2015 · 1 comment
Closed

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

Terry-Mao opened this issue Dec 17, 2015 · 1 comment

Comments

@Terry-Mao
Copy link

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)
}
@bradfitz
Copy link
Contributor

You can reuse bufio Readers and Writers to save on garbage. Look at their Reset methods.

I don't think we'll be making further API changes to bufio.

@golang golang locked and limited conversation to collaborators Dec 29, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants