Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: refactor ReadAll to not check for realloc before first read #59702

Closed
dolmen opened this issue Apr 19, 2023 · 3 comments
Closed

io: refactor ReadAll to not check for realloc before first read #59702

dolmen opened this issue Apr 19, 2023 · 3 comments

Comments

@dolmen
Copy link
Contributor

dolmen commented Apr 19, 2023

The current implementation of io.ReadAll checks for realloc before each read.

I propose instead to check for realloc after each successful read. This will avoid a check before the first read as we know the buffer is already allocated and empty.

Current implementation:

func ReadAll(r Reader) ([]byte, error) {
	b := make([]byte, 0, 512)
	for {
		if len(b) == cap(b) {
			// Add more capacity (let append pick how much).
			b = append(b, 0)[:len(b)]
		}
		n, err := r.Read(b[len(b):cap(b)])
		b = b[:len(b)+n]
		if err != nil {
			if err == EOF {
				err = nil
			}
			return b, err
		}
	}
}

My proposal:

func ReadAll(r Reader) ([]byte, error) {
	b := make([]byte, 0, 512)
	for {
		n, err := r.Read(b[len(b):cap(b)])
		b = b[:len(b)+n]
		if err != nil {
			if err == EOF {
				err = nil
			}
			return b, err
		}

		if len(b) == cap(b) {
			// Add more capacity (let append pick how much).
			b = append(b, 0)[:len(b)]
		}
	}
}
@dolmen
Copy link
Contributor Author

dolmen commented Apr 19, 2023

@ianlancetaylor
Copy link
Contributor

Thanks, but no need to open an issue if you are going to send in a patch anyhow.

@gopherbot
Copy link

Change https://go.dev/cl/486236 mentions this issue: io.ReadAll: do not check for realloc in first round

@golang golang locked and limited conversation to collaborators Apr 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants