diff options
| author | Jorropo <jorropo.pgm@gmail.com> | 2023-11-08 16:08:26 +0100 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-11-21 00:52:06 +0000 |
| commit | 195c88b202695e90b2ff41b3f1a03bc19685baa6 (patch) | |
| tree | 74f99b29ec4951a49dff89208a36d2e13f379682 /src/net/http | |
| parent | cc7b4b3c3685a688638363bf5be9150b04b75945 (diff) | |
| download | go-195c88b202695e90b2ff41b3f1a03bc19685baa6.tar.xz | |
net/http: use pointers to array for copyBufPool
This is inspired by CL 539915, I'm only submitting now that
CL 456435 has been merged.
This divide the number of objects kept alive by the heap by two
and remove the slice header allocation in New and in the put back.
Change-Id: Ibcd5166bac5a37f365a533e09a28f3b79f81ad58
Reviewed-on: https://go-review.googlesource.com/c/go/+/543515
Reviewed-by: Damien Neil <dneil@google.com>
Auto-Submit: Damien Neil <dneil@google.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/net/http')
| -rw-r--r-- | src/net/http/server.go | 22 | ||||
| -rw-r--r-- | src/net/http/transfer.go | 5 |
2 files changed, 16 insertions, 11 deletions
diff --git a/src/net/http/server.go b/src/net/http/server.go index 7fa785dfee..36a03f4a32 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -575,9 +575,8 @@ type writerOnly struct { // to a *net.TCPConn with sendfile, or from a supported src type such // as a *net.TCPConn on Linux with splice. func (w *response) ReadFrom(src io.Reader) (n int64, err error) { - bufp := copyBufPool.Get().(*[]byte) - buf := *bufp - defer copyBufPool.Put(bufp) + buf := getCopyBuf() + defer putCopyBuf(buf) // Our underlying w.conn.rwc is usually a *TCPConn (with its // own ReadFrom method). If not, just fall back to the normal @@ -807,11 +806,18 @@ var ( bufioWriter4kPool sync.Pool ) -var copyBufPool = sync.Pool{ - New: func() any { - b := make([]byte, 32*1024) - return &b - }, +const copyBufPoolSize = 32 * 1024 + +var copyBufPool = sync.Pool{New: func() any { return new([copyBufPoolSize]byte) }} + +func getCopyBuf() []byte { + return copyBufPool.Get().(*[copyBufPoolSize]byte)[:] +} +func putCopyBuf(b []byte) { + if len(b) != copyBufPoolSize { + panic("trying to put back buffer of the wrong size in the copyBufPool") + } + copyBufPool.Put((*[copyBufPoolSize]byte)(b)) } func bufioWriterPool(size int) *sync.Pool { diff --git a/src/net/http/transfer.go b/src/net/http/transfer.go index dffff56b31..d787258487 100644 --- a/src/net/http/transfer.go +++ b/src/net/http/transfer.go @@ -410,9 +410,8 @@ func (t *transferWriter) writeBody(w io.Writer) (err error) { // // This function is only intended for use in writeBody. func (t *transferWriter) doBodyCopy(dst io.Writer, src io.Reader) (n int64, err error) { - bufp := copyBufPool.Get().(*[]byte) - buf := *bufp - defer copyBufPool.Put(bufp) + buf := getCopyBuf() + defer putCopyBuf(buf) n, err = io.CopyBuffer(dst, src, buf) if err != nil && err != io.EOF { |
