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>
"go/token"
"go/types"
"internal/pkgbits"
+ "internal/saferio"
"io"
"os"
"os/exec"
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
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)