aboutsummaryrefslogtreecommitdiff
path: root/src/io
diff options
context:
space:
mode:
authorOlivier Mengué <olivier.mengue@gmail.com>2023-04-18 22:13:27 +0200
committerGopher Robot <gobot@golang.org>2023-04-20 16:22:09 +0000
commit0d699b6cb3e6be63846bbbee8cc7bcbfd6cb9500 (patch)
treeb6e2f0f8f926429dfdac36a10a2431d2d04952ef /src/io
parent466e6dae9570ac88ef15c5f1bda9a59d7253cfee (diff)
downloadgo-0d699b6cb3e6be63846bbbee8cc7bcbfd6cb9500.tar.gz
go-0d699b6cb3e6be63846bbbee8cc7bcbfd6cb9500.zip
io: ReadAll: do not check for realloc in first round
Refactor io.ReadAll to check for realloc of the buffer only after the first read. Fixes: #59702 Change-Id: I93b99139e6756f21738d47e7d9ad08e1d167258e Reviewed-on: https://go-review.googlesource.com/c/go/+/486236 Auto-Submit: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Diffstat (limited to 'src/io')
-rw-r--r--src/io/io.go9
1 files changed, 5 insertions, 4 deletions
diff --git a/src/io/io.go b/src/io/io.go
index 630ab73b56..7b8ee10a56 100644
--- a/src/io/io.go
+++ b/src/io/io.go
@@ -694,10 +694,6 @@ func (c nopCloserWriterTo) WriteTo(w Writer) (n int64, err error) {
func ReadAll(r Reader) ([]byte, error) {
b := make([]byte, 0, 512)
for {
- if len(b) == cap(b) {
- // Add more capacity (let append pick how much).
- b = append(b, 0)[:len(b)]
- }
n, err := r.Read(b[len(b):cap(b)])
b = b[:len(b)+n]
if err != nil {
@@ -706,5 +702,10 @@ func ReadAll(r Reader) ([]byte, error) {
}
return b, err
}
+
+ if len(b) == cap(b) {
+ // Add more capacity (let append pick how much).
+ b = append(b, 0)[:len(b)]
+ }
}
}