From: Jabar Asadi Date: Sun, 28 May 2023 06:45:23 +0000 (+0000) Subject: os: ReadFile: don't check for re-allocation in the first iteration X-Git-Tag: go1.22rc1~1539 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a3a9b1049e59672e4bd56b597b273ef1b07313a0;p=gostls13.git 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: 2fdf25dff2e9984d3a8f8e5e612ea802c88e88a1 GitHub-Pull-Request: golang/go#60473 Reviewed-on: https://go-review.googlesource.com/c/go/+/498915 Reviewed-by: Rob Pike Reviewed-by: Bryan Mills Run-TryBot: Rob Pike TryBot-Result: Gopher Robot Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor --- diff --git a/src/os/file.go b/src/os/file.go index 806c1f2045..2f12c3bdae 100644 --- a/src/os/file.go +++ b/src/os/file.go @@ -725,10 +725,6 @@ func ReadFile(name string) ([]byte, error) { data := make([]byte, 0, size) for { - if len(data) >= cap(data) { - d := append(data[:cap(data)], 0) - data = d[:len(data)] - } n, err := f.Read(data[len(data):cap(data)]) data = data[:len(data)+n] if err != nil { @@ -737,6 +733,11 @@ func ReadFile(name string) ([]byte, error) { } return data, err } + + if len(data) >= cap(data) { + d := append(data[:cap(data)], 0) + data = d[:len(data)] + } } }