Skip to content

proposal: bufio: add (*reader).ResetWithSize to initialize as fields and use with Peek #61146

@Jorropo

Description

@Jorropo

I would like to embed the bufio as reader in my fields to avoid pointers to pointers, reduce allocations and memory used:

 type mything struct{
  someStuff thisIsSomeStuff
- reader *bufio.Reader
+ reader bufio.Reader
 }

This is doable by calling thing.reader.Reset(r) however I also need to use (*reader).Peek but I can't specify the size.
So instead I write: thing.reader = *bufio.NewReaderWithSize(r, maximumBlockSize) which feels wrong.

Proposal:

// ResetWithSize is like [Reset] but it will allocate a new buffer if the current buffer is smaller than required.
func (*Reader) ResetWithSize(r io.Reader, size int)

Potential implementation:

// ResetWithSize is like [Reset] but it will allocate a new buffer if the current buffer is smaller than required.
func (b *Reader) ResetWithSize(r io.Reader, size int) {
 if len(b.buf) < size {
  b.buf = make([]byte, size)
 }
 b.reset(r)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions