1
0
mirror of https://github.com/golang/go synced 2024-09-29 13:34:30 -06:00

go/internal/gcimporter: use saferio.ReadData instead of io.ReadAll when possible

saferio.ReadData avoids unnecessary allocations because the buffer can be
preallocated with the right size (up to a limit) instead of having to resize
and copy it step by step.

Change-Id: Id70f6908971d4f126c601a9571ac3c67ea0accdc
Reviewed-on: https://go-review.googlesource.com/c/go/+/481616
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Lasse Folger 2023-04-03 13:52:56 +02:00 committed by Gopher Robot
parent 2afaa01855
commit 19409663a0

View File

@ -13,6 +13,7 @@ import (
"go/token"
"go/types"
"internal/pkgbits"
"internal/saferio"
"io"
"os"
"os/exec"
@ -204,6 +205,7 @@ func Import(fset *token.FileSet, packages map[string]*types.Package, path, srcDi
if exportFormat, err = buf.ReadByte(); err != nil {
return
}
size--
// The unified export format starts with a 'u'; the indexed export
// format starts with an 'i'; and the older binary export format
@ -214,9 +216,10 @@ func Import(fset *token.FileSet, packages map[string]*types.Package, path, srcDi
var data []byte
var r io.Reader = buf
if size >= 0 {
r = io.LimitReader(r, int64(size))
}
if data, err = io.ReadAll(r); err != nil {
if data, err = saferio.ReadData(r, uint64(size)); err != nil {
return
}
} else if data, err = io.ReadAll(r); err != nil {
return
}
s := string(data)