Skip to content

Commit a3a9b10

Browse files
Azhovangopherbot
authored andcommitted
os: ReadFile: don't check for re-allocation in the first iteration
At the beginning of the for-loop iteration cap(data) > len(data) always. Therefore, in the first iteration, this check becomes unnecessary. we can move this check to after the read operation. Change-Id: I205e4a842ced74f31124b45a39b70523b56ad840 GitHub-Last-Rev: 2fdf25d GitHub-Pull-Request: #60473 Reviewed-on: https://go-review.googlesource.com/c/go/+/498915 Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Rob Pike <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent df0a129 commit a3a9b10

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/os/file.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -725,10 +725,6 @@ func ReadFile(name string) ([]byte, error) {
725725

726726
data := make([]byte, 0, size)
727727
for {
728-
if len(data) >= cap(data) {
729-
d := append(data[:cap(data)], 0)
730-
data = d[:len(data)]
731-
}
732728
n, err := f.Read(data[len(data):cap(data)])
733729
data = data[:len(data)+n]
734730
if err != nil {
@@ -737,6 +733,11 @@ func ReadFile(name string) ([]byte, error) {
737733
}
738734
return data, err
739735
}
736+
737+
if len(data) >= cap(data) {
738+
d := append(data[:cap(data)], 0)
739+
data = d[:len(data)]
740+
}
740741
}
741742
}
742743

0 commit comments

Comments
 (0)