golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.89k stars 17.65k forks source link

proposal: bufio: introduce a NewWriterBuffer #69970

Open ahmedabdou14 opened 2 weeks ago

ahmedabdou14 commented 2 weeks ago

Proposal Details

Background

The bufio package currently only provides 2 APIs to create a new Writer

Problem

Both functions internally allocate a buffer, which may not be ideal for all use cases.

Motivation

There are scenarios where developers may want to manage buffer allocation themselves, such as using a sync.Pool of buffers or byte slices. In such cases, the current bufio APIs force a new buffer allocation each time a bufio.Writer is created, thus limiting the benefits of reusing buffers through a pool.

Proposal

Add a new function to the bufio package:

func NewWriterBuffer(w io.Writer, buf []byte) *Writer
gabyhelp commented 2 weeks ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

seankhliao commented 1 week ago

Note that this was rejected in #13650 by saying to put the entire bufio.Writer in the pool. Why is this not sufficient?

ahmedabdou14 commented 1 week ago

Ya i only saw that issue after i posted this issue. However i left it open as there are still use cases where using a pool of []byte instead of a pool of bufio.Writer would come in handy.

example: what if you need a pool of byte slices to perform some logic and also want to reuse the same pool to create bufio writers? Currently, you will have to have 2 separate pools, one for bufio writers and the other for byte slices.

adonovan commented 1 week ago

example: what if you need a pool of byte slices to perform some logic and also want to reuse the same pool to create bufio writers? Currently, you will have to have 2 separate pools, one for bufio writers and the other for byte slices.

Yes, you would need two pools. But the change proposed here would break the encapsulation of the buffer internal to bufio.Writer, which seems like a high price to pay to avoid the need for two pools in rare cases.

ahmedabdou14 commented 1 week ago

tbf, it is scary what could happen if devs reuse the pointer to the buffer outside the pool cycle. Maybe this feature can be an external package? I would be down to do one if it was decided not to move forward with this request.

adonovan commented 1 week ago

Maybe this feature can be an external package?

I'm not sure what you are suggesting. Are you proposing to make a fork of bufio that has the new functions? If so, you can do that in your own repo without asking for permission here.

ahmedabdou14 commented 1 week ago

Yes exactly it can be a fork if it was decided not be added in the standard library

ahmedabdou14 commented 1 week ago

For those looking for this feature, I have created the package here with the proposed interface and tests https://pkg.go.dev/github.com/ahmedabdou14/bufio