forked from elastic/elastic-agent-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optimized ReadAll function (elastic#229)
* Add optimized ReadAll function This adds an optimized io.ReadAll that uses a bytes.Buffer + io.Copy internally, improving the buffer growth ratio over the runtime append and taking advange of io.WriterTo when available. The only scenario where this optimizes version is slower than io.ReadAll is when reading < 512 bytes of a reader that does not implement io.WriterTo. For now I only updated tests to use the new function as updating the code requires more careful consideration. While at it, migrate from the deprecated ioutil.ReadAll to io.ReadAll. goos: linux goarch: amd64 pkg: github.com/elastic/elastic-agent-libs/iobuf cpu: Intel(R) Xeon(R) CPU E5-2697A v4 @ 2.60GHz BenchmarkReadAll/size_32B/io.ReadAll/WriterTo-32 7479658 162.4 ns/op 512 B/op 1 allocs/op BenchmarkReadAll/size_32B/io.ReadAll/Reader-32 7000012 169.3 ns/op 512 B/op 1 allocs/op BenchmarkReadAll/size_32B/ReadAll/WriterTo-32 10630838 112.3 ns/op 112 B/op 2 allocs/op BenchmarkReadAll/size_32B/ReadAll/Reader-32 2111246 567.3 ns/op 1584 B/op 3 allocs/op BenchmarkReadAll/size_64B/io.ReadAll/WriterTo-32 7154652 163.2 ns/op 512 B/op 1 allocs/op BenchmarkReadAll/size_64B/io.ReadAll/Reader-32 7223264 166.1 ns/op 512 B/op 1 allocs/op BenchmarkReadAll/size_64B/ReadAll/WriterTo-32 10400562 113.5 ns/op 112 B/op 2 allocs/op BenchmarkReadAll/size_64B/ReadAll/Reader-32 2129949 558.7 ns/op 1584 B/op 3 allocs/op BenchmarkReadAll/size_512B/io.ReadAll/WriterTo-32 2843871 419.1 ns/op 1408 B/op 2 allocs/op BenchmarkReadAll/size_512B/io.ReadAll/Reader-32 2871580 413.1 ns/op 1408 B/op 2 allocs/op BenchmarkReadAll/size_512B/ReadAll/WriterTo-32 4976233 241.5 ns/op 560 B/op 2 allocs/op BenchmarkReadAll/size_512B/ReadAll/Reader-32 2183186 552.9 ns/op 1584 B/op 3 allocs/op BenchmarkReadAll/size_10KB/io.ReadAll/WriterTo-32 142633 8235 ns/op 46080 B/op 10 allocs/op BenchmarkReadAll/size_10KB/io.ReadAll/Reader-32 148326 8229 ns/op 46080 B/op 10 allocs/op BenchmarkReadAll/size_10KB/ReadAll/WriterTo-32 574903 2210 ns/op 10288 B/op 2 allocs/op BenchmarkReadAll/size_10KB/ReadAll/Reader-32 147210 7995 ns/op 32304 B/op 7 allocs/op BenchmarkReadAll/size_100KB/io.ReadAll/WriterTo-32 13171 90853 ns/op 514304 B/op 18 allocs/op BenchmarkReadAll/size_100KB/io.ReadAll/Reader-32 12892 91787 ns/op 514304 B/op 18 allocs/op BenchmarkReadAll/size_100KB/ReadAll/WriterTo-32 51472 22420 ns/op 106544 B/op 2 allocs/op BenchmarkReadAll/size_100KB/ReadAll/Reader-32 21568 55070 ns/op 261680 B/op 10 allocs/op BenchmarkReadAll/size_1MB/io.ReadAll/WriterTo-32 1220 983276 ns/op 5241098 B/op 27 allocs/op BenchmarkReadAll/size_1MB/io.ReadAll/Reader-32 1089 990818 ns/op 5241100 B/op 27 allocs/op BenchmarkReadAll/size_1MB/ReadAll/WriterTo-32 4153 294507 ns/op 1048627 B/op 2 allocs/op BenchmarkReadAll/size_1MB/ReadAll/Reader-32 1195 944781 ns/op 4193852 B/op 14 allocs/op * check error returned by Seek * update ReadAll doc * fix linter error in doc * nolint for http.Serve * fix license headers * fix code duplication * use t.TempDir instead of os.MkdirTemp * remove os.CreateTemp --------- Co-authored-by: Denis <denis@rdner.de>
- Loading branch information
Showing
16 changed files
with
232 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package iobuf | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
) | ||
|
||
// ReadAll reads all data from r and returns it as a byte slice. | ||
// A successful call returns err == nil, not err == EOF. It does not | ||
// treat an EOF as an error to be reported. | ||
// | ||
// This function is similar to io.ReadAll, but uses a bytes.Buffer to | ||
// accumulate the data, which has a more efficient growing algorithm and | ||
// uses io.WriterTo if r implements it. | ||
func ReadAll(r io.Reader) ([]byte, error) { | ||
var buf bytes.Buffer | ||
_, err := io.Copy(&buf, r) | ||
return buf.Bytes(), err | ||
} |
Oops, something went wrong.