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

strings: Builder causes unnecessary allocation on Reset() #24716

Closed
ghost opened this issue Apr 5, 2018 · 1 comment
Closed

strings: Builder causes unnecessary allocation on Reset() #24716

ghost opened this issue Apr 5, 2018 · 1 comment

Comments

@ghost
Copy link

ghost commented Apr 5, 2018

What version of Go are you using (go version)?

1.10

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

Any

What did you do?

Spotted a performance degradation after switching from bytes.Buffer to strings.Builder.

https://golang.org/src/bytes/buffer.go#L95
https://golang.org/src/strings/builder.go#L53

What did you expect to see?

strings.Builder reusing the allocated array after Reset().

What did you see instead?

Unnecessary memory allocations. A lot of them.

@robpike
Copy link
Contributor

robpike commented Apr 5, 2018

Working as intended. Since it's aliasing the contents of a byte slice to a string when returning the result, it cannot reuse the data and must abandon it on reset. Perhaps the answer is just not to use strings.Builder for this case. It's not a solution to all problems, just an alternative.

@robpike robpike closed this as completed Apr 5, 2018
@golang golang locked and limited conversation to collaborators Apr 5, 2019
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

2 participants